Skip to content

Commit

Permalink
all: implement Go-specific GenerateLegacyUnmarshalJSON
Browse files Browse the repository at this point in the history
This change adds the first Go specific editions feature and the
associated proto. The feature is used to control if the legacy
UnmarshalJSON method should be generated for enums. This change only
affects protos using editions.

More tests will be added in a followup change.

Change-Id: Ifb62454d7568bd6d90d0b93f8953adcfe46c45fd
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/561938
Reviewed-by: Michael Stapelberg <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Lasse Folger <[email protected]>
  • Loading branch information
lfolger authored and gopherbot committed Feb 12, 2024
1 parent 99e193e commit 2040e86
Show file tree
Hide file tree
Showing 10 changed files with 908 additions and 10 deletions.
7 changes: 6 additions & 1 deletion cmd/protoc-gen-go/internal_gengo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/internal/encoding/tag"
"google.golang.org/protobuf/internal/filedesc"
"google.golang.org/protobuf/internal/genid"
"google.golang.org/protobuf/internal/version"
"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -288,7 +289,11 @@ func genEnum(g *protogen.GeneratedFile, f *fileInfo, e *enumInfo) {
genEnumReflectMethods(g, f, e)

// UnmarshalJSON method.
if e.genJSONMethod && e.Desc.Syntax() == protoreflect.Proto2 {
needsUnmarshalJSONMethod := e.genJSONMethod && e.Desc.Syntax() == protoreflect.Proto2
if fde, ok := e.Desc.(*filedesc.Enum); ok && fde.L1.EditionFeatures.GenerateLegacyUnmarshalJSON {
needsUnmarshalJSONMethod = true
}
if needsUnmarshalJSONMethod {
g.P("// Deprecated: Do not use.")
g.P("func (x *", e.GoIdent, ") UnmarshalJSON(b []byte) error {")
g.P("num, err := ", protoimplPackage.Ident("X"), ".UnmarshalJSONEnum(x.Descriptor(), b)")
Expand Down
Loading

0 comments on commit 2040e86

Please sign in to comment.