Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package proto_test |
| 6 | |
| 7 | import ( |
| 8 | "testing" |
| 9 | |
| 10 | "google.golang.org/protobuf/internal/encoding/pack" |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 11 | "google.golang.org/protobuf/proto" |
| 12 | |
| 13 | testpb "google.golang.org/protobuf/internal/testprotos/test" |
| 14 | ) |
| 15 | |
| 16 | func TestMerge(t *testing.T) { |
| 17 | dst := new(testpb.TestAllTypes) |
| 18 | src := (*testpb.TestAllTypes)(nil) |
| 19 | proto.Merge(dst, src) |
| 20 | |
| 21 | // Mutating the source should not affect dst. |
| 22 | |
| 23 | tests := []struct { |
| 24 | desc string |
| 25 | dst proto.Message |
| 26 | src proto.Message |
| 27 | want proto.Message |
| 28 | mutator func(proto.Message) // if provided, is run on src after merging |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 29 | }{{ |
| 30 | desc: "merge from nil message", |
| 31 | dst: new(testpb.TestAllTypes), |
| 32 | src: (*testpb.TestAllTypes)(nil), |
| 33 | want: new(testpb.TestAllTypes), |
| 34 | }, { |
| 35 | desc: "clone a large message", |
| 36 | dst: new(testpb.TestAllTypes), |
| 37 | src: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 38 | OptionalInt64: proto.Int64(0), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 39 | OptionalNestedEnum: testpb.TestAllTypes_NestedEnum(1).Enum(), |
| 40 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 41 | A: proto.Int32(100), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 42 | }, |
| 43 | RepeatedSfixed32: []int32{1, 2, 3}, |
| 44 | RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 45 | {A: proto.Int32(200)}, |
| 46 | {A: proto.Int32(300)}, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 47 | }, |
| 48 | MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{ |
| 49 | "fizz": 400, |
| 50 | "buzz": 500, |
| 51 | }, |
| 52 | MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 53 | "foo": {A: proto.Int32(600)}, |
| 54 | "bar": {A: proto.Int32(700)}, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 55 | }, |
| 56 | OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
| 57 | &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 58 | A: proto.Int32(800), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 59 | }, |
| 60 | }, |
| 61 | }, |
| 62 | want: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 63 | OptionalInt64: proto.Int64(0), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 64 | OptionalNestedEnum: testpb.TestAllTypes_NestedEnum(1).Enum(), |
| 65 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 66 | A: proto.Int32(100), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 67 | }, |
| 68 | RepeatedSfixed32: []int32{1, 2, 3}, |
| 69 | RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 70 | {A: proto.Int32(200)}, |
| 71 | {A: proto.Int32(300)}, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 72 | }, |
| 73 | MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{ |
| 74 | "fizz": 400, |
| 75 | "buzz": 500, |
| 76 | }, |
| 77 | MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 78 | "foo": {A: proto.Int32(600)}, |
| 79 | "bar": {A: proto.Int32(700)}, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 80 | }, |
| 81 | OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
| 82 | &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 83 | A: proto.Int32(800), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 84 | }, |
| 85 | }, |
| 86 | }, |
| 87 | mutator: func(mi proto.Message) { |
| 88 | m := mi.(*testpb.TestAllTypes) |
| 89 | *m.OptionalInt64++ |
| 90 | *m.OptionalNestedEnum++ |
| 91 | *m.OptionalNestedMessage.A++ |
| 92 | m.RepeatedSfixed32[0]++ |
| 93 | *m.RepeatedNestedMessage[0].A++ |
| 94 | delete(m.MapStringNestedEnum, "fizz") |
| 95 | *m.MapStringNestedMessage["foo"].A++ |
| 96 | *m.OneofField.(*testpb.TestAllTypes_OneofNestedMessage).OneofNestedMessage.A++ |
| 97 | }, |
| 98 | }, { |
| 99 | desc: "merge bytes", |
| 100 | dst: &testpb.TestAllTypes{ |
| 101 | OptionalBytes: []byte{1, 2, 3}, |
| 102 | RepeatedBytes: [][]byte{{1, 2}, {3, 4}}, |
| 103 | MapStringBytes: map[string][]byte{"alpha": {1, 2, 3}}, |
| 104 | }, |
| 105 | src: &testpb.TestAllTypes{ |
| 106 | OptionalBytes: []byte{4, 5, 6}, |
| 107 | RepeatedBytes: [][]byte{{5, 6}, {7, 8}}, |
| 108 | MapStringBytes: map[string][]byte{"alpha": {4, 5, 6}, "bravo": {1, 2, 3}}, |
| 109 | }, |
| 110 | want: &testpb.TestAllTypes{ |
| 111 | OptionalBytes: []byte{4, 5, 6}, |
| 112 | RepeatedBytes: [][]byte{{1, 2}, {3, 4}, {5, 6}, {7, 8}}, |
| 113 | MapStringBytes: map[string][]byte{"alpha": {4, 5, 6}, "bravo": {1, 2, 3}}, |
| 114 | }, |
| 115 | mutator: func(mi proto.Message) { |
| 116 | m := mi.(*testpb.TestAllTypes) |
| 117 | m.OptionalBytes[0]++ |
| 118 | m.RepeatedBytes[0][0]++ |
| 119 | m.MapStringBytes["alpha"][0]++ |
| 120 | }, |
| 121 | }, { |
| 122 | desc: "merge singular fields", |
| 123 | dst: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 124 | OptionalInt32: proto.Int32(1), |
| 125 | OptionalInt64: proto.Int64(1), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 126 | OptionalNestedEnum: testpb.TestAllTypes_NestedEnum(10).Enum(), |
| 127 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 128 | A: proto.Int32(100), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 129 | Corecursive: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 130 | OptionalInt64: proto.Int64(1000), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 131 | }, |
| 132 | }, |
| 133 | }, |
| 134 | src: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 135 | OptionalInt64: proto.Int64(2), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 136 | OptionalNestedEnum: testpb.TestAllTypes_NestedEnum(20).Enum(), |
| 137 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 138 | A: proto.Int32(200), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 139 | }, |
| 140 | }, |
| 141 | want: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 142 | OptionalInt32: proto.Int32(1), |
| 143 | OptionalInt64: proto.Int64(2), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 144 | OptionalNestedEnum: testpb.TestAllTypes_NestedEnum(20).Enum(), |
| 145 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 146 | A: proto.Int32(200), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 147 | Corecursive: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 148 | OptionalInt64: proto.Int64(1000), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 149 | }, |
| 150 | }, |
| 151 | }, |
| 152 | mutator: func(mi proto.Message) { |
| 153 | m := mi.(*testpb.TestAllTypes) |
| 154 | *m.OptionalInt64++ |
| 155 | *m.OptionalNestedEnum++ |
| 156 | *m.OptionalNestedMessage.A++ |
| 157 | }, |
| 158 | }, { |
| 159 | desc: "merge list fields", |
| 160 | dst: &testpb.TestAllTypes{ |
| 161 | RepeatedSfixed32: []int32{1, 2, 3}, |
| 162 | RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 163 | {A: proto.Int32(100)}, |
| 164 | {A: proto.Int32(200)}, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 165 | }, |
| 166 | }, |
| 167 | src: &testpb.TestAllTypes{ |
| 168 | RepeatedSfixed32: []int32{4, 5, 6}, |
| 169 | RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 170 | {A: proto.Int32(300)}, |
| 171 | {A: proto.Int32(400)}, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 172 | }, |
| 173 | }, |
| 174 | want: &testpb.TestAllTypes{ |
| 175 | RepeatedSfixed32: []int32{1, 2, 3, 4, 5, 6}, |
| 176 | RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 177 | {A: proto.Int32(100)}, |
| 178 | {A: proto.Int32(200)}, |
| 179 | {A: proto.Int32(300)}, |
| 180 | {A: proto.Int32(400)}, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 181 | }, |
| 182 | }, |
| 183 | mutator: func(mi proto.Message) { |
| 184 | m := mi.(*testpb.TestAllTypes) |
| 185 | m.RepeatedSfixed32[0]++ |
| 186 | *m.RepeatedNestedMessage[0].A++ |
| 187 | }, |
| 188 | }, { |
| 189 | desc: "merge map fields", |
| 190 | dst: &testpb.TestAllTypes{ |
| 191 | MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{ |
| 192 | "fizz": 100, |
| 193 | "buzz": 200, |
| 194 | "guzz": 300, |
| 195 | }, |
| 196 | MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 197 | "foo": {A: proto.Int32(400)}, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 198 | }, |
| 199 | }, |
| 200 | src: &testpb.TestAllTypes{ |
| 201 | MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{ |
| 202 | "fizz": 1000, |
| 203 | "buzz": 2000, |
| 204 | }, |
| 205 | MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 206 | "foo": {A: proto.Int32(3000)}, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 207 | "bar": {}, |
| 208 | }, |
| 209 | }, |
| 210 | want: &testpb.TestAllTypes{ |
| 211 | MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{ |
| 212 | "fizz": 1000, |
| 213 | "buzz": 2000, |
| 214 | "guzz": 300, |
| 215 | }, |
| 216 | MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 217 | "foo": {A: proto.Int32(3000)}, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 218 | "bar": {}, |
| 219 | }, |
| 220 | }, |
| 221 | mutator: func(mi proto.Message) { |
| 222 | m := mi.(*testpb.TestAllTypes) |
| 223 | delete(m.MapStringNestedEnum, "fizz") |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 224 | m.MapStringNestedMessage["bar"].A = proto.Int32(1) |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 225 | }, |
| 226 | }, { |
| 227 | desc: "merge oneof message fields", |
| 228 | dst: &testpb.TestAllTypes{ |
| 229 | OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
| 230 | &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 231 | A: proto.Int32(100), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 232 | }, |
| 233 | }, |
| 234 | }, |
| 235 | src: &testpb.TestAllTypes{ |
| 236 | OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
| 237 | &testpb.TestAllTypes_NestedMessage{ |
| 238 | Corecursive: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 239 | OptionalInt64: proto.Int64(1000), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 240 | }, |
| 241 | }, |
| 242 | }, |
| 243 | }, |
| 244 | want: &testpb.TestAllTypes{ |
| 245 | OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
| 246 | &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 247 | A: proto.Int32(100), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 248 | Corecursive: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 249 | OptionalInt64: proto.Int64(1000), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 250 | }, |
| 251 | }, |
| 252 | }, |
| 253 | }, |
| 254 | mutator: func(mi proto.Message) { |
| 255 | m := mi.(*testpb.TestAllTypes) |
| 256 | *m.OneofField.(*testpb.TestAllTypes_OneofNestedMessage).OneofNestedMessage.Corecursive.OptionalInt64++ |
| 257 | }, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 258 | }, { |
| 259 | desc: "merge oneof scalar fields", |
| 260 | dst: &testpb.TestAllTypes{ |
| 261 | OneofField: &testpb.TestAllTypes_OneofUint32{100}, |
| 262 | }, |
| 263 | src: &testpb.TestAllTypes{ |
| 264 | OneofField: &testpb.TestAllTypes_OneofFloat{3.14152}, |
| 265 | }, |
| 266 | want: &testpb.TestAllTypes{ |
| 267 | OneofField: &testpb.TestAllTypes_OneofFloat{3.14152}, |
| 268 | }, |
| 269 | mutator: func(mi proto.Message) { |
| 270 | m := mi.(*testpb.TestAllTypes) |
| 271 | m.OneofField.(*testpb.TestAllTypes_OneofFloat).OneofFloat++ |
| 272 | }, |
| 273 | }, { |
| 274 | desc: "merge extension fields", |
| 275 | dst: func() proto.Message { |
| 276 | m := new(testpb.TestAllExtensions) |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame^] | 277 | proto.SetExtension(m, testpb.E_OptionalInt32Extension, int32(32)) |
| 278 | proto.SetExtension(m, testpb.E_OptionalNestedMessageExtension, |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 279 | &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 280 | A: proto.Int32(50), |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 281 | }, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 282 | ) |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame^] | 283 | proto.SetExtension(m, testpb.E_RepeatedFixed32Extension, &[]uint32{1, 2, 3}) |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 284 | return m |
| 285 | }(), |
| 286 | src: func() proto.Message { |
| 287 | m := new(testpb.TestAllExtensions) |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame^] | 288 | proto.SetExtension(m, testpb.E_OptionalInt64Extension, int64(64)) |
| 289 | proto.SetExtension(m, testpb.E_OptionalNestedMessageExtension, |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 290 | &testpb.TestAllTypes_NestedMessage{ |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 291 | Corecursive: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 292 | OptionalInt64: proto.Int64(1000), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 293 | }, |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 294 | }, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 295 | ) |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame^] | 296 | proto.SetExtension(m, testpb.E_RepeatedFixed32Extension, &[]uint32{4, 5, 6}) |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 297 | return m |
| 298 | }(), |
| 299 | want: func() proto.Message { |
| 300 | m := new(testpb.TestAllExtensions) |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame^] | 301 | proto.SetExtension(m, testpb.E_OptionalInt32Extension, int32(32)) |
| 302 | proto.SetExtension(m, testpb.E_OptionalInt64Extension, int64(64)) |
| 303 | proto.SetExtension(m, testpb.E_OptionalNestedMessageExtension, |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 304 | &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 305 | A: proto.Int32(50), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 306 | Corecursive: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 307 | OptionalInt64: proto.Int64(1000), |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 308 | }, |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 309 | }, |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 310 | ) |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame^] | 311 | proto.SetExtension(m, testpb.E_RepeatedFixed32Extension, &[]uint32{1, 2, 3, 4, 5, 6}) |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 312 | return m |
| 313 | }(), |
| 314 | }, { |
| 315 | desc: "merge unknown fields", |
| 316 | dst: func() proto.Message { |
| 317 | m := new(testpb.TestAllTypes) |
| 318 | m.ProtoReflect().SetUnknown(pack.Message{ |
| 319 | pack.Tag{Number: 50000, Type: pack.VarintType}, pack.Svarint(-5), |
| 320 | }.Marshal()) |
| 321 | return m |
| 322 | }(), |
| 323 | src: func() proto.Message { |
| 324 | m := new(testpb.TestAllTypes) |
| 325 | m.ProtoReflect().SetUnknown(pack.Message{ |
| 326 | pack.Tag{Number: 500000, Type: pack.VarintType}, pack.Svarint(-50), |
| 327 | }.Marshal()) |
| 328 | return m |
| 329 | }(), |
| 330 | want: func() proto.Message { |
| 331 | m := new(testpb.TestAllTypes) |
| 332 | m.ProtoReflect().SetUnknown(pack.Message{ |
| 333 | pack.Tag{Number: 50000, Type: pack.VarintType}, pack.Svarint(-5), |
| 334 | pack.Tag{Number: 500000, Type: pack.VarintType}, pack.Svarint(-50), |
| 335 | }.Marshal()) |
| 336 | return m |
| 337 | }(), |
| 338 | }} |
| 339 | |
| 340 | for _, tt := range tests { |
| 341 | t.Run(tt.desc, func(t *testing.T) { |
| 342 | // Merge should be semantically equivalent to unmarshaling the |
| 343 | // encoded form of src into the current dst. |
| 344 | b1, err := proto.MarshalOptions{AllowPartial: true}.Marshal(tt.dst) |
| 345 | if err != nil { |
| 346 | t.Fatalf("Marshal(dst) error: %v", err) |
| 347 | } |
| 348 | b2, err := proto.MarshalOptions{AllowPartial: true}.Marshal(tt.src) |
| 349 | if err != nil { |
| 350 | t.Fatalf("Marshal(src) error: %v", err) |
| 351 | } |
| 352 | dst := tt.dst.ProtoReflect().New().Interface() |
| 353 | err = proto.UnmarshalOptions{AllowPartial: true}.Unmarshal(append(b1, b2...), dst) |
| 354 | if err != nil { |
| 355 | t.Fatalf("Unmarshal() error: %v", err) |
| 356 | } |
Joe Tsai | 6c28674 | 2019-07-11 23:15:05 -0700 | [diff] [blame] | 357 | if !proto.Equal(dst, tt.want) { |
Joe Tsai | 2fc306a | 2019-06-20 03:01:22 -0700 | [diff] [blame] | 358 | t.Fatalf("Unmarshal(Marshal(dst)+Marshal(src)) mismatch: got %v, want %v", dst, tt.want) |
| 359 | } |
| 360 | |
| 361 | proto.Merge(tt.dst, tt.src) |
| 362 | if tt.mutator != nil { |
| 363 | tt.mutator(tt.src) // should not be observable by dst |
| 364 | } |
| 365 | if !proto.Equal(tt.dst, tt.want) { |
| 366 | t.Fatalf("Merge() mismatch: got %v, want %v", tt.dst, tt.want) |
| 367 | } |
| 368 | }) |
| 369 | } |
| 370 | } |