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