[protobuf] cmd/protoc-gen-go: avoid referencing remote enum values by name

57 views
Skip to first unread message

Joe Tsai (Gerrit)

unread,
May 13, 2021, 1:03:53 AM5/13/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Joe Tsai has uploaded this change for review.

View Change

cmd/protoc-gen-go: avoid referencing remote enum values by name

The Go generator has historically always prefixed an enum value
with the name of the enum type, when it was unnecessary to do so.

For example:
enum Status {
STATUS_FAILED = 0;
STATUS_PASSED = 1;
}
would be generated as:
type Status int32
const (
Status_STATUS_FAILED Status = 0
Status_STATUS_PASSED Status = 1
)

It is common for the enum values to be manually prefixed by the
enum type since protobuf enums use C++ namespace rules where
enum types and enum values are in the same namespace scope.
Thus, having the Go generator add a prefix is redundant.
See https://github.com/golang/protobuf/issues/513.

Some custom Go generators like protoc-gen-gogo allow removing
the prefix with the gogoproto.goproto_enum_prefix feature.
However, this leads to interoperability issues between
protoc-gen-go and protoc-gen-gogo, where the enum value names
cannot be accurately inferred.

Avoid this problem by just hard-coding the enum value number
for values declared in other packages. This provides benefits
in interoperability at the small cost of enum values possibly
being stale if their value were ever changed in a remote package.
However, this would only occur with use of proto2 enums and
default values, which seems to be an exceptionally rare situation.

Before:
Default_MyMessage_MyField = remotepb.FooEnum_FOO_ENUM
After:
Default_MyMessage_MyField = remotepb.FooEnum(4) // remotepb.FooEnum_FOO_ENUM

Before:
func (x *MyMessage) GetField() remotepb.FooEnum {
...
return remotepb.FooEnum_FOO_ZERO
}
After:
func (x *MyMessage) GetField() remotepb.FooEnum {
...
return remotepb.FooEnum(0) // always 0 for proto3 and often 0 for proto2
}

Change-Id: I3a06cd553f2eaf6124666f6c36c196d500d35718
---
M cmd/protoc-gen-go/internal_gengo/main.go
M cmd/protoc-gen-go/testdata/import_public/a.pb.go
M cmd/protoc-gen-go/testdata/import_public/b.pb.go
M internal/testprotos/conformance/test_messages_proto3.pb.go
M internal/testprotos/fieldtrack/fieldtrack.pb.go
M internal/testprotos/test/test.pb.go
M internal/testprotos/test/test.proto
M internal/testprotos/textpb2/test.pb.go
M types/known/apipb/api.pb.go
9 files changed, 1,410 insertions(+), 1,313 deletions(-)


To view, visit change 319649. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: protobuf
Gerrit-Branch: master
Gerrit-Change-Id: I3a06cd553f2eaf6124666f6c36c196d500d35718
Gerrit-Change-Number: 319649
Gerrit-PatchSet: 1
Gerrit-Owner: Joe Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Joe Tsai <joe...@digital-static.net>
Gerrit-MessageType: newchange

Ingo Oeser (Gerrit)

unread,
May 14, 2021, 10:56:02 AM5/14/21
to Joe Tsai, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Joe Tsai.

View Change

1 comment:

  • Patchset:

    • Patch Set #1:

      I am not sure this makes things better.

      Now a compile error that an developer expects when the enums are changed incompatibly are not detected by the compiler anymore.

      Optionally (or always) generating additional prefix-less variants of the enum values (if they don't conflict) might be an alternative fix.

To view, visit change 319649. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: protobuf
Gerrit-Branch: master
Gerrit-Change-Id: I3a06cd553f2eaf6124666f6c36c196d500d35718
Gerrit-Change-Number: 319649
Gerrit-PatchSet: 1
Gerrit-Owner: Joe Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Joe Tsai <joe...@digital-static.net>
Gerrit-CC: Ingo Oeser <night...@googlemail.com>
Gerrit-Attention: Joe Tsai <joe...@digital-static.net>
Gerrit-Comment-Date: Fri, 14 May 2021 14:55:55 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Joe Tsai (Gerrit)

unread,
May 14, 2021, 3:04:59 PM5/14/21
to Joe Tsai, goph...@pubsubhelper.golang.org, Joe Tsai, Ingo Oeser, golang-co...@googlegroups.com

Attention is currently required from: Ingo Oeser, Joe Tsai.

View Change

1 comment:

  • Patchset:

    • Patch Set #1:

      Now a compile error that an developer expects when the enums are changed incompatibly are not detected by the compiler anymore.

      Can you explain with a concrete example?

      In proto3, there are no default values and the first enum value must be zero. Thus, it is always safe to hard-code zero.

      In proto2, you may have default values (which is rare), and the first enum value might not be zero (which is also rare). In the rare situation someone changes the meaning of the enums values, then we might have hardcoded a stale value, but the changes of this happening are pretty low.

    • Optionally (or always) generating additional prefix-less variants of the enum values (if they don't conflict) might be an alternative fix.

    • Probably, but that seems like an orthogonal step to take in conjunction with this one. Even if we generated the prefix-less enums, it doesn't change the fact that we don't know if we're allowed to reference them or not (which is what this CL aims to reduce friction on).

To view, visit change 319649. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: protobuf
Gerrit-Branch: master
Gerrit-Change-Id: I3a06cd553f2eaf6124666f6c36c196d500d35718
Gerrit-Change-Number: 319649
Gerrit-PatchSet: 1
Gerrit-Owner: Joe Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Joe Tsai <joe...@digital-static.net>
Gerrit-CC: Ingo Oeser <night...@googlemail.com>
Gerrit-CC: Joe Tsai <thebroke...@gmail.com>
Gerrit-Attention: Ingo Oeser <night...@googlemail.com>
Gerrit-Attention: Joe Tsai <joe...@digital-static.net>
Gerrit-Comment-Date: Fri, 14 May 2021 19:04:55 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Ingo Oeser <night...@googlemail.com>
Gerrit-MessageType: comment

Ingo Oeser (Gerrit)

unread,
May 14, 2021, 3:17:14 PM5/14/21
to Joe Tsai, goph...@pubsubhelper.golang.org, Joe Tsai, golang-co...@googlegroups.com

Attention is currently required from: Joe Tsai.

Patch set 1:Code-Review +1

View Change

1 comment:

  • Patchset:

    • Patch Set #1:

      Thanks for the explanation that this is about non-zero default values only. I clearly misjudged the scope of this change.

To view, visit change 319649. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: protobuf
Gerrit-Branch: master
Gerrit-Change-Id: I3a06cd553f2eaf6124666f6c36c196d500d35718
Gerrit-Change-Number: 319649
Gerrit-PatchSet: 1
Gerrit-Owner: Joe Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Ingo Oeser <night...@googlemail.com>
Gerrit-Reviewer: Joe Tsai <joe...@digital-static.net>
Gerrit-CC: Joe Tsai <thebroke...@gmail.com>
Gerrit-Attention: Joe Tsai <joe...@digital-static.net>
Gerrit-Comment-Date: Fri, 14 May 2021 19:17:08 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Joe Tsai (Gerrit)

unread,
May 14, 2021, 4:21:01 PM5/14/21
to Joe Tsai, goph...@pubsubhelper.golang.org, Ingo Oeser, Joe Tsai, golang-co...@googlegroups.com

Attention is currently required from: Ingo Oeser, Joe Tsai.

View Change

1 comment:

  • Patchset:

    • Patch Set #1:

      Thanks for the explanation that this is about non-zero default values only. […]

    • I still appreciate the review. Thanks!

To view, visit change 319649. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: protobuf
Gerrit-Branch: master
Gerrit-Change-Id: I3a06cd553f2eaf6124666f6c36c196d500d35718
Gerrit-Change-Number: 319649
Gerrit-PatchSet: 1
Gerrit-Owner: Joe Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Ingo Oeser <night...@googlemail.com>
Gerrit-Reviewer: Joe Tsai <joe...@digital-static.net>
Gerrit-CC: Joe Tsai <thebroke...@gmail.com>
Gerrit-Attention: Ingo Oeser <night...@googlemail.com>
Gerrit-Attention: Joe Tsai <joe...@digital-static.net>
Gerrit-Comment-Date: Fri, 14 May 2021 20:20:57 +0000

Damien Neil (Gerrit)

unread,
May 20, 2021, 4:40:40 PM5/20/21
to Joe Tsai, goph...@pubsubhelper.golang.org, Ingo Oeser, Joe Tsai, golang-co...@googlegroups.com

Attention is currently required from: Ingo Oeser, Joe Tsai.

Patch set 1:Code-Review +2

View Change

    To view, visit change 319649. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: protobuf
    Gerrit-Branch: master
    Gerrit-Change-Id: I3a06cd553f2eaf6124666f6c36c196d500d35718
    Gerrit-Change-Number: 319649
    Gerrit-PatchSet: 1
    Gerrit-Owner: Joe Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Ingo Oeser <night...@googlemail.com>
    Gerrit-Reviewer: Joe Tsai <joe...@digital-static.net>
    Gerrit-CC: Joe Tsai <thebroke...@gmail.com>
    Gerrit-Attention: Ingo Oeser <night...@googlemail.com>
    Gerrit-Attention: Joe Tsai <joe...@digital-static.net>
    Gerrit-Comment-Date: Thu, 20 May 2021 20:40:34 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Joe Tsai (Gerrit)

    unread,
    May 24, 2021, 6:22:00 PM5/24/21
    to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Damien Neil, Ingo Oeser, Joe Tsai, golang-co...@googlegroups.com

    Joe Tsai submitted this change.

    View Change

    Approvals: Damien Neil: Looks good to me, approved Joe Tsai: Trusted
    Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/319649
    Trust: Joe Tsai <joe...@digital-static.net>
    Reviewed-by: Damien Neil <dn...@google.com>

    ---
    M cmd/protoc-gen-go/internal_gengo/main.go
    M cmd/protoc-gen-go/testdata/import_public/a.pb.go
    M cmd/protoc-gen-go/testdata/import_public/b.pb.go
    M internal/testprotos/conformance/test_messages_proto3.pb.go
    M internal/testprotos/fieldtrack/fieldtrack.pb.go
    M internal/testprotos/test/test.pb.go
    M internal/testprotos/test/test.proto
    M internal/testprotos/textpb2/test.pb.go
    M types/known/apipb/api.pb.go
    9 files changed, 1,410 insertions(+), 1,313 deletions(-)


    1 is the latest approved patch-set. No files were changed between the latest approved patch-set and the submitted one.

    To view, visit change 319649. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: protobuf
    Gerrit-Branch: master
    Gerrit-Change-Id: I3a06cd553f2eaf6124666f6c36c196d500d35718
    Gerrit-Change-Number: 319649
    Gerrit-PatchSet: 3
    Gerrit-Owner: Joe Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Ingo Oeser <night...@googlemail.com>
    Gerrit-Reviewer: Joe Tsai <joe...@digital-static.net>
    Gerrit-CC: Joe Tsai <thebroke...@gmail.com>
    Gerrit-MessageType: merged
    Reply all
    Reply to author
    Forward
    0 new messages