Skip to content

Commit

Permalink
encoding/proto[json|text]: accept lower case names for group-like fields
Browse files Browse the repository at this point in the history
This is a result of the discussion in [1]. Before editions, a group defined a multiple things:

* a type
* a field
* an encoding scheme

With editions this has changed and groups no longer exist and the different parts have to be defined individually. Most importantly, the field and the type also had the same name (usually and CamelCase name). To keep compatibility with proto2 groups, [2] introduced a concept of group-like fields and adjusted the Text/JSON parsers to accept the type name instead of the field name for such fields. This means you can convert from proto2 groups to editions without changing the semantics.
Furthermore, to avoid suprises with group-like fields (e.g. when a user by coincident specified a field that is group-like) protobuf decided that group-like fields should always accept the type and the field name for group like fields. This also allows us to eventually emit the field name rather than the type name for group like fields in the future.

This change implements this decision in Go.


[1] protocolbuffers/protobuf#16239
[2] https://go.dev/cl/575916

Change-Id: I701c4cd228d2e0867b2a87771b6c6331459c4910
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/582755
Reviewed-by: Lasse Folger <lassefolger@google.com>
Reviewed-by: Mike Kruskal <mkruskal@google.com>
Commit-Queue: Michael Stapelberg <stapelberg@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
Auto-Submit: Michael Stapelberg <stapelberg@google.com>
  • Loading branch information
lfolger authored and gopherbot committed May 6, 2024
1 parent 6c3ebca commit 9d9d8d3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
12 changes: 8 additions & 4 deletions encoding/prototext/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,16 @@ delimited_field {}
desc: "group field name",
inputMessage: &pb2.Nests{},
inputText: `optgroup: {}`,
wantErr: "unknown field: optgroup",
wantMessage: &pb2.Nests{
Optgroup: &pb2.Nests_OptGroup{},
},
}, {
desc: "delimited encoded group-line message field name",
desc: "delimited encoded group-like message field name",
inputMessage: &pbeditions.Nests{},
inputText: `optgroup: {}`,
wantErr: "unknown field: optgroup",
inputText: `optgroup {}`,
wantMessage: &pbeditions.Nests{
Optgroup: &pbeditions.Nests_OptGroup{},
},
}, {
desc: "delimited encoded message field name",
inputMessage: &pbeditions.Nests{},
Expand Down
11 changes: 11 additions & 0 deletions internal/cmd/generate-types/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ var descListTypesTemplate = template.Must(template.New("").Parse(`
if _, ok := p.byText[d.TextName()]; !ok {
p.byText[d.TextName()] = d
}
if isGroupLike(d) {
lowerJSONName := strings.ToLower(d.JSONName())
if _, ok := p.byJSON[lowerJSONName]; !ok {
p.byJSON[lowerJSONName] = d
}
lowerTextName := strings.ToLower(d.TextName())
if _, ok := p.byText[lowerTextName]; !ok {
p.byText[lowerTextName] = d
}
}
{{- end}}
{{- if .NumberExpr}}
if _, ok := p.byNum[d.Number()]; !ok {
Expand Down Expand Up @@ -200,6 +210,7 @@ func writeSource(file, src string) {
"fmt",
"math",
"reflect",
"strings",
"sync",
"unicode/utf8",
"",
Expand Down
10 changes: 0 additions & 10 deletions internal/conformance/failing_tests_text_format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,3 @@ Recommended.Proto3.TextFormatInput.StringLiteralShortUnicodeEscapeSurrogatePairB
Recommended.Proto3.TextFormatInput.StringLiteralShortUnicodeEscapeSurrogatePairString
Recommended.Proto3.TextFormatInput.StringLiteralUnicodeEscapeSurrogatePairLongShortBytes
Recommended.Proto3.TextFormatInput.StringLiteralUnicodeEscapeSurrogatePairLongShortString
Required.Editions.TextFormatInput.DelimitedFieldLowercased.ProtobufOutput
Required.Editions.TextFormatInput.DelimitedFieldLowercased.TextFormatOutput
Required.Editions_Proto2.TextFormatInput.GroupFieldLowercased.ProtobufOutput
Required.Editions_Proto2.TextFormatInput.GroupFieldLowercased.TextFormatOutput
Required.Editions_Proto2.TextFormatInput.GroupFieldLowercasedMultiWord.ProtobufOutput
Required.Editions_Proto2.TextFormatInput.GroupFieldLowercasedMultiWord.TextFormatOutput
Required.Proto2.TextFormatInput.GroupFieldLowercased.ProtobufOutput
Required.Proto2.TextFormatInput.GroupFieldLowercased.TextFormatOutput
Required.Proto2.TextFormatInput.GroupFieldLowercasedMultiWord.ProtobufOutput
Required.Proto2.TextFormatInput.GroupFieldLowercasedMultiWord.TextFormatOutput
11 changes: 11 additions & 0 deletions internal/filedesc/desc_list_gen.go

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

0 comments on commit 9d9d8d3

Please sign in to comment.