Why has methods are not generated using protoc 3.17.* if in protobuf repo they say it should?

huangapple go评论123阅读模式
英文:

Why has methods are not generated using protoc 3.17.* if in protobuf repo they say it should?

问题

阅读这里:https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md#go-example 中的Golang示例:

  1. m := GetProto()
  2. if (m.HasFoo()) {
  3. // Clear the field:
  4. m.Foo = nil
  5. } else {
  6. // Field is not present, so set it.
  7. m.Foo = proto.Int32(1);
  8. }

如果我使用:

  1. protoc pkg/user.proto --go_out=. --go_opt=module=my_app --go-grpc_out=. --go-grpc_opt=module=my_app

和:

  1. syntax = "proto3";
  2. package example;
  3. message MyPlayer {
  4. uint64 id = 1;
  5. optional string description = 2;
  6. uint32 qty = 3;
  7. optional uint64 age = 4;
  8. }

它不会生成任何has<T>()方法。

为什么?

  1. // Code generated by protoc-gen-go. DO NOT EDIT.
  2. // versions:
  3. // protoc-gen-go v1.26.0
  4. // protoc v3.17.3

如果我使用生成的MyPlayer proto字段而不是方法,我错了吗?

例如:

  1. if MyPlayer.description != nil {
  2. description = *MyPlayer.description
  3. }

而不是

  1. description = MyPlayer.GetDescription()

这不是我想要的(我想要检测nil值)。

英文:

Reading here: https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md#go-example the Golang example:

  1. m := GetProto()
  2. if (m.HasFoo()) {
  3. // Clear the field:
  4. m.Foo = nil
  5. } else {
  6. // Field is not present, so set it.
  7. m.Foo = proto.Int32(1);
  8. }

if I use:

  1. protoc pkg/user.proto --go_out=. --go_opt=module=my_app --go-grpc_out=. --go-grpc_opt=module=my_app

with:

  1. syntax = &quot;proto3&quot;;
  2. package example;
  3. message MyPlayer {
  4. uint64 id = 1;
  5. optional string description = 2;
  6. uint32 qty = 3;
  7. optional uint64 age = 4;
  8. }

it doesn't generate any has&lt;T&gt;() method.

Why?

  1. // Code generated by protoc-gen-go. DO NOT EDIT.
  2. // versions:
  3. // protoc-gen-go v1.26.0
  4. // protoc v3.17.3

Am I wrong if I use MyPlayer generated proto fields instead of methods?

Example:

  1. if MyPlayer.description != nil {
  2. description = *MyPlayer.description
  3. }

instead of

  1. description = MyPlayer.GetDescription()

which is not what I want (I want to detect nil values).

答案1

得分: 3

这份文档是错误的,如此处所述:https://github.com/golang/protobuf/issues/1336:

https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md#go-example 上的文档是错误的。在 proto3 中使用 "optional" 会生成与 proto2 中相同的字段。

那份文档是错误的。生成的代码中没有 Has 方法。通过将字段与 nil 进行比较来测试是否存在。

正确重写这些示例:

  1. // 字段 foo 没有存在性。
  2. // 如果字段 foo 不为 0,则将其设置为 0。
  3. // 如果字段 foo 为 0,则将其设置为 1。
  4. m := GetProto()
  5. if m.Foo != 0 {
  6. // “清除”字段:
  7. m.Foo = 0
  8. } else {
  9. // 默认值:字段可能不存在。
  10. m.Foo = 1
  11. }
  1. // 字段 foo 有存在性。
  2. // 如果 foo 已设置,则清除它。
  3. // 如果 foo 未设置,则将其设置为 1。
  4. m := GetProto()
  5. if m.Foo != nil {
  6. // 清除字段:
  7. m.Foo = nil
  8. } else {
  9. // 字段不存在,因此设置它。
  10. m.Foo = proto.Int32(1)
  11. }

修复该文档的 PR:
protocolbuffers/protobuf#8788

英文:

That docs is wrong, as reported here: https://github.com/golang/protobuf/issues/1336:

> The documentation on https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md#go-example is incorrect. Using "optional" in proto3 makes the field be generated just as it would in proto2.

> That doc is wrong. There are no Has methods in the generated code. Test for presence by comparing fields to nil.
>
> Rewriting those examples correctly:
>
>
&gt; // Field foo does not have presence.
&gt; // If field foo is not 0, set it to 0.
&gt; // If field foo is 0, set it to 1.
&gt; m := GetProto()
&gt; if m.Foo != 0 {
&gt; // &quot;Clear&quot; the field:
&gt; m.Foo = 0
&gt; } else {
&gt; // Default value: field may not have been present.
&gt; m.Foo = 1
&gt; }
&gt;

>
>
&gt; // Field foo has presence.
&gt; // If foo is set, clear it.
&gt; // If foo is not set, set it to 1.
&gt; m := GetProto()
&gt; if m.Foo != nil {
&gt; // Clear the field:
&gt; m.Foo = nil
&gt; } else {
&gt; // Field is not present, so set it.
&gt; m.Foo = proto.Int32(1)
&gt; }
&gt;

> PR to fix that doc:
> protocolbuffers/protobuf#8788

huangapple
  • 本文由 发表于 2021年7月3日 02:51:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/68230086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定