Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1 | // Copyright 2018 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 | |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 5 | package proto_test |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "reflect" |
| 10 | "testing" |
| 11 | |
| 12 | protoV1 "github.com/golang/protobuf/proto" |
| 13 | "github.com/golang/protobuf/v2/internal/encoding/pack" |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 14 | "github.com/golang/protobuf/v2/internal/scalar" |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 15 | "github.com/golang/protobuf/v2/proto" |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 16 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
Joe Tsai | 1905843 | 2019-02-27 21:46:29 -0800 | [diff] [blame] | 17 | |
| 18 | testpb "github.com/golang/protobuf/v2/internal/testprotos/test" |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 19 | test3pb "github.com/golang/protobuf/v2/internal/testprotos/test3" |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | type testProto struct { |
| 23 | desc string |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 24 | decodeTo []proto.Message |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 25 | wire []byte |
| 26 | } |
| 27 | |
| 28 | func TestDecode(t *testing.T) { |
| 29 | for _, test := range testProtos { |
| 30 | for _, want := range test.decodeTo { |
| 31 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
| 32 | wire := append(([]byte)(nil), test.wire...) |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 33 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
| 34 | if err := proto.Unmarshal(wire, got); err != nil { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 35 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message))) |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | // Aliasing check: Modifying the original wire bytes shouldn't |
| 40 | // affect the unmarshaled message. |
| 41 | for i := range wire { |
| 42 | wire[i] = 0 |
| 43 | } |
| 44 | |
| 45 | if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) { |
| 46 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message))) |
| 47 | } |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | var testProtos = []testProto{ |
| 54 | { |
| 55 | desc: "basic scalar types", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 56 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 57 | OptionalInt32: scalar.Int32(1001), |
| 58 | OptionalInt64: scalar.Int64(1002), |
| 59 | OptionalUint32: scalar.Uint32(1003), |
| 60 | OptionalUint64: scalar.Uint64(1004), |
| 61 | OptionalSint32: scalar.Int32(1005), |
| 62 | OptionalSint64: scalar.Int64(1006), |
| 63 | OptionalFixed32: scalar.Uint32(1007), |
| 64 | OptionalFixed64: scalar.Uint64(1008), |
| 65 | OptionalSfixed32: scalar.Int32(1009), |
| 66 | OptionalSfixed64: scalar.Int64(1010), |
| 67 | OptionalFloat: scalar.Float32(1011.5), |
| 68 | OptionalDouble: scalar.Float64(1012.5), |
| 69 | OptionalBool: scalar.Bool(true), |
| 70 | OptionalString: scalar.String("string"), |
| 71 | OptionalBytes: []byte("bytes"), |
| 72 | OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(), |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 73 | }, &test3pb.TestAllTypes{ |
| 74 | OptionalInt32: 1001, |
| 75 | OptionalInt64: 1002, |
| 76 | OptionalUint32: 1003, |
| 77 | OptionalUint64: 1004, |
| 78 | OptionalSint32: 1005, |
| 79 | OptionalSint64: 1006, |
| 80 | OptionalFixed32: 1007, |
| 81 | OptionalFixed64: 1008, |
| 82 | OptionalSfixed32: 1009, |
| 83 | OptionalSfixed64: 1010, |
| 84 | OptionalFloat: 1011.5, |
| 85 | OptionalDouble: 1012.5, |
| 86 | OptionalBool: true, |
| 87 | OptionalString: "string", |
| 88 | OptionalBytes: []byte("bytes"), |
| 89 | OptionalNestedEnum: test3pb.TestAllTypes_BAR, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 90 | }, build( |
| 91 | &testpb.TestAllExtensions{}, |
| 92 | extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)), |
| 93 | extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)), |
| 94 | extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)), |
| 95 | extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)), |
| 96 | extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)), |
| 97 | extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)), |
| 98 | extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)), |
| 99 | extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)), |
| 100 | extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)), |
| 101 | extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)), |
| 102 | extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)), |
| 103 | extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)), |
| 104 | extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)), |
| 105 | extend(testpb.E_OptionalStringExtension, scalar.String("string")), |
| 106 | extend(testpb.E_OptionalBytesExtension, []byte("bytes")), |
| 107 | extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()), |
| 108 | )}, |
| 109 | wire: pack.Message{ |
| 110 | pack.Tag{1, pack.VarintType}, pack.Varint(1001), |
| 111 | pack.Tag{2, pack.VarintType}, pack.Varint(1002), |
| 112 | pack.Tag{3, pack.VarintType}, pack.Uvarint(1003), |
| 113 | pack.Tag{4, pack.VarintType}, pack.Uvarint(1004), |
| 114 | pack.Tag{5, pack.VarintType}, pack.Svarint(1005), |
| 115 | pack.Tag{6, pack.VarintType}, pack.Svarint(1006), |
| 116 | pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007), |
| 117 | pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008), |
| 118 | pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009), |
| 119 | pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010), |
| 120 | pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5), |
| 121 | pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5), |
| 122 | pack.Tag{13, pack.VarintType}, pack.Bool(true), |
| 123 | pack.Tag{14, pack.BytesType}, pack.String("string"), |
| 124 | pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")), |
| 125 | pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 126 | }.Marshal(), |
| 127 | }, |
| 128 | { |
| 129 | desc: "groups", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 130 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 131 | Optionalgroup: &testpb.TestAllTypes_OptionalGroup{ |
| 132 | A: scalar.Int32(1017), |
| 133 | }, |
| 134 | }, build( |
| 135 | &testpb.TestAllExtensions{}, |
| 136 | extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{ |
| 137 | A: scalar.Int32(1017), |
| 138 | }), |
| 139 | )}, |
| 140 | wire: pack.Message{ |
| 141 | pack.Tag{16, pack.StartGroupType}, |
| 142 | pack.Tag{17, pack.VarintType}, pack.Varint(1017), |
| 143 | pack.Tag{16, pack.EndGroupType}, |
| 144 | }.Marshal(), |
| 145 | }, |
| 146 | { |
| 147 | desc: "groups (field overridden)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 148 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 149 | Optionalgroup: &testpb.TestAllTypes_OptionalGroup{ |
| 150 | A: scalar.Int32(2), |
| 151 | }, |
| 152 | }, build( |
| 153 | &testpb.TestAllExtensions{}, |
| 154 | extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{ |
| 155 | A: scalar.Int32(2), |
| 156 | }), |
| 157 | )}, |
| 158 | wire: pack.Message{ |
| 159 | pack.Tag{16, pack.StartGroupType}, |
| 160 | pack.Tag{17, pack.VarintType}, pack.Varint(1), |
| 161 | pack.Tag{16, pack.EndGroupType}, |
| 162 | pack.Tag{16, pack.StartGroupType}, |
| 163 | pack.Tag{17, pack.VarintType}, pack.Varint(2), |
| 164 | pack.Tag{16, pack.EndGroupType}, |
| 165 | }.Marshal(), |
| 166 | }, |
| 167 | { |
| 168 | desc: "messages", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 169 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 170 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
| 171 | A: scalar.Int32(42), |
| 172 | Corecursive: &testpb.TestAllTypes{ |
| 173 | OptionalInt32: scalar.Int32(43), |
| 174 | }, |
| 175 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 176 | }, &test3pb.TestAllTypes{ |
| 177 | OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{ |
| 178 | A: 42, |
| 179 | Corecursive: &test3pb.TestAllTypes{ |
| 180 | OptionalInt32: 43, |
| 181 | }, |
| 182 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 183 | }, build( |
| 184 | &testpb.TestAllExtensions{}, |
| 185 | extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{ |
| 186 | A: scalar.Int32(42), |
| 187 | Corecursive: &testpb.TestAllTypes{ |
| 188 | OptionalInt32: scalar.Int32(43), |
| 189 | }, |
| 190 | }), |
| 191 | )}, |
| 192 | wire: pack.Message{ |
| 193 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 194 | pack.Tag{1, pack.VarintType}, pack.Varint(42), |
| 195 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 196 | pack.Tag{1, pack.VarintType}, pack.Varint(43), |
| 197 | }), |
| 198 | }), |
| 199 | }.Marshal(), |
| 200 | }, |
| 201 | { |
| 202 | desc: "messages (split across multiple tags)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 203 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 204 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
| 205 | A: scalar.Int32(42), |
| 206 | Corecursive: &testpb.TestAllTypes{ |
| 207 | OptionalInt32: scalar.Int32(43), |
| 208 | }, |
| 209 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 210 | }, &test3pb.TestAllTypes{ |
| 211 | OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{ |
| 212 | A: 42, |
| 213 | Corecursive: &test3pb.TestAllTypes{ |
| 214 | OptionalInt32: 43, |
| 215 | }, |
| 216 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 217 | }, build( |
| 218 | &testpb.TestAllExtensions{}, |
| 219 | extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{ |
| 220 | A: scalar.Int32(42), |
| 221 | Corecursive: &testpb.TestAllTypes{ |
| 222 | OptionalInt32: scalar.Int32(43), |
| 223 | }, |
| 224 | }), |
| 225 | )}, |
| 226 | wire: pack.Message{ |
| 227 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 228 | pack.Tag{1, pack.VarintType}, pack.Varint(42), |
| 229 | }), |
| 230 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 231 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 232 | pack.Tag{1, pack.VarintType}, pack.Varint(43), |
| 233 | }), |
| 234 | }), |
| 235 | }.Marshal(), |
| 236 | }, |
| 237 | { |
| 238 | desc: "messages (field overridden)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 239 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 240 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
| 241 | A: scalar.Int32(2), |
| 242 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 243 | }, &test3pb.TestAllTypes{ |
| 244 | OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{ |
| 245 | A: 2, |
| 246 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 247 | }, build( |
| 248 | &testpb.TestAllExtensions{}, |
| 249 | extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{ |
| 250 | A: scalar.Int32(2), |
| 251 | }), |
| 252 | )}, |
| 253 | wire: pack.Message{ |
| 254 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 255 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 256 | }), |
| 257 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 258 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 259 | }), |
| 260 | }.Marshal(), |
| 261 | }, |
| 262 | { |
| 263 | desc: "basic repeated types", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 264 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 265 | RepeatedInt32: []int32{1001, 2001}, |
| 266 | RepeatedInt64: []int64{1002, 2002}, |
| 267 | RepeatedUint32: []uint32{1003, 2003}, |
| 268 | RepeatedUint64: []uint64{1004, 2004}, |
| 269 | RepeatedSint32: []int32{1005, 2005}, |
| 270 | RepeatedSint64: []int64{1006, 2006}, |
| 271 | RepeatedFixed32: []uint32{1007, 2007}, |
| 272 | RepeatedFixed64: []uint64{1008, 2008}, |
| 273 | RepeatedSfixed32: []int32{1009, 2009}, |
| 274 | RepeatedSfixed64: []int64{1010, 2010}, |
| 275 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 276 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 277 | RepeatedBool: []bool{true, false}, |
| 278 | RepeatedString: []string{"foo", "bar"}, |
| 279 | RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")}, |
| 280 | RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{ |
| 281 | testpb.TestAllTypes_FOO, |
| 282 | testpb.TestAllTypes_BAR, |
| 283 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 284 | }, &test3pb.TestAllTypes{ |
| 285 | RepeatedInt32: []int32{1001, 2001}, |
| 286 | RepeatedInt64: []int64{1002, 2002}, |
| 287 | RepeatedUint32: []uint32{1003, 2003}, |
| 288 | RepeatedUint64: []uint64{1004, 2004}, |
| 289 | RepeatedSint32: []int32{1005, 2005}, |
| 290 | RepeatedSint64: []int64{1006, 2006}, |
| 291 | RepeatedFixed32: []uint32{1007, 2007}, |
| 292 | RepeatedFixed64: []uint64{1008, 2008}, |
| 293 | RepeatedSfixed32: []int32{1009, 2009}, |
| 294 | RepeatedSfixed64: []int64{1010, 2010}, |
| 295 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 296 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 297 | RepeatedBool: []bool{true, false}, |
| 298 | RepeatedString: []string{"foo", "bar"}, |
| 299 | RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")}, |
| 300 | RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{ |
| 301 | test3pb.TestAllTypes_FOO, |
| 302 | test3pb.TestAllTypes_BAR, |
| 303 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 304 | }, build( |
| 305 | &testpb.TestAllExtensions{}, |
| 306 | extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}), |
| 307 | extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}), |
| 308 | extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}), |
| 309 | extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}), |
| 310 | extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}), |
| 311 | extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}), |
| 312 | extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}), |
| 313 | extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}), |
| 314 | extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}), |
| 315 | extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}), |
| 316 | extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}), |
| 317 | extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}), |
| 318 | extend(testpb.E_RepeatedBoolExtension, []bool{true, false}), |
| 319 | extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}), |
| 320 | extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}), |
| 321 | extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{ |
| 322 | testpb.TestAllTypes_FOO, |
| 323 | testpb.TestAllTypes_BAR, |
| 324 | }), |
| 325 | )}, |
| 326 | wire: pack.Message{ |
| 327 | pack.Tag{31, pack.VarintType}, pack.Varint(1001), |
| 328 | pack.Tag{31, pack.VarintType}, pack.Varint(2001), |
| 329 | pack.Tag{32, pack.VarintType}, pack.Varint(1002), |
| 330 | pack.Tag{32, pack.VarintType}, pack.Varint(2002), |
| 331 | pack.Tag{33, pack.VarintType}, pack.Uvarint(1003), |
| 332 | pack.Tag{33, pack.VarintType}, pack.Uvarint(2003), |
| 333 | pack.Tag{34, pack.VarintType}, pack.Uvarint(1004), |
| 334 | pack.Tag{34, pack.VarintType}, pack.Uvarint(2004), |
| 335 | pack.Tag{35, pack.VarintType}, pack.Svarint(1005), |
| 336 | pack.Tag{35, pack.VarintType}, pack.Svarint(2005), |
| 337 | pack.Tag{36, pack.VarintType}, pack.Svarint(1006), |
| 338 | pack.Tag{36, pack.VarintType}, pack.Svarint(2006), |
| 339 | pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007), |
| 340 | pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007), |
| 341 | pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008), |
| 342 | pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008), |
| 343 | pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009), |
| 344 | pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009), |
| 345 | pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010), |
| 346 | pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010), |
| 347 | pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5), |
| 348 | pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5), |
| 349 | pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5), |
| 350 | pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5), |
| 351 | pack.Tag{43, pack.VarintType}, pack.Bool(true), |
| 352 | pack.Tag{43, pack.VarintType}, pack.Bool(false), |
| 353 | pack.Tag{44, pack.BytesType}, pack.String("foo"), |
| 354 | pack.Tag{44, pack.BytesType}, pack.String("bar"), |
| 355 | pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")), |
| 356 | pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")), |
| 357 | pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)), |
| 358 | pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 359 | }.Marshal(), |
| 360 | }, |
| 361 | { |
| 362 | desc: "basic repeated types (packed encoding)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 363 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 364 | RepeatedInt32: []int32{1001, 2001}, |
| 365 | RepeatedInt64: []int64{1002, 2002}, |
| 366 | RepeatedUint32: []uint32{1003, 2003}, |
| 367 | RepeatedUint64: []uint64{1004, 2004}, |
| 368 | RepeatedSint32: []int32{1005, 2005}, |
| 369 | RepeatedSint64: []int64{1006, 2006}, |
| 370 | RepeatedFixed32: []uint32{1007, 2007}, |
| 371 | RepeatedFixed64: []uint64{1008, 2008}, |
| 372 | RepeatedSfixed32: []int32{1009, 2009}, |
| 373 | RepeatedSfixed64: []int64{1010, 2010}, |
| 374 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 375 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 376 | RepeatedBool: []bool{true, false}, |
| 377 | RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{ |
| 378 | testpb.TestAllTypes_FOO, |
| 379 | testpb.TestAllTypes_BAR, |
| 380 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 381 | }, &test3pb.TestAllTypes{ |
| 382 | RepeatedInt32: []int32{1001, 2001}, |
| 383 | RepeatedInt64: []int64{1002, 2002}, |
| 384 | RepeatedUint32: []uint32{1003, 2003}, |
| 385 | RepeatedUint64: []uint64{1004, 2004}, |
| 386 | RepeatedSint32: []int32{1005, 2005}, |
| 387 | RepeatedSint64: []int64{1006, 2006}, |
| 388 | RepeatedFixed32: []uint32{1007, 2007}, |
| 389 | RepeatedFixed64: []uint64{1008, 2008}, |
| 390 | RepeatedSfixed32: []int32{1009, 2009}, |
| 391 | RepeatedSfixed64: []int64{1010, 2010}, |
| 392 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 393 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 394 | RepeatedBool: []bool{true, false}, |
| 395 | RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{ |
| 396 | test3pb.TestAllTypes_FOO, |
| 397 | test3pb.TestAllTypes_BAR, |
| 398 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 399 | }, build( |
| 400 | &testpb.TestAllExtensions{}, |
| 401 | extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}), |
| 402 | extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}), |
| 403 | extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}), |
| 404 | extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}), |
| 405 | extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}), |
| 406 | extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}), |
| 407 | extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}), |
| 408 | extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}), |
| 409 | extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}), |
| 410 | extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}), |
| 411 | extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}), |
| 412 | extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}), |
| 413 | extend(testpb.E_RepeatedBoolExtension, []bool{true, false}), |
| 414 | extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{ |
| 415 | testpb.TestAllTypes_FOO, |
| 416 | testpb.TestAllTypes_BAR, |
| 417 | }), |
| 418 | )}, |
| 419 | wire: pack.Message{ |
| 420 | pack.Tag{31, pack.BytesType}, pack.LengthPrefix{ |
| 421 | pack.Varint(1001), pack.Varint(2001), |
| 422 | }, |
| 423 | pack.Tag{32, pack.BytesType}, pack.LengthPrefix{ |
| 424 | pack.Varint(1002), pack.Varint(2002), |
| 425 | }, |
| 426 | pack.Tag{33, pack.BytesType}, pack.LengthPrefix{ |
| 427 | pack.Uvarint(1003), pack.Uvarint(2003), |
| 428 | }, |
| 429 | pack.Tag{34, pack.BytesType}, pack.LengthPrefix{ |
| 430 | pack.Uvarint(1004), pack.Uvarint(2004), |
| 431 | }, |
| 432 | pack.Tag{35, pack.BytesType}, pack.LengthPrefix{ |
| 433 | pack.Svarint(1005), pack.Svarint(2005), |
| 434 | }, |
| 435 | pack.Tag{36, pack.BytesType}, pack.LengthPrefix{ |
| 436 | pack.Svarint(1006), pack.Svarint(2006), |
| 437 | }, |
| 438 | pack.Tag{37, pack.BytesType}, pack.LengthPrefix{ |
| 439 | pack.Uint32(1007), pack.Uint32(2007), |
| 440 | }, |
| 441 | pack.Tag{38, pack.BytesType}, pack.LengthPrefix{ |
| 442 | pack.Uint64(1008), pack.Uint64(2008), |
| 443 | }, |
| 444 | pack.Tag{39, pack.BytesType}, pack.LengthPrefix{ |
| 445 | pack.Int32(1009), pack.Int32(2009), |
| 446 | }, |
| 447 | pack.Tag{40, pack.BytesType}, pack.LengthPrefix{ |
| 448 | pack.Int64(1010), pack.Int64(2010), |
| 449 | }, |
| 450 | pack.Tag{41, pack.BytesType}, pack.LengthPrefix{ |
| 451 | pack.Float32(1011.5), pack.Float32(2011.5), |
| 452 | }, |
| 453 | pack.Tag{42, pack.BytesType}, pack.LengthPrefix{ |
| 454 | pack.Float64(1012.5), pack.Float64(2012.5), |
| 455 | }, |
| 456 | pack.Tag{43, pack.BytesType}, pack.LengthPrefix{ |
| 457 | pack.Bool(true), pack.Bool(false), |
| 458 | }, |
| 459 | pack.Tag{51, pack.BytesType}, pack.LengthPrefix{ |
| 460 | pack.Varint(int(testpb.TestAllTypes_FOO)), |
| 461 | pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 462 | }, |
| 463 | }.Marshal(), |
| 464 | }, |
| 465 | { |
| 466 | desc: "repeated messages", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 467 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 468 | RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{ |
| 469 | {A: scalar.Int32(1)}, |
| 470 | {A: scalar.Int32(2)}, |
| 471 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 472 | }, &test3pb.TestAllTypes{ |
| 473 | RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{ |
| 474 | {A: 1}, |
| 475 | {A: 2}, |
| 476 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 477 | }, build( |
| 478 | &testpb.TestAllExtensions{}, |
| 479 | extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{ |
| 480 | {A: scalar.Int32(1)}, |
| 481 | {A: scalar.Int32(2)}, |
| 482 | }), |
| 483 | )}, |
| 484 | wire: pack.Message{ |
| 485 | pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 486 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 487 | }), |
| 488 | pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 489 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 490 | }), |
| 491 | }.Marshal(), |
| 492 | }, |
| 493 | { |
| 494 | desc: "repeated groups", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 495 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 496 | Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{ |
| 497 | {A: scalar.Int32(1017)}, |
| 498 | {A: scalar.Int32(2017)}, |
| 499 | }, |
| 500 | }, build( |
| 501 | &testpb.TestAllExtensions{}, |
| 502 | extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{ |
| 503 | {A: scalar.Int32(1017)}, |
| 504 | {A: scalar.Int32(2017)}, |
| 505 | }), |
| 506 | )}, |
| 507 | wire: pack.Message{ |
| 508 | pack.Tag{46, pack.StartGroupType}, |
| 509 | pack.Tag{47, pack.VarintType}, pack.Varint(1017), |
| 510 | pack.Tag{46, pack.EndGroupType}, |
| 511 | pack.Tag{46, pack.StartGroupType}, |
| 512 | pack.Tag{47, pack.VarintType}, pack.Varint(2017), |
| 513 | pack.Tag{46, pack.EndGroupType}, |
| 514 | }.Marshal(), |
| 515 | }, |
| 516 | { |
| 517 | desc: "maps", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 518 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 519 | MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156}, |
| 520 | MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157}, |
| 521 | MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158}, |
| 522 | MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159}, |
| 523 | MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160}, |
| 524 | MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161}, |
| 525 | MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162}, |
| 526 | MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163}, |
| 527 | MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164}, |
| 528 | MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165}, |
| 529 | MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5}, |
| 530 | MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5}, |
| 531 | MapBoolBool: map[bool]bool{true: false, false: true}, |
| 532 | MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"}, |
| 533 | MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")}, |
| 534 | MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{ |
| 535 | "71.1.key": {A: scalar.Int32(1171)}, |
| 536 | "71.2.key": {A: scalar.Int32(2171)}, |
| 537 | }, |
| 538 | MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{ |
| 539 | "73.1.key": testpb.TestAllTypes_FOO, |
| 540 | "73.2.key": testpb.TestAllTypes_BAR, |
| 541 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 542 | }, &test3pb.TestAllTypes{ |
| 543 | MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156}, |
| 544 | MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157}, |
| 545 | MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158}, |
| 546 | MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159}, |
| 547 | MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160}, |
| 548 | MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161}, |
| 549 | MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162}, |
| 550 | MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163}, |
| 551 | MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164}, |
| 552 | MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165}, |
| 553 | MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5}, |
| 554 | MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5}, |
| 555 | MapBoolBool: map[bool]bool{true: false, false: true}, |
| 556 | MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"}, |
| 557 | MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")}, |
| 558 | MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{ |
| 559 | "71.1.key": {A: 1171}, |
| 560 | "71.2.key": {A: 2171}, |
| 561 | }, |
| 562 | MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{ |
| 563 | "73.1.key": test3pb.TestAllTypes_FOO, |
| 564 | "73.2.key": test3pb.TestAllTypes_BAR, |
| 565 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 566 | }}, |
| 567 | wire: pack.Message{ |
| 568 | pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 569 | pack.Tag{1, pack.VarintType}, pack.Varint(1056), |
| 570 | pack.Tag{2, pack.VarintType}, pack.Varint(1156), |
| 571 | }), |
| 572 | pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 573 | pack.Tag{1, pack.VarintType}, pack.Varint(2056), |
| 574 | pack.Tag{2, pack.VarintType}, pack.Varint(2156), |
| 575 | }), |
| 576 | pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 577 | pack.Tag{1, pack.VarintType}, pack.Varint(1057), |
| 578 | pack.Tag{2, pack.VarintType}, pack.Varint(1157), |
| 579 | }), |
| 580 | pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 581 | pack.Tag{1, pack.VarintType}, pack.Varint(2057), |
| 582 | pack.Tag{2, pack.VarintType}, pack.Varint(2157), |
| 583 | }), |
| 584 | pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 585 | pack.Tag{1, pack.VarintType}, pack.Varint(1058), |
| 586 | pack.Tag{2, pack.VarintType}, pack.Varint(1158), |
| 587 | }), |
| 588 | pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 589 | pack.Tag{1, pack.VarintType}, pack.Varint(2058), |
| 590 | pack.Tag{2, pack.VarintType}, pack.Varint(2158), |
| 591 | }), |
| 592 | pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 593 | pack.Tag{1, pack.VarintType}, pack.Varint(1059), |
| 594 | pack.Tag{2, pack.VarintType}, pack.Varint(1159), |
| 595 | }), |
| 596 | pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 597 | pack.Tag{1, pack.VarintType}, pack.Varint(2059), |
| 598 | pack.Tag{2, pack.VarintType}, pack.Varint(2159), |
| 599 | }), |
| 600 | pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 601 | pack.Tag{1, pack.VarintType}, pack.Svarint(1060), |
| 602 | pack.Tag{2, pack.VarintType}, pack.Svarint(1160), |
| 603 | }), |
| 604 | pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 605 | pack.Tag{1, pack.VarintType}, pack.Svarint(2060), |
| 606 | pack.Tag{2, pack.VarintType}, pack.Svarint(2160), |
| 607 | }), |
| 608 | pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 609 | pack.Tag{1, pack.VarintType}, pack.Svarint(1061), |
| 610 | pack.Tag{2, pack.VarintType}, pack.Svarint(1161), |
| 611 | }), |
| 612 | pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 613 | pack.Tag{1, pack.VarintType}, pack.Svarint(2061), |
| 614 | pack.Tag{2, pack.VarintType}, pack.Svarint(2161), |
| 615 | }), |
| 616 | pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 617 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062), |
| 618 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162), |
| 619 | }), |
| 620 | pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 621 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062), |
| 622 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162), |
| 623 | }), |
| 624 | pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 625 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063), |
| 626 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163), |
| 627 | }), |
| 628 | pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 629 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063), |
| 630 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163), |
| 631 | }), |
| 632 | pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 633 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064), |
| 634 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164), |
| 635 | }), |
| 636 | pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 637 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064), |
| 638 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164), |
| 639 | }), |
| 640 | pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 641 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065), |
| 642 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165), |
| 643 | }), |
| 644 | pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 645 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065), |
| 646 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165), |
| 647 | }), |
| 648 | pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 649 | pack.Tag{1, pack.VarintType}, pack.Varint(1066), |
| 650 | pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5), |
| 651 | }), |
| 652 | pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 653 | pack.Tag{1, pack.VarintType}, pack.Varint(2066), |
| 654 | pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5), |
| 655 | }), |
| 656 | pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 657 | pack.Tag{1, pack.VarintType}, pack.Varint(1067), |
| 658 | pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5), |
| 659 | }), |
| 660 | pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 661 | pack.Tag{1, pack.VarintType}, pack.Varint(2067), |
| 662 | pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5), |
| 663 | }), |
| 664 | pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 665 | pack.Tag{1, pack.VarintType}, pack.Bool(true), |
| 666 | pack.Tag{2, pack.VarintType}, pack.Bool(false), |
| 667 | }), |
| 668 | pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 669 | pack.Tag{1, pack.VarintType}, pack.Bool(false), |
| 670 | pack.Tag{2, pack.VarintType}, pack.Bool(true), |
| 671 | }), |
| 672 | pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 673 | pack.Tag{1, pack.BytesType}, pack.String("69.1.key"), |
| 674 | pack.Tag{2, pack.BytesType}, pack.String("69.1.val"), |
| 675 | }), |
| 676 | pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 677 | pack.Tag{1, pack.BytesType}, pack.String("69.2.key"), |
| 678 | pack.Tag{2, pack.BytesType}, pack.String("69.2.val"), |
| 679 | }), |
| 680 | pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 681 | pack.Tag{1, pack.BytesType}, pack.String("70.1.key"), |
| 682 | pack.Tag{2, pack.BytesType}, pack.String("70.1.val"), |
| 683 | }), |
| 684 | pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 685 | pack.Tag{1, pack.BytesType}, pack.String("70.2.key"), |
| 686 | pack.Tag{2, pack.BytesType}, pack.String("70.2.val"), |
| 687 | }), |
| 688 | pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 689 | pack.Tag{1, pack.BytesType}, pack.String("71.1.key"), |
| 690 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 691 | pack.Tag{1, pack.VarintType}, pack.Varint(1171), |
| 692 | }), |
| 693 | }), |
| 694 | pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 695 | pack.Tag{1, pack.BytesType}, pack.String("71.2.key"), |
| 696 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 697 | pack.Tag{1, pack.VarintType}, pack.Varint(2171), |
| 698 | }), |
| 699 | }), |
| 700 | pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 701 | pack.Tag{1, pack.BytesType}, pack.String("73.1.key"), |
| 702 | pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)), |
| 703 | }), |
| 704 | pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 705 | pack.Tag{1, pack.BytesType}, pack.String("73.2.key"), |
| 706 | pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 707 | }), |
| 708 | }.Marshal(), |
| 709 | }, |
| 710 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 711 | desc: "oneof (uint32)", |
| 712 | decodeTo: []proto.Message{ |
| 713 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}}, |
| 714 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}}, |
| 715 | }, |
| 716 | wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 717 | }, |
| 718 | { |
| 719 | desc: "oneof (message)", |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 720 | decodeTo: []proto.Message{ |
| 721 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
| 722 | &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)}, |
| 723 | }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{ |
| 724 | &test3pb.TestAllTypes_NestedMessage{A: 1112}, |
| 725 | }}, |
| 726 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 727 | wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 728 | pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)}, |
| 729 | })}.Marshal(), |
| 730 | }, |
| 731 | { |
| 732 | desc: "oneof (overridden message)", |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 733 | decodeTo: []proto.Message{ |
| 734 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
| 735 | &testpb.TestAllTypes_NestedMessage{ |
| 736 | Corecursive: &testpb.TestAllTypes{ |
| 737 | OptionalInt32: scalar.Int32(43), |
| 738 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 739 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 740 | }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{ |
| 741 | &test3pb.TestAllTypes_NestedMessage{ |
| 742 | Corecursive: &test3pb.TestAllTypes{ |
| 743 | OptionalInt32: 43, |
| 744 | }, |
| 745 | }, |
| 746 | }}}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 747 | wire: pack.Message{ |
| 748 | pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 749 | pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)}, |
| 750 | }), |
| 751 | pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 752 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 753 | pack.Tag{1, pack.VarintType}, pack.Varint(43), |
| 754 | }), |
| 755 | }), |
| 756 | }.Marshal(), |
| 757 | }, |
| 758 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 759 | desc: "oneof (string)", |
| 760 | decodeTo: []proto.Message{ |
| 761 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}}, |
| 762 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}}, |
| 763 | }, |
| 764 | wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 765 | }, |
| 766 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 767 | desc: "oneof (bytes)", |
| 768 | decodeTo: []proto.Message{ |
| 769 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}}, |
| 770 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}}, |
| 771 | }, |
| 772 | wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 773 | }, |
| 774 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 775 | desc: "oneof (bool)", |
| 776 | decodeTo: []proto.Message{ |
| 777 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}}, |
| 778 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}}, |
| 779 | }, |
| 780 | wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 781 | }, |
| 782 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 783 | desc: "oneof (uint64)", |
| 784 | decodeTo: []proto.Message{ |
| 785 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}}, |
| 786 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}}, |
| 787 | }, |
| 788 | wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 789 | }, |
| 790 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 791 | desc: "oneof (float)", |
| 792 | decodeTo: []proto.Message{ |
| 793 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}}, |
| 794 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}}, |
| 795 | }, |
| 796 | wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 797 | }, |
| 798 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 799 | desc: "oneof (double)", |
| 800 | decodeTo: []proto.Message{ |
| 801 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}}, |
| 802 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}}, |
| 803 | }, |
| 804 | wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 805 | }, |
| 806 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 807 | desc: "oneof (enum)", |
| 808 | decodeTo: []proto.Message{ |
| 809 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}}, |
| 810 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}}, |
| 811 | }, |
| 812 | wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 813 | }, |
| 814 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 815 | desc: "oneof (overridden value)", |
| 816 | decodeTo: []proto.Message{ |
| 817 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}}, |
| 818 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}}, |
| 819 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 820 | wire: pack.Message{ |
| 821 | pack.Tag{111, pack.VarintType}, pack.Varint(1), |
| 822 | pack.Tag{116, pack.VarintType}, pack.Varint(2), |
| 823 | }.Marshal(), |
| 824 | }, |
| 825 | // TODO: More unknown field tests for ordering, repeated fields, etc. |
| 826 | // |
| 827 | // It is currently impossible to produce results that the v1 Equal |
| 828 | // considers equivalent to those of the v1 decoder. Figure out if |
| 829 | // that's a problem or not. |
| 830 | { |
| 831 | desc: "unknown fields", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 832 | decodeTo: []proto.Message{build( |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 833 | &testpb.TestAllTypes{}, |
| 834 | unknown(100000, pack.Message{ |
| 835 | pack.Tag{100000, pack.VarintType}, pack.Varint(1), |
| 836 | }.Marshal()), |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 837 | ), build( |
| 838 | &test3pb.TestAllTypes{}, |
| 839 | unknown(100000, pack.Message{ |
| 840 | pack.Tag{100000, pack.VarintType}, pack.Varint(1), |
| 841 | }.Marshal()), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 842 | )}, |
| 843 | wire: pack.Message{ |
| 844 | pack.Tag{100000, pack.VarintType}, pack.Varint(1), |
| 845 | }.Marshal(), |
| 846 | }, |
| 847 | { |
| 848 | desc: "field type mismatch", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 849 | decodeTo: []proto.Message{build( |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 850 | &testpb.TestAllTypes{}, |
| 851 | unknown(1, pack.Message{ |
| 852 | pack.Tag{1, pack.BytesType}, pack.String("string"), |
| 853 | }.Marshal()), |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 854 | ), build( |
| 855 | &test3pb.TestAllTypes{}, |
| 856 | unknown(1, pack.Message{ |
| 857 | pack.Tag{1, pack.BytesType}, pack.String("string"), |
| 858 | }.Marshal()), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 859 | )}, |
| 860 | wire: pack.Message{ |
| 861 | pack.Tag{1, pack.BytesType}, pack.String("string"), |
| 862 | }.Marshal(), |
| 863 | }, |
| 864 | { |
| 865 | desc: "map field element mismatch", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 866 | decodeTo: []proto.Message{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 867 | &testpb.TestAllTypes{ |
| 868 | MapInt32Int32: map[int32]int32{1: 0}, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame^] | 869 | }, &test3pb.TestAllTypes{ |
| 870 | MapInt32Int32: map[int32]int32{1: 0}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 871 | }, |
| 872 | }, |
| 873 | wire: pack.Message{ |
| 874 | pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 875 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 876 | pack.Tag{2, pack.BytesType}, pack.String("string"), |
| 877 | }), |
| 878 | }.Marshal(), |
| 879 | }, |
| 880 | } |
| 881 | |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 882 | func build(m proto.Message, opts ...buildOpt) proto.Message { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 883 | for _, opt := range opts { |
| 884 | opt(m) |
| 885 | } |
| 886 | return m |
| 887 | } |
| 888 | |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 889 | type buildOpt func(proto.Message) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 890 | |
| 891 | func unknown(num pref.FieldNumber, raw pref.RawFields) buildOpt { |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 892 | return func(m proto.Message) { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 893 | m.ProtoReflect().UnknownFields().Set(num, raw) |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt { |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 898 | return func(m proto.Message) { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 899 | if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil { |
| 900 | panic(err) |
| 901 | } |
| 902 | } |
| 903 | } |