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 | |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/encoding/prototext" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 13 | "google.golang.org/protobuf/internal/encoding/pack" |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 14 | "google.golang.org/protobuf/internal/filedesc" |
| 15 | "google.golang.org/protobuf/internal/flags" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 16 | "google.golang.org/protobuf/proto" |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 17 | "google.golang.org/protobuf/reflect/protodesc" |
| 18 | "google.golang.org/protobuf/reflect/protoreflect" |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 19 | "google.golang.org/protobuf/reflect/prototype" |
Joe Tsai | 74615a3 | 2019-07-14 18:51:46 -0700 | [diff] [blame] | 20 | "google.golang.org/protobuf/runtime/protoiface" |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 21 | "google.golang.org/protobuf/runtime/protoimpl" |
Joe Tsai | 1905843 | 2019-02-27 21:46:29 -0800 | [diff] [blame] | 22 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 23 | legacypb "google.golang.org/protobuf/internal/testprotos/legacy" |
| 24 | legacy1pb "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 25 | testpb "google.golang.org/protobuf/internal/testprotos/test" |
| 26 | test3pb "google.golang.org/protobuf/internal/testprotos/test3" |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 27 | "google.golang.org/protobuf/types/descriptorpb" |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | type testProto struct { |
Joe Tsai | 09cef32 | 2019-07-11 22:13:49 -0700 | [diff] [blame] | 31 | desc string |
| 32 | decodeTo []proto.Message |
| 33 | wire []byte |
| 34 | partial bool |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | func TestDecode(t *testing.T) { |
| 38 | for _, test := range testProtos { |
| 39 | for _, want := range test.decodeTo { |
| 40 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 41 | opts := proto.UnmarshalOptions{ |
| 42 | AllowPartial: test.partial, |
| 43 | } |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 44 | wire := append(([]byte)(nil), test.wire...) |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 45 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 46 | if err := opts.Unmarshal(wire, got); err != nil { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 47 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want)) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 48 | return |
| 49 | } |
| 50 | |
| 51 | // Aliasing check: Modifying the original wire bytes shouldn't |
| 52 | // affect the unmarshaled message. |
| 53 | for i := range wire { |
| 54 | wire[i] = 0 |
| 55 | } |
Joe Tsai | db38ddd | 2019-05-07 15:14:40 -0700 | [diff] [blame] | 56 | if !proto.Equal(got, want) { |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 57 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want)) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 58 | } |
| 59 | }) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 64 | func TestDecodeRequiredFieldChecks(t *testing.T) { |
| 65 | for _, test := range testProtos { |
| 66 | if !test.partial { |
| 67 | continue |
| 68 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 69 | for _, m := range test.decodeTo { |
| 70 | t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) { |
| 71 | got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message) |
| 72 | if err := proto.Unmarshal(test.wire, got); err == nil { |
| 73 | t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got)) |
| 74 | } |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 80 | func TestDecodeInvalidUTF8(t *testing.T) { |
| 81 | for _, test := range invalidUTF8TestProtos { |
| 82 | for _, want := range test.decodeTo { |
| 83 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
| 84 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
| 85 | err := proto.Unmarshal(test.wire, got) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 86 | if err == nil { |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 87 | t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want)) |
| 88 | } |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 89 | }) |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 94 | func TestDecodeNoEnforceUTF8(t *testing.T) { |
| 95 | for _, test := range noEnforceUTF8TestProtos { |
| 96 | for _, want := range test.decodeTo { |
| 97 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
| 98 | got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message) |
| 99 | err := proto.Unmarshal(test.wire, got) |
| 100 | switch { |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 101 | case flags.ProtoLegacy && err != nil: |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 102 | t.Errorf("Unmarshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want)) |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 103 | case !flags.ProtoLegacy && err == nil: |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 104 | t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want)) |
| 105 | } |
| 106 | }) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Damien Neil | 8003f08 | 2019-08-02 15:13:00 -0700 | [diff] [blame] | 111 | func TestDecodeZeroLengthBytes(t *testing.T) { |
| 112 | // Verify that proto3 bytes fields don't give the mistaken |
| 113 | // impression that they preserve presence. |
| 114 | wire := pack.Message{ |
| 115 | pack.Tag{15, pack.BytesType}, pack.Bytes(nil), |
| 116 | }.Marshal() |
| 117 | m := &test3pb.TestAllTypes{} |
| 118 | if err := proto.Unmarshal(wire, m); err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | if m.OptionalBytes != nil { |
| 122 | t.Errorf("unmarshal zero-length proto3 bytes field: got %v, want nil", m.OptionalBytes) |
| 123 | } |
| 124 | } |
| 125 | |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 126 | var testProtos = []testProto{ |
| 127 | { |
| 128 | desc: "basic scalar types", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 129 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 130 | OptionalInt32: proto.Int32(1001), |
| 131 | OptionalInt64: proto.Int64(1002), |
| 132 | OptionalUint32: proto.Uint32(1003), |
| 133 | OptionalUint64: proto.Uint64(1004), |
| 134 | OptionalSint32: proto.Int32(1005), |
| 135 | OptionalSint64: proto.Int64(1006), |
| 136 | OptionalFixed32: proto.Uint32(1007), |
| 137 | OptionalFixed64: proto.Uint64(1008), |
| 138 | OptionalSfixed32: proto.Int32(1009), |
| 139 | OptionalSfixed64: proto.Int64(1010), |
| 140 | OptionalFloat: proto.Float32(1011.5), |
| 141 | OptionalDouble: proto.Float64(1012.5), |
| 142 | OptionalBool: proto.Bool(true), |
| 143 | OptionalString: proto.String("string"), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 144 | OptionalBytes: []byte("bytes"), |
| 145 | OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(), |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 146 | }, &test3pb.TestAllTypes{ |
| 147 | OptionalInt32: 1001, |
| 148 | OptionalInt64: 1002, |
| 149 | OptionalUint32: 1003, |
| 150 | OptionalUint64: 1004, |
| 151 | OptionalSint32: 1005, |
| 152 | OptionalSint64: 1006, |
| 153 | OptionalFixed32: 1007, |
| 154 | OptionalFixed64: 1008, |
| 155 | OptionalSfixed32: 1009, |
| 156 | OptionalSfixed64: 1010, |
| 157 | OptionalFloat: 1011.5, |
| 158 | OptionalDouble: 1012.5, |
| 159 | OptionalBool: true, |
| 160 | OptionalString: "string", |
| 161 | OptionalBytes: []byte("bytes"), |
| 162 | OptionalNestedEnum: test3pb.TestAllTypes_BAR, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 163 | }, build( |
| 164 | &testpb.TestAllExtensions{}, |
Joe Tsai | 8d30bbe | 2019-05-16 15:53:25 -0700 | [diff] [blame] | 165 | extend(testpb.E_OptionalInt32Extension, int32(1001)), |
| 166 | extend(testpb.E_OptionalInt64Extension, int64(1002)), |
| 167 | extend(testpb.E_OptionalUint32Extension, uint32(1003)), |
| 168 | extend(testpb.E_OptionalUint64Extension, uint64(1004)), |
| 169 | extend(testpb.E_OptionalSint32Extension, int32(1005)), |
| 170 | extend(testpb.E_OptionalSint64Extension, int64(1006)), |
| 171 | extend(testpb.E_OptionalFixed32Extension, uint32(1007)), |
| 172 | extend(testpb.E_OptionalFixed64Extension, uint64(1008)), |
| 173 | extend(testpb.E_OptionalSfixed32Extension, int32(1009)), |
| 174 | extend(testpb.E_OptionalSfixed64Extension, int64(1010)), |
| 175 | extend(testpb.E_OptionalFloatExtension, float32(1011.5)), |
| 176 | extend(testpb.E_OptionalDoubleExtension, float64(1012.5)), |
| 177 | extend(testpb.E_OptionalBoolExtension, bool(true)), |
| 178 | extend(testpb.E_OptionalStringExtension, string("string")), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 179 | extend(testpb.E_OptionalBytesExtension, []byte("bytes")), |
Joe Tsai | 8d30bbe | 2019-05-16 15:53:25 -0700 | [diff] [blame] | 180 | extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 181 | )}, |
| 182 | wire: pack.Message{ |
| 183 | pack.Tag{1, pack.VarintType}, pack.Varint(1001), |
| 184 | pack.Tag{2, pack.VarintType}, pack.Varint(1002), |
| 185 | pack.Tag{3, pack.VarintType}, pack.Uvarint(1003), |
| 186 | pack.Tag{4, pack.VarintType}, pack.Uvarint(1004), |
| 187 | pack.Tag{5, pack.VarintType}, pack.Svarint(1005), |
| 188 | pack.Tag{6, pack.VarintType}, pack.Svarint(1006), |
| 189 | pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007), |
| 190 | pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008), |
| 191 | pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009), |
| 192 | pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010), |
| 193 | pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5), |
| 194 | pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5), |
| 195 | pack.Tag{13, pack.VarintType}, pack.Bool(true), |
| 196 | pack.Tag{14, pack.BytesType}, pack.String("string"), |
| 197 | pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")), |
| 198 | pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 199 | }.Marshal(), |
| 200 | }, |
| 201 | { |
Damien Neil | 8003f08 | 2019-08-02 15:13:00 -0700 | [diff] [blame] | 202 | desc: "zero values", |
| 203 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
| 204 | OptionalInt32: proto.Int32(0), |
| 205 | OptionalInt64: proto.Int64(0), |
| 206 | OptionalUint32: proto.Uint32(0), |
| 207 | OptionalUint64: proto.Uint64(0), |
| 208 | OptionalSint32: proto.Int32(0), |
| 209 | OptionalSint64: proto.Int64(0), |
| 210 | OptionalFixed32: proto.Uint32(0), |
| 211 | OptionalFixed64: proto.Uint64(0), |
| 212 | OptionalSfixed32: proto.Int32(0), |
| 213 | OptionalSfixed64: proto.Int64(0), |
| 214 | OptionalFloat: proto.Float32(0), |
| 215 | OptionalDouble: proto.Float64(0), |
| 216 | OptionalBool: proto.Bool(false), |
| 217 | OptionalString: proto.String(""), |
| 218 | OptionalBytes: []byte{}, |
| 219 | }, &test3pb.TestAllTypes{}, build( |
| 220 | &testpb.TestAllExtensions{}, |
| 221 | extend(testpb.E_OptionalInt32Extension, int32(0)), |
| 222 | extend(testpb.E_OptionalInt64Extension, int64(0)), |
| 223 | extend(testpb.E_OptionalUint32Extension, uint32(0)), |
| 224 | extend(testpb.E_OptionalUint64Extension, uint64(0)), |
| 225 | extend(testpb.E_OptionalSint32Extension, int32(0)), |
| 226 | extend(testpb.E_OptionalSint64Extension, int64(0)), |
| 227 | extend(testpb.E_OptionalFixed32Extension, uint32(0)), |
| 228 | extend(testpb.E_OptionalFixed64Extension, uint64(0)), |
| 229 | extend(testpb.E_OptionalSfixed32Extension, int32(0)), |
| 230 | extend(testpb.E_OptionalSfixed64Extension, int64(0)), |
| 231 | extend(testpb.E_OptionalFloatExtension, float32(0)), |
| 232 | extend(testpb.E_OptionalDoubleExtension, float64(0)), |
| 233 | extend(testpb.E_OptionalBoolExtension, bool(false)), |
| 234 | extend(testpb.E_OptionalStringExtension, string("")), |
| 235 | extend(testpb.E_OptionalBytesExtension, []byte{}), |
| 236 | )}, |
| 237 | wire: pack.Message{ |
| 238 | pack.Tag{1, pack.VarintType}, pack.Varint(0), |
| 239 | pack.Tag{2, pack.VarintType}, pack.Varint(0), |
| 240 | pack.Tag{3, pack.VarintType}, pack.Uvarint(0), |
| 241 | pack.Tag{4, pack.VarintType}, pack.Uvarint(0), |
| 242 | pack.Tag{5, pack.VarintType}, pack.Svarint(0), |
| 243 | pack.Tag{6, pack.VarintType}, pack.Svarint(0), |
| 244 | pack.Tag{7, pack.Fixed32Type}, pack.Uint32(0), |
| 245 | pack.Tag{8, pack.Fixed64Type}, pack.Uint64(0), |
| 246 | pack.Tag{9, pack.Fixed32Type}, pack.Int32(0), |
| 247 | pack.Tag{10, pack.Fixed64Type}, pack.Int64(0), |
| 248 | pack.Tag{11, pack.Fixed32Type}, pack.Float32(0), |
| 249 | pack.Tag{12, pack.Fixed64Type}, pack.Float64(0), |
| 250 | pack.Tag{13, pack.VarintType}, pack.Bool(false), |
| 251 | pack.Tag{14, pack.BytesType}, pack.String(""), |
| 252 | pack.Tag{15, pack.BytesType}, pack.Bytes(nil), |
| 253 | }.Marshal(), |
| 254 | }, |
| 255 | { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 256 | desc: "groups", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 257 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 258 | Optionalgroup: &testpb.TestAllTypes_OptionalGroup{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 259 | A: proto.Int32(1017), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 260 | }, |
| 261 | }, build( |
| 262 | &testpb.TestAllExtensions{}, |
| 263 | extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 264 | A: proto.Int32(1017), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 265 | }), |
| 266 | )}, |
| 267 | wire: pack.Message{ |
| 268 | pack.Tag{16, pack.StartGroupType}, |
| 269 | pack.Tag{17, pack.VarintType}, pack.Varint(1017), |
| 270 | pack.Tag{16, pack.EndGroupType}, |
| 271 | }.Marshal(), |
| 272 | }, |
| 273 | { |
| 274 | desc: "groups (field overridden)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 275 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 276 | Optionalgroup: &testpb.TestAllTypes_OptionalGroup{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 277 | A: proto.Int32(2), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 278 | }, |
| 279 | }, build( |
| 280 | &testpb.TestAllExtensions{}, |
| 281 | extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 282 | A: proto.Int32(2), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 283 | }), |
| 284 | )}, |
| 285 | wire: pack.Message{ |
| 286 | pack.Tag{16, pack.StartGroupType}, |
| 287 | pack.Tag{17, pack.VarintType}, pack.Varint(1), |
| 288 | pack.Tag{16, pack.EndGroupType}, |
| 289 | pack.Tag{16, pack.StartGroupType}, |
| 290 | pack.Tag{17, pack.VarintType}, pack.Varint(2), |
| 291 | pack.Tag{16, pack.EndGroupType}, |
| 292 | }.Marshal(), |
| 293 | }, |
| 294 | { |
| 295 | desc: "messages", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 296 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 297 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 298 | A: proto.Int32(42), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 299 | Corecursive: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 300 | OptionalInt32: proto.Int32(43), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 301 | }, |
| 302 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 303 | }, &test3pb.TestAllTypes{ |
| 304 | OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{ |
| 305 | A: 42, |
| 306 | Corecursive: &test3pb.TestAllTypes{ |
| 307 | OptionalInt32: 43, |
| 308 | }, |
| 309 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 310 | }, build( |
| 311 | &testpb.TestAllExtensions{}, |
| 312 | extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 313 | A: proto.Int32(42), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 314 | Corecursive: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 315 | OptionalInt32: proto.Int32(43), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 316 | }, |
| 317 | }), |
| 318 | )}, |
| 319 | wire: pack.Message{ |
| 320 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 321 | pack.Tag{1, pack.VarintType}, pack.Varint(42), |
| 322 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 323 | pack.Tag{1, pack.VarintType}, pack.Varint(43), |
| 324 | }), |
| 325 | }), |
| 326 | }.Marshal(), |
| 327 | }, |
| 328 | { |
| 329 | desc: "messages (split across multiple tags)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 330 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 331 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 332 | A: proto.Int32(42), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 333 | Corecursive: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 334 | OptionalInt32: proto.Int32(43), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 335 | }, |
| 336 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 337 | }, &test3pb.TestAllTypes{ |
| 338 | OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{ |
| 339 | A: 42, |
| 340 | Corecursive: &test3pb.TestAllTypes{ |
| 341 | OptionalInt32: 43, |
| 342 | }, |
| 343 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 344 | }, build( |
| 345 | &testpb.TestAllExtensions{}, |
| 346 | extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 347 | A: proto.Int32(42), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 348 | Corecursive: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 349 | OptionalInt32: proto.Int32(43), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 350 | }, |
| 351 | }), |
| 352 | )}, |
| 353 | wire: pack.Message{ |
| 354 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 355 | pack.Tag{1, pack.VarintType}, pack.Varint(42), |
| 356 | }), |
| 357 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 358 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 359 | pack.Tag{1, pack.VarintType}, pack.Varint(43), |
| 360 | }), |
| 361 | }), |
| 362 | }.Marshal(), |
| 363 | }, |
| 364 | { |
| 365 | desc: "messages (field overridden)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 366 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 367 | OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 368 | A: proto.Int32(2), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 369 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 370 | }, &test3pb.TestAllTypes{ |
| 371 | OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{ |
| 372 | A: 2, |
| 373 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 374 | }, build( |
| 375 | &testpb.TestAllExtensions{}, |
| 376 | extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 377 | A: proto.Int32(2), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 378 | }), |
| 379 | )}, |
| 380 | wire: pack.Message{ |
| 381 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 382 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 383 | }), |
| 384 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 385 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 386 | }), |
| 387 | }.Marshal(), |
| 388 | }, |
| 389 | { |
| 390 | desc: "basic repeated types", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 391 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 392 | RepeatedInt32: []int32{1001, 2001}, |
| 393 | RepeatedInt64: []int64{1002, 2002}, |
| 394 | RepeatedUint32: []uint32{1003, 2003}, |
| 395 | RepeatedUint64: []uint64{1004, 2004}, |
| 396 | RepeatedSint32: []int32{1005, 2005}, |
| 397 | RepeatedSint64: []int64{1006, 2006}, |
| 398 | RepeatedFixed32: []uint32{1007, 2007}, |
| 399 | RepeatedFixed64: []uint64{1008, 2008}, |
| 400 | RepeatedSfixed32: []int32{1009, 2009}, |
| 401 | RepeatedSfixed64: []int64{1010, 2010}, |
| 402 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 403 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 404 | RepeatedBool: []bool{true, false}, |
| 405 | RepeatedString: []string{"foo", "bar"}, |
| 406 | RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")}, |
| 407 | RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{ |
| 408 | testpb.TestAllTypes_FOO, |
| 409 | testpb.TestAllTypes_BAR, |
| 410 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 411 | }, &test3pb.TestAllTypes{ |
| 412 | RepeatedInt32: []int32{1001, 2001}, |
| 413 | RepeatedInt64: []int64{1002, 2002}, |
| 414 | RepeatedUint32: []uint32{1003, 2003}, |
| 415 | RepeatedUint64: []uint64{1004, 2004}, |
| 416 | RepeatedSint32: []int32{1005, 2005}, |
| 417 | RepeatedSint64: []int64{1006, 2006}, |
| 418 | RepeatedFixed32: []uint32{1007, 2007}, |
| 419 | RepeatedFixed64: []uint64{1008, 2008}, |
| 420 | RepeatedSfixed32: []int32{1009, 2009}, |
| 421 | RepeatedSfixed64: []int64{1010, 2010}, |
| 422 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 423 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 424 | RepeatedBool: []bool{true, false}, |
| 425 | RepeatedString: []string{"foo", "bar"}, |
| 426 | RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")}, |
| 427 | RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{ |
| 428 | test3pb.TestAllTypes_FOO, |
| 429 | test3pb.TestAllTypes_BAR, |
| 430 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 431 | }, build( |
| 432 | &testpb.TestAllExtensions{}, |
| 433 | extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}), |
| 434 | extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}), |
| 435 | extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}), |
| 436 | extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}), |
| 437 | extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}), |
| 438 | extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}), |
| 439 | extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}), |
| 440 | extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}), |
| 441 | extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}), |
| 442 | extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}), |
| 443 | extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}), |
| 444 | extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}), |
| 445 | extend(testpb.E_RepeatedBoolExtension, []bool{true, false}), |
| 446 | extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}), |
| 447 | extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}), |
| 448 | extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{ |
| 449 | testpb.TestAllTypes_FOO, |
| 450 | testpb.TestAllTypes_BAR, |
| 451 | }), |
| 452 | )}, |
| 453 | wire: pack.Message{ |
| 454 | pack.Tag{31, pack.VarintType}, pack.Varint(1001), |
| 455 | pack.Tag{31, pack.VarintType}, pack.Varint(2001), |
| 456 | pack.Tag{32, pack.VarintType}, pack.Varint(1002), |
| 457 | pack.Tag{32, pack.VarintType}, pack.Varint(2002), |
| 458 | pack.Tag{33, pack.VarintType}, pack.Uvarint(1003), |
| 459 | pack.Tag{33, pack.VarintType}, pack.Uvarint(2003), |
| 460 | pack.Tag{34, pack.VarintType}, pack.Uvarint(1004), |
| 461 | pack.Tag{34, pack.VarintType}, pack.Uvarint(2004), |
| 462 | pack.Tag{35, pack.VarintType}, pack.Svarint(1005), |
| 463 | pack.Tag{35, pack.VarintType}, pack.Svarint(2005), |
| 464 | pack.Tag{36, pack.VarintType}, pack.Svarint(1006), |
| 465 | pack.Tag{36, pack.VarintType}, pack.Svarint(2006), |
| 466 | pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007), |
| 467 | pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007), |
| 468 | pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008), |
| 469 | pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008), |
| 470 | pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009), |
| 471 | pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009), |
| 472 | pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010), |
| 473 | pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010), |
| 474 | pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5), |
| 475 | pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5), |
| 476 | pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5), |
| 477 | pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5), |
| 478 | pack.Tag{43, pack.VarintType}, pack.Bool(true), |
| 479 | pack.Tag{43, pack.VarintType}, pack.Bool(false), |
| 480 | pack.Tag{44, pack.BytesType}, pack.String("foo"), |
| 481 | pack.Tag{44, pack.BytesType}, pack.String("bar"), |
| 482 | pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")), |
| 483 | pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")), |
| 484 | pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)), |
| 485 | pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 486 | }.Marshal(), |
| 487 | }, |
| 488 | { |
| 489 | desc: "basic repeated types (packed encoding)", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 490 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 491 | RepeatedInt32: []int32{1001, 2001}, |
| 492 | RepeatedInt64: []int64{1002, 2002}, |
| 493 | RepeatedUint32: []uint32{1003, 2003}, |
| 494 | RepeatedUint64: []uint64{1004, 2004}, |
| 495 | RepeatedSint32: []int32{1005, 2005}, |
| 496 | RepeatedSint64: []int64{1006, 2006}, |
| 497 | RepeatedFixed32: []uint32{1007, 2007}, |
| 498 | RepeatedFixed64: []uint64{1008, 2008}, |
| 499 | RepeatedSfixed32: []int32{1009, 2009}, |
| 500 | RepeatedSfixed64: []int64{1010, 2010}, |
| 501 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 502 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 503 | RepeatedBool: []bool{true, false}, |
| 504 | RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{ |
| 505 | testpb.TestAllTypes_FOO, |
| 506 | testpb.TestAllTypes_BAR, |
| 507 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 508 | }, &test3pb.TestAllTypes{ |
| 509 | RepeatedInt32: []int32{1001, 2001}, |
| 510 | RepeatedInt64: []int64{1002, 2002}, |
| 511 | RepeatedUint32: []uint32{1003, 2003}, |
| 512 | RepeatedUint64: []uint64{1004, 2004}, |
| 513 | RepeatedSint32: []int32{1005, 2005}, |
| 514 | RepeatedSint64: []int64{1006, 2006}, |
| 515 | RepeatedFixed32: []uint32{1007, 2007}, |
| 516 | RepeatedFixed64: []uint64{1008, 2008}, |
| 517 | RepeatedSfixed32: []int32{1009, 2009}, |
| 518 | RepeatedSfixed64: []int64{1010, 2010}, |
| 519 | RepeatedFloat: []float32{1011.5, 2011.5}, |
| 520 | RepeatedDouble: []float64{1012.5, 2012.5}, |
| 521 | RepeatedBool: []bool{true, false}, |
| 522 | RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{ |
| 523 | test3pb.TestAllTypes_FOO, |
| 524 | test3pb.TestAllTypes_BAR, |
| 525 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 526 | }, build( |
| 527 | &testpb.TestAllExtensions{}, |
| 528 | extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}), |
| 529 | extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}), |
| 530 | extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}), |
| 531 | extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}), |
| 532 | extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}), |
| 533 | extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}), |
| 534 | extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}), |
| 535 | extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}), |
| 536 | extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}), |
| 537 | extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}), |
| 538 | extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}), |
| 539 | extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}), |
| 540 | extend(testpb.E_RepeatedBoolExtension, []bool{true, false}), |
| 541 | extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{ |
| 542 | testpb.TestAllTypes_FOO, |
| 543 | testpb.TestAllTypes_BAR, |
| 544 | }), |
| 545 | )}, |
| 546 | wire: pack.Message{ |
| 547 | pack.Tag{31, pack.BytesType}, pack.LengthPrefix{ |
| 548 | pack.Varint(1001), pack.Varint(2001), |
| 549 | }, |
| 550 | pack.Tag{32, pack.BytesType}, pack.LengthPrefix{ |
| 551 | pack.Varint(1002), pack.Varint(2002), |
| 552 | }, |
| 553 | pack.Tag{33, pack.BytesType}, pack.LengthPrefix{ |
| 554 | pack.Uvarint(1003), pack.Uvarint(2003), |
| 555 | }, |
| 556 | pack.Tag{34, pack.BytesType}, pack.LengthPrefix{ |
| 557 | pack.Uvarint(1004), pack.Uvarint(2004), |
| 558 | }, |
| 559 | pack.Tag{35, pack.BytesType}, pack.LengthPrefix{ |
| 560 | pack.Svarint(1005), pack.Svarint(2005), |
| 561 | }, |
| 562 | pack.Tag{36, pack.BytesType}, pack.LengthPrefix{ |
| 563 | pack.Svarint(1006), pack.Svarint(2006), |
| 564 | }, |
| 565 | pack.Tag{37, pack.BytesType}, pack.LengthPrefix{ |
| 566 | pack.Uint32(1007), pack.Uint32(2007), |
| 567 | }, |
| 568 | pack.Tag{38, pack.BytesType}, pack.LengthPrefix{ |
| 569 | pack.Uint64(1008), pack.Uint64(2008), |
| 570 | }, |
| 571 | pack.Tag{39, pack.BytesType}, pack.LengthPrefix{ |
| 572 | pack.Int32(1009), pack.Int32(2009), |
| 573 | }, |
| 574 | pack.Tag{40, pack.BytesType}, pack.LengthPrefix{ |
| 575 | pack.Int64(1010), pack.Int64(2010), |
| 576 | }, |
| 577 | pack.Tag{41, pack.BytesType}, pack.LengthPrefix{ |
| 578 | pack.Float32(1011.5), pack.Float32(2011.5), |
| 579 | }, |
| 580 | pack.Tag{42, pack.BytesType}, pack.LengthPrefix{ |
| 581 | pack.Float64(1012.5), pack.Float64(2012.5), |
| 582 | }, |
| 583 | pack.Tag{43, pack.BytesType}, pack.LengthPrefix{ |
| 584 | pack.Bool(true), pack.Bool(false), |
| 585 | }, |
| 586 | pack.Tag{51, pack.BytesType}, pack.LengthPrefix{ |
| 587 | pack.Varint(int(testpb.TestAllTypes_FOO)), |
| 588 | pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 589 | }, |
| 590 | }.Marshal(), |
| 591 | }, |
| 592 | { |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 593 | desc: "packed repeated types", |
| 594 | decodeTo: []proto.Message{&testpb.TestPackedTypes{ |
| 595 | PackedInt32: []int32{1001, 2001}, |
| 596 | PackedInt64: []int64{1002, 2002}, |
| 597 | PackedUint32: []uint32{1003, 2003}, |
| 598 | PackedUint64: []uint64{1004, 2004}, |
| 599 | PackedSint32: []int32{1005, 2005}, |
| 600 | PackedSint64: []int64{1006, 2006}, |
| 601 | PackedFixed32: []uint32{1007, 2007}, |
| 602 | PackedFixed64: []uint64{1008, 2008}, |
| 603 | PackedSfixed32: []int32{1009, 2009}, |
| 604 | PackedSfixed64: []int64{1010, 2010}, |
| 605 | PackedFloat: []float32{1011.5, 2011.5}, |
| 606 | PackedDouble: []float64{1012.5, 2012.5}, |
| 607 | PackedBool: []bool{true, false}, |
| 608 | PackedEnum: []testpb.ForeignEnum{ |
| 609 | testpb.ForeignEnum_FOREIGN_FOO, |
| 610 | testpb.ForeignEnum_FOREIGN_BAR, |
| 611 | }, |
| 612 | }, build( |
| 613 | &testpb.TestPackedExtensions{}, |
| 614 | extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}), |
| 615 | extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}), |
| 616 | extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}), |
| 617 | extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}), |
| 618 | extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}), |
| 619 | extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}), |
| 620 | extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}), |
| 621 | extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}), |
| 622 | extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}), |
| 623 | extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}), |
| 624 | extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}), |
| 625 | extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}), |
| 626 | extend(testpb.E_PackedBoolExtension, []bool{true, false}), |
| 627 | extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{ |
| 628 | testpb.ForeignEnum_FOREIGN_FOO, |
| 629 | testpb.ForeignEnum_FOREIGN_BAR, |
| 630 | }), |
| 631 | )}, |
| 632 | wire: pack.Message{ |
| 633 | pack.Tag{90, pack.BytesType}, pack.LengthPrefix{ |
| 634 | pack.Varint(1001), pack.Varint(2001), |
| 635 | }, |
| 636 | pack.Tag{91, pack.BytesType}, pack.LengthPrefix{ |
| 637 | pack.Varint(1002), pack.Varint(2002), |
| 638 | }, |
| 639 | pack.Tag{92, pack.BytesType}, pack.LengthPrefix{ |
| 640 | pack.Uvarint(1003), pack.Uvarint(2003), |
| 641 | }, |
| 642 | pack.Tag{93, pack.BytesType}, pack.LengthPrefix{ |
| 643 | pack.Uvarint(1004), pack.Uvarint(2004), |
| 644 | }, |
| 645 | pack.Tag{94, pack.BytesType}, pack.LengthPrefix{ |
| 646 | pack.Svarint(1005), pack.Svarint(2005), |
| 647 | }, |
| 648 | pack.Tag{95, pack.BytesType}, pack.LengthPrefix{ |
| 649 | pack.Svarint(1006), pack.Svarint(2006), |
| 650 | }, |
| 651 | pack.Tag{96, pack.BytesType}, pack.LengthPrefix{ |
| 652 | pack.Uint32(1007), pack.Uint32(2007), |
| 653 | }, |
| 654 | pack.Tag{97, pack.BytesType}, pack.LengthPrefix{ |
| 655 | pack.Uint64(1008), pack.Uint64(2008), |
| 656 | }, |
| 657 | pack.Tag{98, pack.BytesType}, pack.LengthPrefix{ |
| 658 | pack.Int32(1009), pack.Int32(2009), |
| 659 | }, |
| 660 | pack.Tag{99, pack.BytesType}, pack.LengthPrefix{ |
| 661 | pack.Int64(1010), pack.Int64(2010), |
| 662 | }, |
| 663 | pack.Tag{100, pack.BytesType}, pack.LengthPrefix{ |
| 664 | pack.Float32(1011.5), pack.Float32(2011.5), |
| 665 | }, |
| 666 | pack.Tag{101, pack.BytesType}, pack.LengthPrefix{ |
| 667 | pack.Float64(1012.5), pack.Float64(2012.5), |
| 668 | }, |
| 669 | pack.Tag{102, pack.BytesType}, pack.LengthPrefix{ |
| 670 | pack.Bool(true), pack.Bool(false), |
| 671 | }, |
| 672 | pack.Tag{103, pack.BytesType}, pack.LengthPrefix{ |
| 673 | pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)), |
| 674 | pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)), |
| 675 | }, |
| 676 | }.Marshal(), |
| 677 | }, |
| 678 | { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 679 | desc: "repeated messages", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 680 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 681 | RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 682 | {A: proto.Int32(1)}, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 683 | nil, |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 684 | {A: proto.Int32(2)}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 685 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 686 | }, &test3pb.TestAllTypes{ |
| 687 | RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{ |
| 688 | {A: 1}, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 689 | nil, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 690 | {A: 2}, |
| 691 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 692 | }, build( |
| 693 | &testpb.TestAllExtensions{}, |
| 694 | extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 695 | {A: proto.Int32(1)}, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 696 | nil, |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 697 | {A: proto.Int32(2)}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 698 | }), |
| 699 | )}, |
| 700 | wire: pack.Message{ |
| 701 | pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 702 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 703 | }), |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 704 | pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 705 | pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 706 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 707 | }), |
| 708 | }.Marshal(), |
| 709 | }, |
| 710 | { |
| 711 | desc: "repeated groups", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 712 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 713 | Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 714 | {A: proto.Int32(1017)}, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 715 | nil, |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 716 | {A: proto.Int32(2017)}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 717 | }, |
| 718 | }, build( |
| 719 | &testpb.TestAllExtensions{}, |
| 720 | extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 721 | {A: proto.Int32(1017)}, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 722 | nil, |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 723 | {A: proto.Int32(2017)}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 724 | }), |
| 725 | )}, |
| 726 | wire: pack.Message{ |
| 727 | pack.Tag{46, pack.StartGroupType}, |
| 728 | pack.Tag{47, pack.VarintType}, pack.Varint(1017), |
| 729 | pack.Tag{46, pack.EndGroupType}, |
| 730 | pack.Tag{46, pack.StartGroupType}, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 731 | pack.Tag{46, pack.EndGroupType}, |
| 732 | pack.Tag{46, pack.StartGroupType}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 733 | pack.Tag{47, pack.VarintType}, pack.Varint(2017), |
| 734 | pack.Tag{46, pack.EndGroupType}, |
| 735 | }.Marshal(), |
| 736 | }, |
| 737 | { |
| 738 | desc: "maps", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 739 | decodeTo: []proto.Message{&testpb.TestAllTypes{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 740 | MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156}, |
| 741 | MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157}, |
| 742 | MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158}, |
| 743 | MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159}, |
| 744 | MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160}, |
| 745 | MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161}, |
| 746 | MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162}, |
| 747 | MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163}, |
| 748 | MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164}, |
| 749 | MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165}, |
| 750 | MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5}, |
| 751 | MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5}, |
| 752 | MapBoolBool: map[bool]bool{true: false, false: true}, |
| 753 | MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"}, |
| 754 | MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")}, |
| 755 | MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 756 | "71.1.key": {A: proto.Int32(1171)}, |
| 757 | "71.2.key": {A: proto.Int32(2171)}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 758 | }, |
| 759 | MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{ |
| 760 | "73.1.key": testpb.TestAllTypes_FOO, |
| 761 | "73.2.key": testpb.TestAllTypes_BAR, |
| 762 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 763 | }, &test3pb.TestAllTypes{ |
| 764 | MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156}, |
| 765 | MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157}, |
| 766 | MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158}, |
| 767 | MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159}, |
| 768 | MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160}, |
| 769 | MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161}, |
| 770 | MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162}, |
| 771 | MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163}, |
| 772 | MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164}, |
| 773 | MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165}, |
| 774 | MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5}, |
| 775 | MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5}, |
| 776 | MapBoolBool: map[bool]bool{true: false, false: true}, |
| 777 | MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"}, |
| 778 | MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")}, |
| 779 | MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{ |
| 780 | "71.1.key": {A: 1171}, |
| 781 | "71.2.key": {A: 2171}, |
| 782 | }, |
| 783 | MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{ |
| 784 | "73.1.key": test3pb.TestAllTypes_FOO, |
| 785 | "73.2.key": test3pb.TestAllTypes_BAR, |
| 786 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 787 | }}, |
| 788 | wire: pack.Message{ |
| 789 | pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 790 | pack.Tag{1, pack.VarintType}, pack.Varint(1056), |
| 791 | pack.Tag{2, pack.VarintType}, pack.Varint(1156), |
| 792 | }), |
| 793 | pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 794 | pack.Tag{1, pack.VarintType}, pack.Varint(2056), |
| 795 | pack.Tag{2, pack.VarintType}, pack.Varint(2156), |
| 796 | }), |
| 797 | pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 798 | pack.Tag{1, pack.VarintType}, pack.Varint(1057), |
| 799 | pack.Tag{2, pack.VarintType}, pack.Varint(1157), |
| 800 | }), |
| 801 | pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 802 | pack.Tag{1, pack.VarintType}, pack.Varint(2057), |
| 803 | pack.Tag{2, pack.VarintType}, pack.Varint(2157), |
| 804 | }), |
| 805 | pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 806 | pack.Tag{1, pack.VarintType}, pack.Varint(1058), |
| 807 | pack.Tag{2, pack.VarintType}, pack.Varint(1158), |
| 808 | }), |
| 809 | pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 810 | pack.Tag{1, pack.VarintType}, pack.Varint(2058), |
| 811 | pack.Tag{2, pack.VarintType}, pack.Varint(2158), |
| 812 | }), |
| 813 | pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 814 | pack.Tag{1, pack.VarintType}, pack.Varint(1059), |
| 815 | pack.Tag{2, pack.VarintType}, pack.Varint(1159), |
| 816 | }), |
| 817 | pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 818 | pack.Tag{1, pack.VarintType}, pack.Varint(2059), |
| 819 | pack.Tag{2, pack.VarintType}, pack.Varint(2159), |
| 820 | }), |
| 821 | pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 822 | pack.Tag{1, pack.VarintType}, pack.Svarint(1060), |
| 823 | pack.Tag{2, pack.VarintType}, pack.Svarint(1160), |
| 824 | }), |
| 825 | pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 826 | pack.Tag{1, pack.VarintType}, pack.Svarint(2060), |
| 827 | pack.Tag{2, pack.VarintType}, pack.Svarint(2160), |
| 828 | }), |
| 829 | pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 830 | pack.Tag{1, pack.VarintType}, pack.Svarint(1061), |
| 831 | pack.Tag{2, pack.VarintType}, pack.Svarint(1161), |
| 832 | }), |
| 833 | pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 834 | pack.Tag{1, pack.VarintType}, pack.Svarint(2061), |
| 835 | pack.Tag{2, pack.VarintType}, pack.Svarint(2161), |
| 836 | }), |
| 837 | pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 838 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062), |
| 839 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162), |
| 840 | }), |
| 841 | pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 842 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062), |
| 843 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162), |
| 844 | }), |
| 845 | pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 846 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063), |
| 847 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163), |
| 848 | }), |
| 849 | pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 850 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063), |
| 851 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163), |
| 852 | }), |
| 853 | pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 854 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064), |
| 855 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164), |
| 856 | }), |
| 857 | pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 858 | pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064), |
| 859 | pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164), |
| 860 | }), |
| 861 | pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 862 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065), |
| 863 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165), |
| 864 | }), |
| 865 | pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 866 | pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065), |
| 867 | pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165), |
| 868 | }), |
| 869 | pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 870 | pack.Tag{1, pack.VarintType}, pack.Varint(1066), |
| 871 | pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5), |
| 872 | }), |
| 873 | pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 874 | pack.Tag{1, pack.VarintType}, pack.Varint(2066), |
| 875 | pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5), |
| 876 | }), |
| 877 | pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 878 | pack.Tag{1, pack.VarintType}, pack.Varint(1067), |
| 879 | pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5), |
| 880 | }), |
| 881 | pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 882 | pack.Tag{1, pack.VarintType}, pack.Varint(2067), |
| 883 | pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5), |
| 884 | }), |
| 885 | pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 886 | pack.Tag{1, pack.VarintType}, pack.Bool(true), |
| 887 | pack.Tag{2, pack.VarintType}, pack.Bool(false), |
| 888 | }), |
| 889 | pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 890 | pack.Tag{1, pack.VarintType}, pack.Bool(false), |
| 891 | pack.Tag{2, pack.VarintType}, pack.Bool(true), |
| 892 | }), |
| 893 | pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 894 | pack.Tag{1, pack.BytesType}, pack.String("69.1.key"), |
| 895 | pack.Tag{2, pack.BytesType}, pack.String("69.1.val"), |
| 896 | }), |
| 897 | pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 898 | pack.Tag{1, pack.BytesType}, pack.String("69.2.key"), |
| 899 | pack.Tag{2, pack.BytesType}, pack.String("69.2.val"), |
| 900 | }), |
| 901 | pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 902 | pack.Tag{1, pack.BytesType}, pack.String("70.1.key"), |
| 903 | pack.Tag{2, pack.BytesType}, pack.String("70.1.val"), |
| 904 | }), |
| 905 | pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 906 | pack.Tag{1, pack.BytesType}, pack.String("70.2.key"), |
| 907 | pack.Tag{2, pack.BytesType}, pack.String("70.2.val"), |
| 908 | }), |
| 909 | pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 910 | pack.Tag{1, pack.BytesType}, pack.String("71.1.key"), |
| 911 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 912 | pack.Tag{1, pack.VarintType}, pack.Varint(1171), |
| 913 | }), |
| 914 | }), |
| 915 | pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 916 | pack.Tag{1, pack.BytesType}, pack.String("71.2.key"), |
| 917 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 918 | pack.Tag{1, pack.VarintType}, pack.Varint(2171), |
| 919 | }), |
| 920 | }), |
| 921 | pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 922 | pack.Tag{1, pack.BytesType}, pack.String("73.1.key"), |
| 923 | pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)), |
| 924 | }), |
| 925 | pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 926 | pack.Tag{1, pack.BytesType}, pack.String("73.2.key"), |
| 927 | pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)), |
| 928 | }), |
| 929 | }.Marshal(), |
| 930 | }, |
| 931 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 932 | desc: "oneof (uint32)", |
| 933 | decodeTo: []proto.Message{ |
| 934 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}}, |
| 935 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}}, |
| 936 | }, |
| 937 | wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 938 | }, |
| 939 | { |
| 940 | desc: "oneof (message)", |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 941 | decodeTo: []proto.Message{ |
| 942 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 943 | &testpb.TestAllTypes_NestedMessage{A: proto.Int32(1112)}, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 944 | }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{ |
| 945 | &test3pb.TestAllTypes_NestedMessage{A: 1112}, |
| 946 | }}, |
| 947 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 948 | wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 949 | pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)}, |
| 950 | })}.Marshal(), |
| 951 | }, |
| 952 | { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 953 | desc: "oneof (empty message)", |
| 954 | decodeTo: []proto.Message{ |
| 955 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
| 956 | &testpb.TestAllTypes_NestedMessage{}, |
| 957 | }}, |
| 958 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{ |
| 959 | &test3pb.TestAllTypes_NestedMessage{}, |
| 960 | }}, |
| 961 | }, |
| 962 | wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(), |
| 963 | }, |
| 964 | { |
Joe Tsai | 6c28674 | 2019-07-11 23:15:05 -0700 | [diff] [blame] | 965 | desc: "oneof (merged message)", |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 966 | decodeTo: []proto.Message{ |
| 967 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{ |
| 968 | &testpb.TestAllTypes_NestedMessage{ |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 969 | A: proto.Int32(1), |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 970 | Corecursive: &testpb.TestAllTypes{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 971 | OptionalInt32: proto.Int32(43), |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 972 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 973 | }, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 974 | }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{ |
| 975 | &test3pb.TestAllTypes_NestedMessage{ |
Joe Tsai | 6c28674 | 2019-07-11 23:15:05 -0700 | [diff] [blame] | 976 | A: 1, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 977 | Corecursive: &test3pb.TestAllTypes{ |
| 978 | OptionalInt32: 43, |
| 979 | }, |
| 980 | }, |
| 981 | }}}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 982 | wire: pack.Message{ |
| 983 | pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 984 | pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)}, |
| 985 | }), |
| 986 | pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 987 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 988 | pack.Tag{1, pack.VarintType}, pack.Varint(43), |
| 989 | }), |
| 990 | }), |
| 991 | }.Marshal(), |
| 992 | }, |
| 993 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 994 | desc: "oneof (string)", |
| 995 | decodeTo: []proto.Message{ |
| 996 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}}, |
| 997 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}}, |
| 998 | }, |
| 999 | wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1000 | }, |
| 1001 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1002 | desc: "oneof (bytes)", |
| 1003 | decodeTo: []proto.Message{ |
| 1004 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}}, |
| 1005 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}}, |
| 1006 | }, |
| 1007 | wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1008 | }, |
| 1009 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1010 | desc: "oneof (bool)", |
| 1011 | decodeTo: []proto.Message{ |
| 1012 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}}, |
| 1013 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}}, |
| 1014 | }, |
| 1015 | wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1016 | }, |
| 1017 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1018 | desc: "oneof (uint64)", |
| 1019 | decodeTo: []proto.Message{ |
| 1020 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}}, |
| 1021 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}}, |
| 1022 | }, |
| 1023 | wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1024 | }, |
| 1025 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1026 | desc: "oneof (float)", |
| 1027 | decodeTo: []proto.Message{ |
| 1028 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}}, |
| 1029 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}}, |
| 1030 | }, |
| 1031 | 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] | 1032 | }, |
| 1033 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1034 | desc: "oneof (double)", |
| 1035 | decodeTo: []proto.Message{ |
| 1036 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}}, |
| 1037 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}}, |
| 1038 | }, |
| 1039 | 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] | 1040 | }, |
| 1041 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1042 | desc: "oneof (enum)", |
| 1043 | decodeTo: []proto.Message{ |
| 1044 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}}, |
| 1045 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}}, |
| 1046 | }, |
| 1047 | 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] | 1048 | }, |
| 1049 | { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1050 | desc: "oneof (zero)", |
| 1051 | decodeTo: []proto.Message{ |
| 1052 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}}, |
| 1053 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}}, |
| 1054 | }, |
| 1055 | wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(), |
| 1056 | }, |
| 1057 | { |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1058 | desc: "oneof (overridden value)", |
| 1059 | decodeTo: []proto.Message{ |
| 1060 | &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}}, |
| 1061 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}}, |
| 1062 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1063 | wire: pack.Message{ |
| 1064 | pack.Tag{111, pack.VarintType}, pack.Varint(1), |
| 1065 | pack.Tag{116, pack.VarintType}, pack.Varint(2), |
| 1066 | }.Marshal(), |
| 1067 | }, |
| 1068 | // TODO: More unknown field tests for ordering, repeated fields, etc. |
| 1069 | // |
| 1070 | // It is currently impossible to produce results that the v1 Equal |
| 1071 | // considers equivalent to those of the v1 decoder. Figure out if |
| 1072 | // that's a problem or not. |
| 1073 | { |
| 1074 | desc: "unknown fields", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 1075 | decodeTo: []proto.Message{build( |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1076 | &testpb.TestAllTypes{}, |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 1077 | unknown(pack.Message{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1078 | pack.Tag{100000, pack.VarintType}, pack.Varint(1), |
| 1079 | }.Marshal()), |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1080 | ), build( |
| 1081 | &test3pb.TestAllTypes{}, |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 1082 | unknown(pack.Message{ |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1083 | pack.Tag{100000, pack.VarintType}, pack.Varint(1), |
| 1084 | }.Marshal()), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1085 | )}, |
| 1086 | wire: pack.Message{ |
| 1087 | pack.Tag{100000, pack.VarintType}, pack.Varint(1), |
| 1088 | }.Marshal(), |
| 1089 | }, |
| 1090 | { |
| 1091 | desc: "field type mismatch", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 1092 | decodeTo: []proto.Message{build( |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1093 | &testpb.TestAllTypes{}, |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 1094 | unknown(pack.Message{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1095 | pack.Tag{1, pack.BytesType}, pack.String("string"), |
| 1096 | }.Marshal()), |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1097 | ), build( |
| 1098 | &test3pb.TestAllTypes{}, |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 1099 | unknown(pack.Message{ |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1100 | pack.Tag{1, pack.BytesType}, pack.String("string"), |
| 1101 | }.Marshal()), |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1102 | )}, |
| 1103 | wire: pack.Message{ |
| 1104 | pack.Tag{1, pack.BytesType}, pack.String("string"), |
| 1105 | }.Marshal(), |
| 1106 | }, |
| 1107 | { |
| 1108 | desc: "map field element mismatch", |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 1109 | decodeTo: []proto.Message{ |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1110 | &testpb.TestAllTypes{ |
| 1111 | MapInt32Int32: map[int32]int32{1: 0}, |
Damien Neil | 3b46ade | 2019-03-26 13:55:02 -0700 | [diff] [blame] | 1112 | }, &test3pb.TestAllTypes{ |
| 1113 | MapInt32Int32: map[int32]int32{1: 0}, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1114 | }, |
| 1115 | }, |
| 1116 | wire: pack.Message{ |
| 1117 | pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1118 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1119 | pack.Tag{2, pack.BytesType}, pack.String("string"), |
| 1120 | }), |
| 1121 | }.Marshal(), |
| 1122 | }, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1123 | { |
Damien Neil | 3d0706a | 2019-07-09 11:40:49 -0700 | [diff] [blame] | 1124 | desc: "required field in nil message unset", |
| 1125 | partial: true, |
| 1126 | decodeTo: []proto.Message{(*testpb.TestRequired)(nil)}, |
| 1127 | }, |
| 1128 | { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1129 | desc: "required field unset", |
| 1130 | partial: true, |
| 1131 | decodeTo: []proto.Message{&testpb.TestRequired{}}, |
| 1132 | }, |
| 1133 | { |
| 1134 | desc: "required field set", |
| 1135 | decodeTo: []proto.Message{&testpb.TestRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1136 | RequiredField: proto.Int32(1), |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1137 | }}, |
| 1138 | wire: pack.Message{ |
| 1139 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1140 | }.Marshal(), |
| 1141 | }, |
| 1142 | { |
| 1143 | desc: "required field in optional message unset", |
| 1144 | partial: true, |
| 1145 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 1146 | OptionalMessage: &testpb.TestRequired{}, |
| 1147 | }}, |
| 1148 | wire: pack.Message{ |
| 1149 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 1150 | }.Marshal(), |
| 1151 | }, |
| 1152 | { |
| 1153 | desc: "required field in optional message set", |
| 1154 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 1155 | OptionalMessage: &testpb.TestRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1156 | RequiredField: proto.Int32(1), |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1157 | }, |
| 1158 | }}, |
| 1159 | wire: pack.Message{ |
| 1160 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1161 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1162 | }), |
| 1163 | }.Marshal(), |
| 1164 | }, |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 1165 | { |
| 1166 | desc: "required field in optional message set (split across multiple tags)", |
| 1167 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 1168 | OptionalMessage: &testpb.TestRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1169 | RequiredField: proto.Int32(1), |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 1170 | }, |
| 1171 | }}, |
| 1172 | wire: pack.Message{ |
| 1173 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 1174 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1175 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1176 | }), |
| 1177 | }.Marshal(), |
| 1178 | }, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1179 | { |
| 1180 | desc: "required field in repeated message unset", |
| 1181 | partial: true, |
| 1182 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 1183 | RepeatedMessage: []*testpb.TestRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1184 | {RequiredField: proto.Int32(1)}, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1185 | {}, |
| 1186 | }, |
| 1187 | }}, |
| 1188 | wire: pack.Message{ |
| 1189 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1190 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1191 | }), |
| 1192 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 1193 | }.Marshal(), |
| 1194 | }, |
| 1195 | { |
| 1196 | desc: "required field in repeated message set", |
| 1197 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 1198 | RepeatedMessage: []*testpb.TestRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1199 | {RequiredField: proto.Int32(1)}, |
| 1200 | {RequiredField: proto.Int32(2)}, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1201 | }, |
| 1202 | }}, |
| 1203 | wire: pack.Message{ |
| 1204 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1205 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1206 | }), |
| 1207 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1208 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 1209 | }), |
| 1210 | }.Marshal(), |
| 1211 | }, |
| 1212 | { |
| 1213 | desc: "required field in map message unset", |
| 1214 | partial: true, |
| 1215 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 1216 | MapMessage: map[int32]*testpb.TestRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1217 | 1: {RequiredField: proto.Int32(1)}, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1218 | 2: {}, |
| 1219 | }, |
| 1220 | }}, |
| 1221 | wire: pack.Message{ |
| 1222 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1223 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1224 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1225 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1226 | }), |
| 1227 | }), |
| 1228 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1229 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 1230 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 1231 | }), |
| 1232 | }.Marshal(), |
| 1233 | }, |
| 1234 | { |
| 1235 | desc: "required field in map message set", |
| 1236 | decodeTo: []proto.Message{&testpb.TestRequiredForeign{ |
| 1237 | MapMessage: map[int32]*testpb.TestRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1238 | 1: {RequiredField: proto.Int32(1)}, |
| 1239 | 2: {RequiredField: proto.Int32(2)}, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1240 | }, |
| 1241 | }}, |
| 1242 | wire: pack.Message{ |
| 1243 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1244 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1245 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1246 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1247 | }), |
| 1248 | }), |
| 1249 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1250 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 1251 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1252 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 1253 | }), |
| 1254 | }), |
| 1255 | }.Marshal(), |
| 1256 | }, |
| 1257 | { |
| 1258 | desc: "required field in optional group unset", |
| 1259 | partial: true, |
| 1260 | decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{ |
| 1261 | Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{}, |
| 1262 | }}, |
| 1263 | wire: pack.Message{ |
| 1264 | pack.Tag{1, pack.StartGroupType}, |
| 1265 | pack.Tag{1, pack.EndGroupType}, |
| 1266 | }.Marshal(), |
| 1267 | }, |
| 1268 | { |
| 1269 | desc: "required field in optional group set", |
| 1270 | decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{ |
| 1271 | Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1272 | A: proto.Int32(1), |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1273 | }, |
| 1274 | }}, |
| 1275 | wire: pack.Message{ |
| 1276 | pack.Tag{1, pack.StartGroupType}, |
| 1277 | pack.Tag{2, pack.VarintType}, pack.Varint(1), |
| 1278 | pack.Tag{1, pack.EndGroupType}, |
| 1279 | }.Marshal(), |
| 1280 | }, |
| 1281 | { |
| 1282 | desc: "required field in repeated group unset", |
| 1283 | partial: true, |
| 1284 | decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{ |
| 1285 | Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1286 | {A: proto.Int32(1)}, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1287 | {}, |
| 1288 | }, |
| 1289 | }}, |
| 1290 | wire: pack.Message{ |
| 1291 | pack.Tag{3, pack.StartGroupType}, |
| 1292 | pack.Tag{4, pack.VarintType}, pack.Varint(1), |
| 1293 | pack.Tag{3, pack.EndGroupType}, |
| 1294 | pack.Tag{3, pack.StartGroupType}, |
| 1295 | pack.Tag{3, pack.EndGroupType}, |
| 1296 | }.Marshal(), |
| 1297 | }, |
| 1298 | { |
| 1299 | desc: "required field in repeated group set", |
| 1300 | decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{ |
| 1301 | Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1302 | {A: proto.Int32(1)}, |
| 1303 | {A: proto.Int32(2)}, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1304 | }, |
| 1305 | }}, |
| 1306 | wire: pack.Message{ |
| 1307 | pack.Tag{3, pack.StartGroupType}, |
| 1308 | pack.Tag{4, pack.VarintType}, pack.Varint(1), |
| 1309 | pack.Tag{3, pack.EndGroupType}, |
| 1310 | pack.Tag{3, pack.StartGroupType}, |
| 1311 | pack.Tag{4, pack.VarintType}, pack.Varint(2), |
| 1312 | pack.Tag{3, pack.EndGroupType}, |
| 1313 | }.Marshal(), |
| 1314 | }, |
| 1315 | { |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 1316 | desc: "required field in oneof message unset", |
| 1317 | partial: true, |
| 1318 | decodeTo: []proto.Message{ |
| 1319 | &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{ |
| 1320 | &testpb.TestRequired{}, |
| 1321 | }}, |
| 1322 | }, |
| 1323 | wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(), |
| 1324 | }, |
| 1325 | { |
| 1326 | desc: "required field in oneof message set", |
| 1327 | decodeTo: []proto.Message{ |
| 1328 | &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{ |
| 1329 | &testpb.TestRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1330 | RequiredField: proto.Int32(1), |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 1331 | }, |
| 1332 | }}, |
| 1333 | }, |
| 1334 | wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1335 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1336 | })}.Marshal(), |
| 1337 | }, |
| 1338 | { |
Joe Tsai | 09cef32 | 2019-07-11 22:13:49 -0700 | [diff] [blame] | 1339 | desc: "required field in extension message unset", |
| 1340 | partial: true, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1341 | decodeTo: []proto.Message{build( |
| 1342 | &testpb.TestAllExtensions{}, |
| 1343 | extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}), |
| 1344 | )}, |
| 1345 | wire: pack.Message{ |
| 1346 | pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 1347 | }.Marshal(), |
| 1348 | }, |
| 1349 | { |
| 1350 | desc: "required field in extension message set", |
| 1351 | decodeTo: []proto.Message{build( |
| 1352 | &testpb.TestAllExtensions{}, |
| 1353 | extend(testpb.E_TestRequired_Single, &testpb.TestRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1354 | RequiredField: proto.Int32(1), |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1355 | }), |
| 1356 | )}, |
| 1357 | wire: pack.Message{ |
| 1358 | pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1359 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1360 | }), |
| 1361 | }.Marshal(), |
| 1362 | }, |
| 1363 | { |
Joe Tsai | 09cef32 | 2019-07-11 22:13:49 -0700 | [diff] [blame] | 1364 | desc: "required field in repeated extension message unset", |
| 1365 | partial: true, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1366 | decodeTo: []proto.Message{build( |
| 1367 | &testpb.TestAllExtensions{}, |
| 1368 | extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1369 | {RequiredField: proto.Int32(1)}, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1370 | {}, |
| 1371 | }), |
| 1372 | )}, |
| 1373 | wire: pack.Message{ |
| 1374 | pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1375 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1376 | }), |
| 1377 | pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 1378 | }.Marshal(), |
| 1379 | }, |
| 1380 | { |
| 1381 | desc: "required field in repeated extension message set", |
| 1382 | decodeTo: []proto.Message{build( |
| 1383 | &testpb.TestAllExtensions{}, |
| 1384 | extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1385 | {RequiredField: proto.Int32(1)}, |
| 1386 | {RequiredField: proto.Int32(2)}, |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 1387 | }), |
| 1388 | )}, |
| 1389 | wire: pack.Message{ |
| 1390 | pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1391 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1392 | }), |
| 1393 | pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1394 | pack.Tag{1, pack.VarintType}, pack.Varint(2), |
| 1395 | }), |
| 1396 | }.Marshal(), |
| 1397 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1398 | { |
Damien Neil | 3d0706a | 2019-07-09 11:40:49 -0700 | [diff] [blame] | 1399 | desc: "nil messages", |
| 1400 | decodeTo: []proto.Message{ |
| 1401 | (*testpb.TestAllTypes)(nil), |
| 1402 | (*test3pb.TestAllTypes)(nil), |
| 1403 | (*testpb.TestAllExtensions)(nil), |
| 1404 | }, |
| 1405 | }, |
| 1406 | { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1407 | desc: "legacy", |
| 1408 | partial: true, |
| 1409 | decodeTo: []proto.Message{ |
| 1410 | &legacypb.Legacy{ |
| 1411 | F1: &legacy1pb.Message{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1412 | OptionalInt32: proto.Int32(1), |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1413 | OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(), |
| 1414 | OptionalChildMessage: &legacy1pb.Message_ChildMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1415 | F1: proto.String("x"), |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1416 | }, |
| 1417 | Optionalgroup: &legacy1pb.Message_OptionalGroup{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1418 | F1: proto.String("x"), |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1419 | }, |
| 1420 | RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1421 | {F1: proto.String("x")}, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1422 | }, |
| 1423 | Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1424 | {F1: proto.String("x")}, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1425 | }, |
| 1426 | MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1427 | true: {F1: proto.String("x")}, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1428 | }, |
| 1429 | OneofUnion: &legacy1pb.Message_OneofChildMessage{ |
| 1430 | &legacy1pb.Message_ChildMessage{ |
Damien Neil | a8a2cea | 2019-07-10 16:17:16 -0700 | [diff] [blame] | 1431 | F1: proto.String("x"), |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1432 | }, |
| 1433 | }, |
| 1434 | }, |
| 1435 | }, |
| 1436 | }, |
| 1437 | wire: pack.Message{ |
| 1438 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1439 | pack.Tag{101, pack.VarintType}, pack.Varint(1), |
| 1440 | pack.Tag{115, pack.VarintType}, pack.Varint(0), |
| 1441 | pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1442 | pack.Tag{1, pack.BytesType}, pack.String("x"), |
| 1443 | }), |
| 1444 | pack.Tag{120, pack.StartGroupType}, |
| 1445 | pack.Tag{1, pack.BytesType}, pack.String("x"), |
| 1446 | pack.Tag{120, pack.EndGroupType}, |
| 1447 | pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1448 | pack.Tag{1, pack.BytesType}, pack.String("x"), |
| 1449 | }), |
| 1450 | pack.Tag{520, pack.StartGroupType}, |
| 1451 | pack.Tag{1, pack.BytesType}, pack.String("x"), |
| 1452 | pack.Tag{520, pack.EndGroupType}, |
| 1453 | pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1454 | pack.Tag{1, pack.VarintType}, pack.Varint(1), |
| 1455 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1456 | pack.Tag{1, pack.BytesType}, pack.String("x"), |
| 1457 | }), |
| 1458 | }), |
| 1459 | pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1460 | pack.Tag{1, pack.BytesType}, pack.String("x"), |
| 1461 | }), |
| 1462 | }), |
| 1463 | }.Marshal(), |
| 1464 | }, |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 1467 | var invalidUTF8TestProtos = []testProto{ |
| 1468 | { |
| 1469 | desc: "invalid UTF-8 in optional string field", |
| 1470 | decodeTo: []proto.Message{&test3pb.TestAllTypes{ |
| 1471 | OptionalString: "abc\xff", |
| 1472 | }}, |
| 1473 | wire: pack.Message{ |
| 1474 | pack.Tag{14, pack.BytesType}, pack.String("abc\xff"), |
| 1475 | }.Marshal(), |
| 1476 | }, |
| 1477 | { |
| 1478 | desc: "invalid UTF-8 in repeated string field", |
| 1479 | decodeTo: []proto.Message{&test3pb.TestAllTypes{ |
| 1480 | RepeatedString: []string{"foo", "abc\xff"}, |
| 1481 | }}, |
| 1482 | wire: pack.Message{ |
| 1483 | pack.Tag{44, pack.BytesType}, pack.String("foo"), |
| 1484 | pack.Tag{44, pack.BytesType}, pack.String("abc\xff"), |
| 1485 | }.Marshal(), |
| 1486 | }, |
| 1487 | { |
| 1488 | desc: "invalid UTF-8 in nested message", |
| 1489 | decodeTo: []proto.Message{&test3pb.TestAllTypes{ |
| 1490 | OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{ |
| 1491 | Corecursive: &test3pb.TestAllTypes{ |
| 1492 | OptionalString: "abc\xff", |
| 1493 | }, |
| 1494 | }, |
| 1495 | }}, |
| 1496 | wire: pack.Message{ |
| 1497 | pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1498 | pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1499 | pack.Tag{14, pack.BytesType}, pack.String("abc\xff"), |
| 1500 | }), |
| 1501 | }), |
| 1502 | }.Marshal(), |
| 1503 | }, |
| 1504 | { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1505 | desc: "invalid UTF-8 in oneof field", |
| 1506 | decodeTo: []proto.Message{ |
| 1507 | &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}}, |
| 1508 | }, |
| 1509 | wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(), |
| 1510 | }, |
| 1511 | { |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 1512 | desc: "invalid UTF-8 in map key", |
| 1513 | decodeTo: []proto.Message{&test3pb.TestAllTypes{ |
| 1514 | MapStringString: map[string]string{"key\xff": "val"}, |
| 1515 | }}, |
| 1516 | wire: pack.Message{ |
| 1517 | pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1518 | pack.Tag{1, pack.BytesType}, pack.String("key\xff"), |
| 1519 | pack.Tag{2, pack.BytesType}, pack.String("val"), |
| 1520 | }), |
| 1521 | }.Marshal(), |
| 1522 | }, |
| 1523 | { |
| 1524 | desc: "invalid UTF-8 in map value", |
| 1525 | decodeTo: []proto.Message{&test3pb.TestAllTypes{ |
| 1526 | MapStringString: map[string]string{"key": "val\xff"}, |
| 1527 | }}, |
| 1528 | wire: pack.Message{ |
| 1529 | pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 1530 | pack.Tag{1, pack.BytesType}, pack.String("key"), |
| 1531 | pack.Tag{2, pack.BytesType}, pack.String("val\xff"), |
| 1532 | }), |
| 1533 | }.Marshal(), |
| 1534 | }, |
| 1535 | } |
| 1536 | |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 1537 | var noEnforceUTF8TestProtos = []testProto{ |
| 1538 | { |
| 1539 | desc: "invalid UTF-8 in optional string field", |
| 1540 | decodeTo: []proto.Message{&TestNoEnforceUTF8{ |
| 1541 | OptionalString: string("abc\xff"), |
| 1542 | }}, |
| 1543 | wire: pack.Message{ |
| 1544 | pack.Tag{1, pack.BytesType}, pack.String("abc\xff"), |
| 1545 | }.Marshal(), |
| 1546 | }, |
| 1547 | { |
| 1548 | desc: "invalid UTF-8 in optional string field of Go bytes", |
| 1549 | decodeTo: []proto.Message{&TestNoEnforceUTF8{ |
| 1550 | OptionalBytes: []byte("abc\xff"), |
| 1551 | }}, |
| 1552 | wire: pack.Message{ |
| 1553 | pack.Tag{2, pack.BytesType}, pack.String("abc\xff"), |
| 1554 | }.Marshal(), |
| 1555 | }, |
| 1556 | { |
| 1557 | desc: "invalid UTF-8 in repeated string field", |
| 1558 | decodeTo: []proto.Message{&TestNoEnforceUTF8{ |
| 1559 | RepeatedString: []string{string("foo"), string("abc\xff")}, |
| 1560 | }}, |
| 1561 | wire: pack.Message{ |
| 1562 | pack.Tag{3, pack.BytesType}, pack.String("foo"), |
| 1563 | pack.Tag{3, pack.BytesType}, pack.String("abc\xff"), |
| 1564 | }.Marshal(), |
| 1565 | }, |
| 1566 | { |
| 1567 | desc: "invalid UTF-8 in repeated string field of Go bytes", |
| 1568 | decodeTo: []proto.Message{&TestNoEnforceUTF8{ |
| 1569 | RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")}, |
| 1570 | }}, |
| 1571 | wire: pack.Message{ |
| 1572 | pack.Tag{4, pack.BytesType}, pack.String("foo"), |
| 1573 | pack.Tag{4, pack.BytesType}, pack.String("abc\xff"), |
| 1574 | }.Marshal(), |
| 1575 | }, |
| 1576 | { |
| 1577 | desc: "invalid UTF-8 in oneof string field", |
| 1578 | decodeTo: []proto.Message{ |
| 1579 | &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}}, |
| 1580 | }, |
| 1581 | wire: pack.Message{pack.Tag{5, pack.BytesType}, pack.String("abc\xff")}.Marshal(), |
| 1582 | }, |
| 1583 | { |
| 1584 | desc: "invalid UTF-8 in oneof string field of Go bytes", |
| 1585 | decodeTo: []proto.Message{ |
| 1586 | &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}}, |
| 1587 | }, |
| 1588 | wire: pack.Message{pack.Tag{6, pack.BytesType}, pack.String("abc\xff")}.Marshal(), |
| 1589 | }, |
| 1590 | } |
| 1591 | |
| 1592 | type TestNoEnforceUTF8 struct { |
| 1593 | OptionalString string `protobuf:"bytes,1,opt,name=optional_string"` |
| 1594 | OptionalBytes []byte `protobuf:"bytes,2,opt,name=optional_bytes"` |
| 1595 | RepeatedString []string `protobuf:"bytes,3,rep,name=repeated_string"` |
| 1596 | RepeatedBytes [][]byte `protobuf:"bytes,4,rep,name=repeated_bytes"` |
| 1597 | OneofField isOneofField `protobuf_oneof:"oneof_field"` |
| 1598 | } |
| 1599 | |
| 1600 | type isOneofField interface{ isOneofField() } |
| 1601 | |
| 1602 | type TestNoEnforceUTF8_OneofString struct { |
| 1603 | OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"` |
| 1604 | } |
| 1605 | type TestNoEnforceUTF8_OneofBytes struct { |
| 1606 | OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"` |
| 1607 | } |
| 1608 | |
| 1609 | func (*TestNoEnforceUTF8_OneofString) isOneofField() {} |
| 1610 | func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {} |
| 1611 | |
Joe Tsai | 74615a3 | 2019-07-14 18:51:46 -0700 | [diff] [blame] | 1612 | func (m *TestNoEnforceUTF8) ProtoReflect() protoreflect.Message { |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 1613 | return messageInfo_TestNoEnforceUTF8.MessageOf(m) |
| 1614 | } |
| 1615 | |
| 1616 | var messageInfo_TestNoEnforceUTF8 = protoimpl.MessageInfo{ |
| 1617 | GoType: reflect.TypeOf((*TestNoEnforceUTF8)(nil)), |
| 1618 | PBType: &prototype.Message{ |
| 1619 | MessageDescriptor: func() protoreflect.MessageDescriptor { |
| 1620 | pb := new(descriptorpb.FileDescriptorProto) |
| 1621 | if err := prototext.Unmarshal([]byte(` |
| 1622 | syntax: "proto3" |
| 1623 | name: "test.proto" |
| 1624 | message_type: [{ |
| 1625 | name: "TestNoEnforceUTF8" |
| 1626 | field: [ |
| 1627 | {name:"optional_string" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, |
| 1628 | {name:"optional_bytes" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}, |
| 1629 | {name:"repeated_string" number:3 label:LABEL_REPEATED type:TYPE_STRING}, |
| 1630 | {name:"repeated_bytes" number:4 label:LABEL_REPEATED type:TYPE_STRING}, |
| 1631 | {name:"oneof_string" number:5 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0}, |
| 1632 | {name:"oneof_bytes" number:6 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0} |
| 1633 | ] |
| 1634 | oneof_decl: [{name:"oneof_field"}] |
| 1635 | }] |
| 1636 | `), pb); err != nil { |
| 1637 | panic(err) |
| 1638 | } |
| 1639 | fd, err := protodesc.NewFile(pb, nil) |
| 1640 | if err != nil { |
| 1641 | panic(err) |
| 1642 | } |
| 1643 | md := fd.Messages().Get(0) |
| 1644 | for i := 0; i < md.Fields().Len(); i++ { |
| 1645 | md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true |
| 1646 | md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false |
| 1647 | } |
| 1648 | return md |
| 1649 | }(), |
Joe Tsai | 74615a3 | 2019-07-14 18:51:46 -0700 | [diff] [blame] | 1650 | NewMessage: func() protoreflect.Message { |
| 1651 | return protoreflect.ProtoMessage(new(TestNoEnforceUTF8)).ProtoReflect() |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 1652 | }, |
| 1653 | }, |
| 1654 | OneofWrappers: []interface{}{ |
| 1655 | (*TestNoEnforceUTF8_OneofString)(nil), |
| 1656 | (*TestNoEnforceUTF8_OneofBytes)(nil), |
| 1657 | }, |
| 1658 | } |
| 1659 | |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 1660 | func build(m proto.Message, opts ...buildOpt) proto.Message { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1661 | for _, opt := range opts { |
| 1662 | opt(m) |
| 1663 | } |
| 1664 | return m |
| 1665 | } |
| 1666 | |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 1667 | type buildOpt func(proto.Message) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1668 | |
Joe Tsai | 74615a3 | 2019-07-14 18:51:46 -0700 | [diff] [blame] | 1669 | func unknown(raw protoreflect.RawFields) buildOpt { |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 1670 | return func(m proto.Message) { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 1671 | m.ProtoReflect().SetUnknown(raw) |
Damien Neil | e6f060f | 2019-04-23 17:11:02 -0700 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | |
Joe Tsai | 74615a3 | 2019-07-14 18:51:46 -0700 | [diff] [blame] | 1675 | func extend(desc *protoiface.ExtensionDescV1, value interface{}) buildOpt { |
Joe Tsai | 8d30bbe | 2019-05-16 15:53:25 -0700 | [diff] [blame] | 1676 | // TODO: Should ExtensionType.ValueOf accept []T instead of *[]T? |
| 1677 | t := reflect.TypeOf(value) |
| 1678 | if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 { |
| 1679 | v := reflect.New(t) |
| 1680 | v.Elem().Set(reflect.ValueOf(value)) |
| 1681 | value = v.Interface() |
| 1682 | } |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 1683 | return func(m proto.Message) { |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 1684 | proto.SetExtension(m, desc, value) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 1685 | } |
| 1686 | } |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 1687 | |
| 1688 | func marshalText(m proto.Message) string { |
Joe Tsai | 8d30bbe | 2019-05-16 15:53:25 -0700 | [diff] [blame] | 1689 | b, _ := prototext.MarshalOptions{Indent: "\t", AllowPartial: true}.Marshal(m) |
Damien Neil | 61e93c7 | 2019-03-27 09:23:20 -0700 | [diff] [blame] | 1690 | return string(b) |
| 1691 | } |