Browse Source

proto: import change from Google.

Add tests for merging a map field. When src contains a duplicate key,
its value message replaces (not merges with) the value message in dst.
matloob@google.com 9 years ago
parent
commit
f6b4231c7f
3 changed files with 35 additions and 0 deletions
  1. 33 0
      proto/clone_test.go
  2. 1 0
      proto/proto3_proto/proto3.proto
  3. 1 0
      proto/testdata/test.proto

+ 33 - 0
proto/clone_test.go

@@ -195,6 +195,9 @@ var mergeTests = []struct {
 			NameMapping: map[int32]string{6: "Nigel"},
 			MsgMapping: map[int64]*pb.FloatingPoint{
 				0x4001: &pb.FloatingPoint{F: proto.Float64(2.0)},
+				0x4002: &pb.FloatingPoint{
+					F: proto.Float64(2.0),
+				},
 			},
 			ByteMapping: map[bool][]byte{true: []byte("wowsa")},
 		},
@@ -203,6 +206,12 @@ var mergeTests = []struct {
 				6: "Bruce", // should be overwritten
 				7: "Andrew",
 			},
+			MsgMapping: map[int64]*pb.FloatingPoint{
+				0x4002: &pb.FloatingPoint{
+					F:     proto.Float64(3.0),
+					Exact: proto.Bool(true),
+				}, // the entire message should be overwritten
+			},
 		},
 		want: &pb.MessageWithMap{
 			NameMapping: map[int32]string{
@@ -211,6 +220,9 @@ var mergeTests = []struct {
 			},
 			MsgMapping: map[int64]*pb.FloatingPoint{
 				0x4001: &pb.FloatingPoint{F: proto.Float64(2.0)},
+				0x4002: &pb.FloatingPoint{
+					F: proto.Float64(2.0),
+				},
 			},
 			ByteMapping: map[bool][]byte{true: []byte("wowsa")},
 		},
@@ -254,6 +266,27 @@ var mergeTests = []struct {
 			Union: &pb.Communique_Name{"Bobby Tables"},
 		},
 	},
+	{
+		src: &proto3pb.Message{
+			Terrain: map[string]*proto3pb.Nested{
+				"kay_a": &proto3pb.Nested{Cute: true},      // replace
+				"kay_b": &proto3pb.Nested{Bunny: "rabbit"}, // insert
+			},
+		},
+		dst: &proto3pb.Message{
+			Terrain: map[string]*proto3pb.Nested{
+				"kay_a": &proto3pb.Nested{Bunny: "lost"},  // replaced
+				"kay_c": &proto3pb.Nested{Bunny: "bunny"}, // keep
+			},
+		},
+		want: &proto3pb.Message{
+			Terrain: map[string]*proto3pb.Nested{
+				"kay_a": &proto3pb.Nested{Cute: true},
+				"kay_b": &proto3pb.Nested{Bunny: "rabbit"},
+				"kay_c": &proto3pb.Nested{Bunny: "bunny"},
+			},
+		},
+	},
 }
 
 func TestMerge(t *testing.T) {

+ 1 - 0
proto/proto3_proto/proto3.proto

@@ -66,6 +66,7 @@ message Message {
 
 message Nested {
   string bunny = 1;
+  bool cute = 2;
 }
 
 message MessageWithMap {

+ 1 - 0
proto/testdata/test.proto

@@ -488,6 +488,7 @@ message GroupNew {
 
 message FloatingPoint {
   required double f = 1;
+  optional bool exact = 2;
 }
 
 message MessageWithMap {