machinekit / machinetalk-protobuf

Protobuf declarations for machinekit messages
MIT License
10 stars 11 forks source link

Make all fields optional and set sensibe default values #53

Open machinekoder opened 8 years ago

machinekoder commented 8 years ago

It is good design practice to make all fields optional. It is not only recommended with Protocol Buffers but also Apache Thrift which has similar IDL language:

Any new fields that you add should be optional. This means that any messages serialized by code
using your "old" message format can be parsed by your new generated code, as they won’t be
missing any required elements. You should set up sensible default values for these elements so that
new code can properly interact with messages generated by old code. Similarly, messages created
by your new code can be parsed by your old code

See: http://diwakergupta.github.io/thrift-missing-guide/thrift.pdf

With Protobuf 3 there only will be optional fields as a result of feedback to the problem.

machinekoder commented 8 years ago

Another good tip from the thrift guide:

Non-required fields can be removed, as long as the tag number is not used again in your
updated message type (it may be better to rename the field instead, perhaps adding the prefix
"OBSOLETE_", so that future users of your .thrift can’t accidentally reuse the number).
machinekoder commented 7 years ago

Protobuf3 removes the option to set default values instead type specific values are used.

dkhughes commented 7 years ago

@machinekoder Is a move to proto3 in the works? The new language support (c#, go, etc.) is only available in proto3+. Plus, they promise not to change the API as fluidly as before. nanopb has supported proto3 since 0.3.7, but I don't think Jessie is supporting it. Would probably need to make a package like czmq. Pretty sure stretch is already supporting proto3 with the default packages.

machinekoder commented 7 years ago

@dkhughes You can already compile Machinekit with Protobuf 3.x.

machinekoder commented 7 years ago

Regarding default values, I have no idea what Protobuf 3.x does with proto2 files when it comes to default values.

dkhughes commented 7 years ago

I meant proto3 syntax in the proto files. Protoc will not generate code for proto2 files in some of the newer languages.