Skip to content

Commit

Permalink
all: implement strip_enum_prefix editions feature
Browse files Browse the repository at this point in the history
This change required updating editions_defaults.binpb with
--edition_defaults_maximum=2024 so that we can use edition 2024 in our
testdata/enumprefix.proto test file.

For end users, this feature will only be available once:
1. protoc declares support for edition 2024 (expected early 2025).
2. protoc-gen-go declares support for edition 2024.

related to golang/protobuf#513

Change-Id: Ib8daeecae39ef32eff942279707d256c312f2a53
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/618979
Reviewed-by: Lasse Folger <[email protected]>
Reviewed-by: Mike Kruskal <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
  • Loading branch information
stapelberg committed Oct 23, 2024
1 parent fb995f1 commit d14ebce
Show file tree
Hide file tree
Showing 16 changed files with 626 additions and 28 deletions.
29 changes: 29 additions & 0 deletions cmd/protoc-gen-go/enumprefix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"testing"

"google.golang.org/protobuf/cmd/protoc-gen-go/testdata/enumprefix"
)

func TestStripEnumPrefix(t *testing.T) {
// The file default for enumprefix.proto is to strip prefixes:
_ = enumprefix.Strip_ZERO

// The enum Both should generate both names:
_ = enumprefix.Both_ZERO
_ = enumprefix.Both_BOTH_ZERO

// The enum BothNoPrefix is configured to generate both names, but there is
// no prefix to be stripped, so only an unmodified name is generated.
_ = enumprefix.BothNoPrefix_ZERO

// The enum BothButOne is configured to generate both names, except for the
// ONE value, where only the prefixed name is generated.
_ = enumprefix.BothButOne_ZERO
_ = enumprefix.BothButOne_BOTH_BUT_ONE_ONE
}
18 changes: 18 additions & 0 deletions cmd/protoc-gen-go/internal_gengo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func genEnum(g *protogen.GeneratedFile, f *fileInfo, e *enumInfo) {

// Enum value constants.
g.P("const (")
anyOldName := false
for _, value := range e.Values {
g.AnnotateSymbol(value.GoIdent.GoName, protogen.Annotation{Location: value.Location})
leadingComments := appendDeprecationSuffix(value.Comments.Leading,
Expand All @@ -268,9 +269,26 @@ func genEnum(g *protogen.GeneratedFile, f *fileInfo, e *enumInfo) {
g.P(leadingComments,
value.GoIdent, " ", e.GoIdent, " = ", value.Desc.Number(),
trailingComment(value.Comments.Trailing))

if value.PrefixedAlias.GoName != "" &&
value.PrefixedAlias.GoName != value.GoIdent.GoName {
anyOldName = true
}
}
g.P(")")
g.P()
if anyOldName {
g.P("// Old (prefixed) names for ", e.GoIdent, " enum values.")
g.P("const (")
for _, value := range e.Values {
if value.PrefixedAlias.GoName != "" &&
value.PrefixedAlias.GoName != value.GoIdent.GoName {
g.P(value.PrefixedAlias, " ", e.GoIdent, " = ", value.GoIdent)
}
}
g.P(")")
g.P()
}

// Enum value maps.
g.P("// Enum value maps for ", e.GoIdent, ".")
Expand Down
294 changes: 294 additions & 0 deletions cmd/protoc-gen-go/testdata/enumprefix/enumprefix.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions cmd/protoc-gen-go/testdata/enumprefix/enumprefix.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

edition = "2024";

package goproto.protoc.enumprefix;

import "google/protobuf/go_features.proto";

option go_package = "google.golang.org/protobuf/cmd/protoc-gen-go/testdata/enumprefix";
option features.(pb.go).strip_enum_prefix = STRIP_ENUM_PREFIX_STRIP;

enum Strip {
STRIP_ZERO = 0;
STRIP_ONE = 1;
}

enum Both {
option features.(pb.go).strip_enum_prefix = STRIP_ENUM_PREFIX_GENERATE_BOTH;

BOTH_ZERO = 0;
BOTH_ONE = 1;
}

enum BothNoPrefix {
option features.(pb.go).strip_enum_prefix = STRIP_ENUM_PREFIX_GENERATE_BOTH;

ZERO = 0;
ONE = 1;
}

enum BothButOne {
option features.(pb.go).strip_enum_prefix = STRIP_ENUM_PREFIX_GENERATE_BOTH;

BOTH_BUT_ONE_ZERO = 0;
BOTH_BUT_ONE_ONE = 1
[features.(pb.go).strip_enum_prefix = STRIP_ENUM_PREFIX_KEEP];
}
Loading

0 comments on commit d14ebce

Please sign in to comment.