Skip to content

Commit 056d5ce

Browse files
committed
Accept comma and semicolon characters in text format maps.
This makes this consistent with the C++ parser and structs in general.
1 parent b955ee1 commit 056d5ce

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

proto/text_parser.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,9 @@ func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
567567
if err := p.readAny(key, props.mkeyprop); err != nil {
568568
return err
569569
}
570+
if err := p.consumeOptionalSeparator(); err != nil {
571+
return err
572+
}
570573
if err := p.consumeToken("value"); err != nil {
571574
return err
572575
}
@@ -576,6 +579,9 @@ func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
576579
if err := p.readAny(val, props.mvalprop); err != nil {
577580
return err
578581
}
582+
if err := p.consumeOptionalSeparator(); err != nil {
583+
return err
584+
}
579585
if err := p.consumeToken(terminator); err != nil {
580586
return err
581587
}
@@ -605,14 +611,10 @@ func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
605611
}
606612
}
607613

608-
// For backward compatibility, permit a semicolon or comma after a field.
609-
tok = p.next()
610-
if tok.err != nil {
611-
return tok.err
612-
}
613-
if tok.value != ";" && tok.value != "," {
614-
p.back()
614+
if err := p.consumeOptionalSeparator(); err != nil {
615+
return err
615616
}
617+
616618
}
617619

618620
if reqCount > 0 {
@@ -621,6 +623,19 @@ func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
621623
return reqFieldErr
622624
}
623625

626+
// consumeOptionalSeparator consumes an optional semicolon or comma.
627+
// It is used in readStruct to provide backward compatibility.
628+
func (p *textParser) consumeOptionalSeparator() error {
629+
tok := p.next()
630+
if tok.err != nil {
631+
return tok.err
632+
}
633+
if tok.value != ";" && tok.value != "," {
634+
p.back()
635+
}
636+
return nil
637+
}
638+
624639
func (p *textParser) readAny(v reflect.Value, props *Properties) error {
625640
tok := p.next()
626641
if tok.err != nil {

proto/text_parser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ func TestProto3TextParsing(t *testing.T) {
462462
func TestMapParsing(t *testing.T) {
463463
m := new(MessageWithMap)
464464
const in = `name_mapping:<key:1234 value:"Feist"> name_mapping:<key:1 value:"Beatles">` +
465-
`msg_mapping:<key:-4 value:<f: 2.0>>` +
465+
`msg_mapping:<key:-4, value:<f: 2.0>,>` + // separating commas are okay
466466
`msg_mapping<key:-2 value<f: 4.0>>` + // no colon after "value"
467467
`byte_mapping:<key:true value:"so be it">`
468468
want := &MessageWithMap{

0 commit comments

Comments
 (0)