Protocol Buffer optional integer, distinct from zero

╄→гoц情女王★ 提交于 2019-12-04 22:08:27

There are two main options for this in proto3. The first is to use a oneof like you suggested, but actually you only need to have one item in the oneof:

oneof option {
  int64 value = 15;
}

Oneof fields have a notion of presence so you can still determine whether value is absent or zero. The other alternative is to use one of the wrapper types in google/protobuf/wrappers.proto. Each of these wrappers just takes a primitive type and wraps it in a message, and this helps in your situation because submessage fields have presence. Here is what the Int64 wrapper looks like for example:

// Wrapper message for `int64`.
//
// The JSON representation for `Int64Value` is JSON string.
message Int64Value {
  // The int64 value.
  int64 value = 1;
}

Finally, one other thing to consider is that you can always keep using proto2. Both the proto2 and proto3 styles are supported in protobuf versions 3.0 and up and we plan to continue supporting proto2 indefinitely.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!