1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package proto_test
33
34 import (
35 "testing"
36
37 "github.com/gogo/protobuf/proto"
38 proto3pb "github.com/gogo/protobuf/proto/proto3_proto"
39 pb "github.com/gogo/protobuf/proto/test_proto"
40 )
41
42 func TestDiscardUnknown(t *testing.T) {
43 tests := []struct {
44 desc string
45 in, want proto.Message
46 }{{
47 desc: "Nil",
48 in: nil, want: nil,
49 }, {
50 desc: "NilPtr",
51 in: (*proto3pb.Message)(nil), want: (*proto3pb.Message)(nil),
52 }, {
53 desc: "Nested",
54 in: &proto3pb.Message{
55 Name: "Aaron",
56 Nested: &proto3pb.Nested{Cute: true, XXX_unrecognized: []byte("blah")},
57 XXX_unrecognized: []byte("blah"),
58 },
59 want: &proto3pb.Message{
60 Name: "Aaron",
61 Nested: &proto3pb.Nested{Cute: true},
62 },
63 }, {
64 desc: "Slice",
65 in: &proto3pb.Message{
66 Name: "Aaron",
67 Children: []*proto3pb.Message{
68 {Name: "Sarah", XXX_unrecognized: []byte("blah")},
69 {Name: "Abraham", XXX_unrecognized: []byte("blah")},
70 },
71 XXX_unrecognized: []byte("blah"),
72 },
73 want: &proto3pb.Message{
74 Name: "Aaron",
75 Children: []*proto3pb.Message{
76 {Name: "Sarah"},
77 {Name: "Abraham"},
78 },
79 },
80 }, {
81 desc: "OneOf",
82 in: &pb.Communique{
83 Union: &pb.Communique_Msg{Msg: &pb.Strings{
84 StringField: proto.String("123"),
85 XXX_unrecognized: []byte("blah"),
86 }},
87 XXX_unrecognized: []byte("blah"),
88 },
89 want: &pb.Communique{
90 Union: &pb.Communique_Msg{Msg: &pb.Strings{StringField: proto.String("123")}},
91 },
92 }, {
93 desc: "Map",
94 in: &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{
95 0x4002: {
96 Exact: proto.Bool(true),
97 XXX_unrecognized: []byte("blah"),
98 },
99 }},
100 want: &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{
101 0x4002: {Exact: proto.Bool(true)},
102 }},
103 }, {
104 desc: "Extension",
105 in: func() proto.Message {
106 m := &pb.MyMessage{
107 Count: proto.Int32(42),
108 Somegroup: &pb.MyMessage_SomeGroup{
109 GroupField: proto.Int32(6),
110 XXX_unrecognized: []byte("blah"),
111 },
112 XXX_unrecognized: []byte("blah"),
113 }
114 proto.SetExtension(m, pb.E_Ext_More, &pb.Ext{
115 Data: proto.String("extension"),
116 XXX_unrecognized: []byte("blah"),
117 })
118 return m
119 }(),
120 want: func() proto.Message {
121 m := &pb.MyMessage{
122 Count: proto.Int32(42),
123 Somegroup: &pb.MyMessage_SomeGroup{GroupField: proto.Int32(6)},
124 }
125 proto.SetExtension(m, pb.E_Ext_More, &pb.Ext{Data: proto.String("extension")})
126 return m
127 }(),
128 }}
129
130
131 for _, tt := range tests {
132
133 in := tt.in
134 if in != nil {
135 in = proto.Clone(tt.in)
136 }
137
138 var m LegacyMessage
139 m.Message, _ = in.(*proto3pb.Message)
140 m.Communique, _ = in.(*pb.Communique)
141 m.MessageWithMap, _ = in.(*pb.MessageWithMap)
142 m.MyMessage, _ = in.(*pb.MyMessage)
143 proto.DiscardUnknown(&m)
144 if !proto.Equal(in, tt.want) {
145 t.Errorf("test %s/Legacy, expected unknown fields to be discarded\ngot %v\nwant %v", tt.desc, in, tt.want)
146 }
147 }
148
149 for _, tt := range tests {
150 proto.DiscardUnknown(tt.in)
151 if !proto.Equal(tt.in, tt.want) {
152 t.Errorf("test %s, expected unknown fields to be discarded\ngot %v\nwant %v", tt.desc, tt.in, tt.want)
153 }
154 }
155 }
156
157
158
159
160 type LegacyMessage struct {
161 Message *proto3pb.Message
162 Communique *pb.Communique
163 MessageWithMap *pb.MessageWithMap
164 MyMessage *pb.MyMessage
165 }
166
167 func (m *LegacyMessage) Reset() { *m = LegacyMessage{} }
168 func (m *LegacyMessage) String() string { return proto.CompactTextString(m) }
169 func (*LegacyMessage) ProtoMessage() {}
170
View as plain text