Even though it's not a field on FieldOptions, you can supply a "pseudo-option" for default value named simply default: https://developers.google.com/protocol-buffers/docs/proto#optional When protoc compiles this source, the default value ends up in a field on FieldDescriptorProto. There is one other "pseudo-option" in protocol buffers to be aware of: "json_name", also for fields, also ends up in FieldDescriptorProto instead of FieldOptions when compiled.
Language runtimes that support syntax "proto2" and default values should generate useful API, so that if you use a *method* to query the field (instead of, for example, directly examining a field in an object/struct), the method should return the specified default if the field has not been explicitly set. The default value that is stored in the descriptor is stored as a string. The one thing to keep in mind is that bytes fields have a strange encoding: the string value uses C-escapes for non-printable characters. So you'll have to decode the default for bytes fields. For other types, the string encoding is exactly what you expect (enums are represented as enum value names, not numbers). BTW, the default option in protos does not allow you to define a default value for aggregate fields (e.g. no defaults allowed for repeated/map fields or fields whose type is a nested message). ---- *Josh Humphries* [email protected] On Sun, Nov 12, 2017 at 2:11 AM, <[email protected]> wrote: > I have an message using FieldOptions but I also want to use default values > as well, how do I combine two of those things ? > > extend google.protobuf.MessageOptions { > optional CommandInfo cmd = 5678; > } > > > message TestCmd { > > optional bool BoolFlag = 1 [(info) = { name: "bool-flag" value: "false" > shorthand: "c" usage: "test"} ]; > optional string StringFlag = 2 [ (info) = { name: "string-flag" value: "" > shorthand: "o" usage: "test"}]; > > } > > > > I want that TestCmd.BoolFlag default value to be bool value "false", > currently I'm using string value then convert it to bool, How do I > > specify the default value in a type-safe way ? > > > > > Thanks > > -- > You received this message because you are subscribed to the Google Groups > "Protocol Buffers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/protobuf. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
