Skip to content

Commit 9d9d8d3

Browse files
lfolgergopherbot
authored andcommitted
encoding/proto[json|text]: accept lower case names for group-like fields
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>
1 parent 6c3ebca commit 9d9d8d3

4 files changed

Lines changed: 30 additions & 14 deletions

File tree

encoding/prototext/decode_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,16 @@ delimited_field {}
657657
desc: "group field name",
658658
inputMessage: &pb2.Nests{},
659659
inputText: `optgroup: {}`,
660-
wantErr: "unknown field: optgroup",
660+
wantMessage: &pb2.Nests{
661+
Optgroup: &pb2.Nests_OptGroup{},
662+
},
661663
}, {
662-
desc: "delimited encoded group-line message field name",
664+
desc: "delimited encoded group-like message field name",
663665
inputMessage: &pbeditions.Nests{},
664-
inputText: `optgroup: {}`,
665-
wantErr: "unknown field: optgroup",
666+
inputText: `optgroup {}`,
667+
wantMessage: &pbeditions.Nests{
668+
Optgroup: &pbeditions.Nests_OptGroup{},
669+
},
666670
}, {
667671
desc: "delimited encoded message field name",
668672
inputMessage: &pbeditions.Nests{},

internal/cmd/generate-types/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ var descListTypesTemplate = template.Must(template.New("").Parse(`
171171
if _, ok := p.byText[d.TextName()]; !ok {
172172
p.byText[d.TextName()] = d
173173
}
174+
if isGroupLike(d) {
175+
lowerJSONName := strings.ToLower(d.JSONName())
176+
if _, ok := p.byJSON[lowerJSONName]; !ok {
177+
p.byJSON[lowerJSONName] = d
178+
}
179+
lowerTextName := strings.ToLower(d.TextName())
180+
if _, ok := p.byText[lowerTextName]; !ok {
181+
p.byText[lowerTextName] = d
182+
}
183+
}
174184
{{- end}}
175185
{{- if .NumberExpr}}
176186
if _, ok := p.byNum[d.Number()]; !ok {
@@ -200,6 +210,7 @@ func writeSource(file, src string) {
200210
"fmt",
201211
"math",
202212
"reflect",
213+
"strings",
203214
"sync",
204215
"unicode/utf8",
205216
"",

internal/conformance/failing_tests_text_format.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,3 @@ Recommended.Proto3.TextFormatInput.StringLiteralShortUnicodeEscapeSurrogatePairB
88
Recommended.Proto3.TextFormatInput.StringLiteralShortUnicodeEscapeSurrogatePairString
99
Recommended.Proto3.TextFormatInput.StringLiteralUnicodeEscapeSurrogatePairLongShortBytes
1010
Recommended.Proto3.TextFormatInput.StringLiteralUnicodeEscapeSurrogatePairLongShortString
11-
Required.Editions.TextFormatInput.DelimitedFieldLowercased.ProtobufOutput
12-
Required.Editions.TextFormatInput.DelimitedFieldLowercased.TextFormatOutput
13-
Required.Editions_Proto2.TextFormatInput.GroupFieldLowercased.ProtobufOutput
14-
Required.Editions_Proto2.TextFormatInput.GroupFieldLowercased.TextFormatOutput
15-
Required.Editions_Proto2.TextFormatInput.GroupFieldLowercasedMultiWord.ProtobufOutput
16-
Required.Editions_Proto2.TextFormatInput.GroupFieldLowercasedMultiWord.TextFormatOutput
17-
Required.Proto2.TextFormatInput.GroupFieldLowercased.ProtobufOutput
18-
Required.Proto2.TextFormatInput.GroupFieldLowercased.TextFormatOutput
19-
Required.Proto2.TextFormatInput.GroupFieldLowercasedMultiWord.ProtobufOutput
20-
Required.Proto2.TextFormatInput.GroupFieldLowercasedMultiWord.TextFormatOutput

internal/filedesc/desc_list_gen.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)