lrascao / rebar3_gpb_plugin

A rebar3 plugin for automatically compiling .proto files using the gpb protobuf compiler
MIT License
55 stars 37 forks source link

Add possibility to have per-proto option overrides #137

Closed tomas-abrahamsson closed 1 year ago

tomas-abrahamsson commented 3 years ago

I was toying with possibilities to add per-proto gpb_opts as a possible solution to tomas-abrahamsson/gpb#197 and came up with this change. In its current form, it is a bit of proof-of-concept, for discussiong the idea. If things look good, I can go on to add documentation and examples and such things.

My idea was to add a per_proto_override key, which could add to options, or replace them altogether with other options.

I found a gpb_test repo that I used as an example. It has two files, demo_bytes.proto and demo2.proto. The gpb_opts in the rebar.config look like below, notice the module suffix override for "demo2.proto":

{gpb_opts, [
            {i, "src"},
            {msg_name_suffix, "_pb"},
            {o_erl, "src/pb"},
            {o_hrl, "src/pb"},
            {module_name_suffix, "_pb"},
            {strings_as_binaries, false},
            type_specs,
            {per_proto_override,
             [{"demo2.proto", [{module_name_suffix, "_pb2"}]}]}
           ]}.

This is what happens when I compile, notice the different suffix in the for the second proto:

$ DEBUG=1 rebar3 compile
...
compiling "/usr/local/src/gpb_test/src/demo_bytes.proto" to "/usr/local/src/gpb_test/_build/default/lib/gpb_test/src/pb/demo_bytes_pb.erl"
opts:
  [{i,"/usr/local/src/gpb_test/src"},
   {msg_name_suffix,"_pb"},
   {o_erl,"/usr/local/src/gpb_test/_build/default/lib/gpb_test/src/pb"},
   {o_hrl,"/usr/local/src/gpb_test/_build/default/lib/gpb_test/src/pb"},
   {module_name_suffix,"_pb"},
   {strings_as_binaries,false},
   type_specs,
   {per_proto_override,[{"demo2.proto",[{module_name_suffix,"_pb2"}]}]},
   {i,"/usr/local/src/gpb_test/src"},
   {i,"/usr/local/src/gpb_test/src"}]
...
compiling "/usr/local/src/gpb_test/src/demo2.proto" to "/usr/local/src/gpb_test/_build/default/lib/gpb_test/src/pb/demo2_pb.erl"
opts (options from "demo2.proto" were prepended):
  [{module_name_suffix,"_pb2"},
   {module_name_suffix,"_pb2"},
   {i,"/usr/local/src/gpb_test/src"},
   {msg_name_suffix,"_pb"},
   {o_erl,"/usr/local/src/gpb_test/_build/default/lib/gpb_test/src/pb"},
   {o_hrl,"/usr/local/src/gpb_test/_build/default/lib/gpb_test/src/pb"},
   {module_name_suffix,"_pb"},
   {strings_as_binaries,false},
   type_specs,
   {per_proto_override,[{"demo2.proto",[{module_name_suffix,"_pb2"}]}]},
   {i,"/usr/local/src/gpb_test/src"},
   {i,"/usr/local/src/gpb_test/src"}]
...

Here is some more description of the format extension (text from the commit message)

    [...
     % Common gpb opts
     % These will be used if no per-proto override is specified
     % NB: Currently output-dir options cannot be overridden
     %     ie, any common {o,Dir}, {o_erl,Dir} or {o_hrl,Dir} are used
     ...
     {per_proto_override,
      [{"dir1/a.proto", [{module_name_prefix, "dir1_"}]},
       {"dir2/a.proto", [{module_name_prefix, "dir2_"}]},
       {"dir2/b.proto", {replace,    % <-- means to override all common options
                         [{verify, false}, ...]}},
       {"dir2/c.proto", {merge,    % <-- means to prepend to common options,
                                   %     this is the default,
                                   %     as in the a.proto examples above
                         [{verify, false}, ...]}}]}]
lrascao commented 3 years ago

👍 looks good, wish i had written tests since the beginning

tomas-abrahamsson commented 1 year ago

I'm withdrawing this due to inactivity on the tomas-abrahamsson/gpb#197. If you would want to merge anyway, then it would be fine with me.