Optional fields and restrictions in protocol buffers (protobuf) v3.0.0-alpha-2

I am currently playing with v3.0.0-alpha-2 Google Protocol Buffers .

As far as I understand, v3 removes the required keyword, extensions keyword and default values โ€‹โ€‹for fields to simplify the proto language.

What I do not know is the meaning of the optional keyword in proto3.

Example:

 syntax = "proto3"; package fw.example; message ExampleMessage { optional string optional_string = 1; string normal_string = 2; } 

Question: What is the difference between optional_string and normal_string other than name and tag?

I already read the following resources (they seem to be the only publicly available yet for v3 protobuf):

But they donโ€™t even mention the optional keyword.

  • Is optional deprecated in proto3 since a field is always optional?
  • How can I force mandatory fields with proto3 to be completed if required gone?

It seems that in proto3 you can no longer distinguish between undefined fields and fields set by the client for the (implicit) default value.

Is it best to wrap each proto3 message in a language-specific class? I use C ++ and I need to make sure certain fields are set. It seems that validation should be done manually in language-specific source text in contrast with proto2.

Can someone enlighten me, what is the best approach to apply proto3 restrictions, but allow the evolution of the circuit? At the moment, I think the new API should be written around proto3 messages, so the client does not deal directly with the code generated by proto3, but with custom API code. Is that right?

Maybe someone can show me a concrete example for discussion.

I'm pretty confused, as the v3 release notes say the following:

We recommend that new users of protocol buffers use proto3. However, we do not usually recommend that existing users migrate from proto2 from proto3 due to the incompatibility API, and we will continue to support proto2 for a long time.

If proto3 is the way to work, why is everything more complicated? It seems to me that I need to write a lot more code than with proto2.

+6
source share
3 answers

My reasoning is this:

Since required deprecated, then everything is optional . Thus, there is no reason for an explicit keyword.

The following paragraph from the manual indicates how the values โ€‹โ€‹are initialized if not set:

Please note that for scalar message fields, after parsing the message, there is no way to say whether the field was explicitly set to the default value (for example, whether the logical value was false) or simply not set at all: you should keep this in mind when defining types of messages.

So optional really means "if you did not install it explicitly, we will install it by default for you!"

Please note that this allows them to do some good optimization (do not include the value on the wire if it is set by default):

For example, it does not have a logical value that switches any behavior when set to false, if you do not want this behavior to occur by default as well. Also note that if the scalar field of the message is set to its default, the value will not be serialized on the wire.

+6
source

The final version of proto3 does not have the optional keyword. In proto3, all fields are optional .

+1
source

Source: https://habr.com/ru/post/986283/


All Articles