Skip to content

Commit

Permalink
protoc-gen-go: Add JSON tags to oneof values and members
Browse files Browse the repository at this point in the history
Oneof values should include json tags to use proper camelCase name and
can be safely omitted when we're handling the interface.
  • Loading branch information
3v1n0 committed Nov 29, 2023
1 parent 2087447 commit 0dd9e2d
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 123 deletions.
2 changes: 2 additions & 0 deletions cmd/protoc-gen-go/internal_gengo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ func genMessageField(g *protogen.GeneratedFile, f *fileInfo, m *messageInfo, fie

tags := structTags{
{"protobuf_oneof", string(oneof.Desc.Name())},
{"json", string(oneof.Desc.JSONName()) + ",omitempty"},
}
if m.isTracked {
tags = append(tags, gotrackTags...)
Expand Down Expand Up @@ -812,6 +813,7 @@ func genMessageOneofWrapperTypes(g *protogen.GeneratedFile, f *fileInfo, m *mess
goType, _ := fieldGoType(g, f, field)
tags := structTags{
{"protobuf", fieldProtobufTagValue(field)},
{"json", string(field.Desc.JSONName())},
}
if m.isTracked {
tags = append(tags, gotrackTags...)
Expand Down
4 changes: 2 additions & 2 deletions cmd/protoc-gen-go/testdata/comments/comments.pb.go

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

14 changes: 7 additions & 7 deletions cmd/protoc-gen-go/testdata/fieldnames/fieldnames.pb.go

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

12 changes: 6 additions & 6 deletions cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go

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

4 changes: 2 additions & 2 deletions cmd/protoc-gen-go/testdata/issue780_oneof_conflict/test.pb.go

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

46 changes: 23 additions & 23 deletions cmd/protoc-gen-go/testdata/proto2/fields.pb.go

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

4 changes: 2 additions & 2 deletions cmd/protoc-gen-go/testdata/retention/retention.pb.go

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

1 change: 1 addition & 0 deletions internal/filedesc/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ func (od *Oneof) Options() protoreflect.ProtoMessage {
func (od *Oneof) Fields() protoreflect.FieldDescriptors { return &od.L1.Fields }
func (od *Oneof) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, od) }
func (od *Oneof) ProtoType(protoreflect.OneofDescriptor) {}
func (od *Oneof) JSONName() string { return strs.JSONCamelCase(string(od.Name())) }

type (
Extension struct {
Expand Down

0 comments on commit 0dd9e2d

Please sign in to comment.