protocol-buffers

protobuf: when should I used parenthesis around identifiers in options


It is possible to put parenthesis around identifiers in options, as documented in https://protobuf.dev/reference/protobuf/proto3-spec/#option

e.g. you can write

option foo.bar.baz = 1;

and also

option (foo.bar.baz) = 1;

or

option (foo.bar).baz = 1;

What's the difference between them? Why do we need parenthesis in the grammar?


Solution

  • The use of parens syntax is to designate between builtin options (provided by Protobuf/protoc directly), and custom options defined by external parties. More info on Custom Options here: https://protobuf.dev/programming-guides/proto2/#customoptions

    Now, with custom options, there's a specific meaning to

    option (foo.bar.baz) = 1;
    

    vs

    option (foo.bar).baz = 1;
    

    In the first example, you're setting the value of an extension field directly (a scalar field). In the second example, you're setting a field of a sub-message.

    If that's not totally clear, the link above has some great examples of what I'm referring to.