Skip to content

Commit

Permalink
proto: ignore unknown fields in map entries (#561)
Browse files Browse the repository at this point in the history
A proto map entry is conceptually a message with two fields.
In the same way that unknown fields don't cause a parsing error,
map parsing should ignore unknown fields and tags.
Similarly, it treats a missing field as the zero value.

Fixes #403
  • Loading branch information
dsnet committed Mar 14, 2018
1 parent 397ff90 commit b028a76
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 146 deletions.
24 changes: 24 additions & 0 deletions proto/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,36 @@ package proto_test

import (
"fmt"
"reflect"
"testing"

"github.com/golang/protobuf/proto"
ppb "github.com/golang/protobuf/proto/proto3_proto"
)

func TestMap(t *testing.T) {
var b []byte
fmt.Sscanf("a2010c0a044b657931120456616c31a201130a044b657932120556616c3261120456616c32a201240a044b6579330d05000000120556616c33621a0556616c3361120456616c331505000000a20100a201260a044b657934130a07536f6d6555524c1209536f6d655469746c651a08536e69707065743114", "%x", &b)

var m ppb.Message
if err := proto.Unmarshal(b, &m); err != nil {
t.Fatalf("proto.Unmarshal error: %v", err)
}

got := m.StringMap
want := map[string]string{
"": "",
"Key1": "Val1",
"Key2": "Val2",
"Key3": "Val3",
"Key4": "",
}

if !reflect.DeepEqual(got, want) {
t.Errorf("maps differ:\ngot %#v\nwant %#v", got, want)
}
}

func marshalled() []byte {
m := &ppb.IntMaps{}
for i := 0; i < 1000; i++ {
Expand Down
124 changes: 68 additions & 56 deletions proto/proto3_proto/proto3.pb.go

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

2 changes: 2 additions & 0 deletions proto/proto3_proto/proto3.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ message Message {

Message submessage = 17;
repeated Message children = 18;

map<string, string> string_map = 20;
}

message Nested {
Expand Down
Loading

0 comments on commit b028a76

Please sign in to comment.