Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "reflect" |
| 10 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 11 | "google.golang.org/protobuf/internal/encoding/wire" |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/internal/strs" |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 13 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 14 | ) |
| 15 | |
| 16 | // pointerCoderFuncs is a set of pointer encoding functions. |
| 17 | type pointerCoderFuncs struct { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 18 | size func(p pointer, tagsize int, opts marshalOptions) int |
| 19 | marshal func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) |
| 20 | unmarshal func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) |
| 21 | isInit func(p pointer) error |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 24 | // valueCoderFuncs is a set of protoreflect.Value encoding functions. |
| 25 | type valueCoderFuncs struct { |
| 26 | size func(v pref.Value, tagsize int, opts marshalOptions) int |
| 27 | marshal func(b []byte, v pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) |
| 28 | unmarshal func(b []byte, v pref.Value, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (pref.Value, int, error) |
| 29 | isInit func(v pref.Value) error |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // fieldCoder returns pointer functions for a field, used for operating on |
| 33 | // struct fields. |
| 34 | func fieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
| 35 | switch { |
Damien Neil | 4ae30bb | 2019-06-20 10:12:23 -0700 | [diff] [blame] | 36 | case fd.IsMap(): |
| 37 | return encoderFuncsForMap(fd, ft) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 38 | case fd.Cardinality() == pref.Repeated && !fd.IsPacked(): |
| 39 | // Repeated fields (not packed). |
| 40 | if ft.Kind() != reflect.Slice { |
| 41 | break |
| 42 | } |
| 43 | ft := ft.Elem() |
| 44 | switch fd.Kind() { |
| 45 | case pref.BoolKind: |
| 46 | if ft.Kind() == reflect.Bool { |
| 47 | return coderBoolSlice |
| 48 | } |
| 49 | case pref.EnumKind: |
| 50 | if ft.Kind() == reflect.Int32 { |
| 51 | return coderEnumSlice |
| 52 | } |
| 53 | case pref.Int32Kind: |
| 54 | if ft.Kind() == reflect.Int32 { |
| 55 | return coderInt32Slice |
| 56 | } |
| 57 | case pref.Sint32Kind: |
| 58 | if ft.Kind() == reflect.Int32 { |
| 59 | return coderSint32Slice |
| 60 | } |
| 61 | case pref.Uint32Kind: |
| 62 | if ft.Kind() == reflect.Uint32 { |
| 63 | return coderUint32Slice |
| 64 | } |
| 65 | case pref.Int64Kind: |
| 66 | if ft.Kind() == reflect.Int64 { |
| 67 | return coderInt64Slice |
| 68 | } |
| 69 | case pref.Sint64Kind: |
| 70 | if ft.Kind() == reflect.Int64 { |
| 71 | return coderSint64Slice |
| 72 | } |
| 73 | case pref.Uint64Kind: |
| 74 | if ft.Kind() == reflect.Uint64 { |
| 75 | return coderUint64Slice |
| 76 | } |
| 77 | case pref.Sfixed32Kind: |
| 78 | if ft.Kind() == reflect.Int32 { |
| 79 | return coderSfixed32Slice |
| 80 | } |
| 81 | case pref.Fixed32Kind: |
| 82 | if ft.Kind() == reflect.Uint32 { |
| 83 | return coderFixed32Slice |
| 84 | } |
| 85 | case pref.FloatKind: |
| 86 | if ft.Kind() == reflect.Float32 { |
| 87 | return coderFloatSlice |
| 88 | } |
| 89 | case pref.Sfixed64Kind: |
| 90 | if ft.Kind() == reflect.Int64 { |
| 91 | return coderSfixed64Slice |
| 92 | } |
| 93 | case pref.Fixed64Kind: |
| 94 | if ft.Kind() == reflect.Uint64 { |
| 95 | return coderFixed64Slice |
| 96 | } |
| 97 | case pref.DoubleKind: |
| 98 | if ft.Kind() == reflect.Float64 { |
| 99 | return coderDoubleSlice |
| 100 | } |
| 101 | case pref.StringKind: |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 102 | if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 103 | return coderStringSliceValidateUTF8 |
| 104 | } |
| 105 | if ft.Kind() == reflect.String { |
| 106 | return coderStringSlice |
| 107 | } |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 108 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) { |
| 109 | return coderBytesSliceValidateUTF8 |
| 110 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 111 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
| 112 | return coderBytesSlice |
| 113 | } |
| 114 | case pref.BytesKind: |
| 115 | if ft.Kind() == reflect.String { |
| 116 | return coderStringSlice |
| 117 | } |
| 118 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
| 119 | return coderBytesSlice |
| 120 | } |
| 121 | case pref.MessageKind: |
| 122 | return makeMessageSliceFieldCoder(fd, ft) |
| 123 | case pref.GroupKind: |
| 124 | return makeGroupSliceFieldCoder(fd, ft) |
| 125 | } |
| 126 | case fd.Cardinality() == pref.Repeated && fd.IsPacked(): |
| 127 | // Packed repeated fields. |
| 128 | // |
| 129 | // Only repeated fields of primitive numeric types |
| 130 | // (Varint, Fixed32, or Fixed64 wire type) can be packed. |
| 131 | if ft.Kind() != reflect.Slice { |
| 132 | break |
| 133 | } |
| 134 | ft := ft.Elem() |
| 135 | switch fd.Kind() { |
| 136 | case pref.BoolKind: |
| 137 | if ft.Kind() == reflect.Bool { |
| 138 | return coderBoolPackedSlice |
| 139 | } |
| 140 | case pref.EnumKind: |
| 141 | if ft.Kind() == reflect.Int32 { |
| 142 | return coderEnumPackedSlice |
| 143 | } |
| 144 | case pref.Int32Kind: |
| 145 | if ft.Kind() == reflect.Int32 { |
| 146 | return coderInt32PackedSlice |
| 147 | } |
| 148 | case pref.Sint32Kind: |
| 149 | if ft.Kind() == reflect.Int32 { |
| 150 | return coderSint32PackedSlice |
| 151 | } |
| 152 | case pref.Uint32Kind: |
| 153 | if ft.Kind() == reflect.Uint32 { |
| 154 | return coderUint32PackedSlice |
| 155 | } |
| 156 | case pref.Int64Kind: |
| 157 | if ft.Kind() == reflect.Int64 { |
| 158 | return coderInt64PackedSlice |
| 159 | } |
| 160 | case pref.Sint64Kind: |
| 161 | if ft.Kind() == reflect.Int64 { |
| 162 | return coderSint64PackedSlice |
| 163 | } |
| 164 | case pref.Uint64Kind: |
| 165 | if ft.Kind() == reflect.Uint64 { |
| 166 | return coderUint64PackedSlice |
| 167 | } |
| 168 | case pref.Sfixed32Kind: |
| 169 | if ft.Kind() == reflect.Int32 { |
| 170 | return coderSfixed32PackedSlice |
| 171 | } |
| 172 | case pref.Fixed32Kind: |
| 173 | if ft.Kind() == reflect.Uint32 { |
| 174 | return coderFixed32PackedSlice |
| 175 | } |
| 176 | case pref.FloatKind: |
| 177 | if ft.Kind() == reflect.Float32 { |
| 178 | return coderFloatPackedSlice |
| 179 | } |
| 180 | case pref.Sfixed64Kind: |
| 181 | if ft.Kind() == reflect.Int64 { |
| 182 | return coderSfixed64PackedSlice |
| 183 | } |
| 184 | case pref.Fixed64Kind: |
| 185 | if ft.Kind() == reflect.Uint64 { |
| 186 | return coderFixed64PackedSlice |
| 187 | } |
| 188 | case pref.DoubleKind: |
| 189 | if ft.Kind() == reflect.Float64 { |
| 190 | return coderDoublePackedSlice |
| 191 | } |
| 192 | } |
| 193 | case fd.Kind() == pref.MessageKind: |
| 194 | return makeMessageFieldCoder(fd, ft) |
| 195 | case fd.Kind() == pref.GroupKind: |
| 196 | return makeGroupFieldCoder(fd, ft) |
| 197 | case fd.Syntax() == pref.Proto3 && fd.ContainingOneof() == nil: |
| 198 | // Populated oneof fields always encode even if set to the zero value, |
| 199 | // which normally are not encoded in proto3. |
| 200 | switch fd.Kind() { |
| 201 | case pref.BoolKind: |
| 202 | if ft.Kind() == reflect.Bool { |
| 203 | return coderBoolNoZero |
| 204 | } |
| 205 | case pref.EnumKind: |
| 206 | if ft.Kind() == reflect.Int32 { |
| 207 | return coderEnumNoZero |
| 208 | } |
| 209 | case pref.Int32Kind: |
| 210 | if ft.Kind() == reflect.Int32 { |
| 211 | return coderInt32NoZero |
| 212 | } |
| 213 | case pref.Sint32Kind: |
| 214 | if ft.Kind() == reflect.Int32 { |
| 215 | return coderSint32NoZero |
| 216 | } |
| 217 | case pref.Uint32Kind: |
| 218 | if ft.Kind() == reflect.Uint32 { |
| 219 | return coderUint32NoZero |
| 220 | } |
| 221 | case pref.Int64Kind: |
| 222 | if ft.Kind() == reflect.Int64 { |
| 223 | return coderInt64NoZero |
| 224 | } |
| 225 | case pref.Sint64Kind: |
| 226 | if ft.Kind() == reflect.Int64 { |
| 227 | return coderSint64NoZero |
| 228 | } |
| 229 | case pref.Uint64Kind: |
| 230 | if ft.Kind() == reflect.Uint64 { |
| 231 | return coderUint64NoZero |
| 232 | } |
| 233 | case pref.Sfixed32Kind: |
| 234 | if ft.Kind() == reflect.Int32 { |
| 235 | return coderSfixed32NoZero |
| 236 | } |
| 237 | case pref.Fixed32Kind: |
| 238 | if ft.Kind() == reflect.Uint32 { |
| 239 | return coderFixed32NoZero |
| 240 | } |
| 241 | case pref.FloatKind: |
| 242 | if ft.Kind() == reflect.Float32 { |
| 243 | return coderFloatNoZero |
| 244 | } |
| 245 | case pref.Sfixed64Kind: |
| 246 | if ft.Kind() == reflect.Int64 { |
| 247 | return coderSfixed64NoZero |
| 248 | } |
| 249 | case pref.Fixed64Kind: |
| 250 | if ft.Kind() == reflect.Uint64 { |
| 251 | return coderFixed64NoZero |
| 252 | } |
| 253 | case pref.DoubleKind: |
| 254 | if ft.Kind() == reflect.Float64 { |
| 255 | return coderDoubleNoZero |
| 256 | } |
| 257 | case pref.StringKind: |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 258 | if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 259 | return coderStringNoZeroValidateUTF8 |
| 260 | } |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 261 | if ft.Kind() == reflect.String { |
| 262 | return coderStringNoZero |
| 263 | } |
| 264 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) { |
| 265 | return coderBytesNoZeroValidateUTF8 |
| 266 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 267 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
| 268 | return coderBytesNoZero |
| 269 | } |
| 270 | case pref.BytesKind: |
| 271 | if ft.Kind() == reflect.String { |
| 272 | return coderStringNoZero |
| 273 | } |
| 274 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
| 275 | return coderBytesNoZero |
| 276 | } |
| 277 | } |
| 278 | case ft.Kind() == reflect.Ptr: |
| 279 | ft := ft.Elem() |
| 280 | switch fd.Kind() { |
| 281 | case pref.BoolKind: |
| 282 | if ft.Kind() == reflect.Bool { |
| 283 | return coderBoolPtr |
| 284 | } |
| 285 | case pref.EnumKind: |
| 286 | if ft.Kind() == reflect.Int32 { |
| 287 | return coderEnumPtr |
| 288 | } |
| 289 | case pref.Int32Kind: |
| 290 | if ft.Kind() == reflect.Int32 { |
| 291 | return coderInt32Ptr |
| 292 | } |
| 293 | case pref.Sint32Kind: |
| 294 | if ft.Kind() == reflect.Int32 { |
| 295 | return coderSint32Ptr |
| 296 | } |
| 297 | case pref.Uint32Kind: |
| 298 | if ft.Kind() == reflect.Uint32 { |
| 299 | return coderUint32Ptr |
| 300 | } |
| 301 | case pref.Int64Kind: |
| 302 | if ft.Kind() == reflect.Int64 { |
| 303 | return coderInt64Ptr |
| 304 | } |
| 305 | case pref.Sint64Kind: |
| 306 | if ft.Kind() == reflect.Int64 { |
| 307 | return coderSint64Ptr |
| 308 | } |
| 309 | case pref.Uint64Kind: |
| 310 | if ft.Kind() == reflect.Uint64 { |
| 311 | return coderUint64Ptr |
| 312 | } |
| 313 | case pref.Sfixed32Kind: |
| 314 | if ft.Kind() == reflect.Int32 { |
| 315 | return coderSfixed32Ptr |
| 316 | } |
| 317 | case pref.Fixed32Kind: |
| 318 | if ft.Kind() == reflect.Uint32 { |
| 319 | return coderFixed32Ptr |
| 320 | } |
| 321 | case pref.FloatKind: |
| 322 | if ft.Kind() == reflect.Float32 { |
| 323 | return coderFloatPtr |
| 324 | } |
| 325 | case pref.Sfixed64Kind: |
| 326 | if ft.Kind() == reflect.Int64 { |
| 327 | return coderSfixed64Ptr |
| 328 | } |
| 329 | case pref.Fixed64Kind: |
| 330 | if ft.Kind() == reflect.Uint64 { |
| 331 | return coderFixed64Ptr |
| 332 | } |
| 333 | case pref.DoubleKind: |
| 334 | if ft.Kind() == reflect.Float64 { |
| 335 | return coderDoublePtr |
| 336 | } |
| 337 | case pref.StringKind: |
| 338 | if ft.Kind() == reflect.String { |
| 339 | return coderStringPtr |
| 340 | } |
| 341 | case pref.BytesKind: |
| 342 | if ft.Kind() == reflect.String { |
| 343 | return coderStringPtr |
| 344 | } |
| 345 | } |
| 346 | default: |
| 347 | switch fd.Kind() { |
| 348 | case pref.BoolKind: |
| 349 | if ft.Kind() == reflect.Bool { |
| 350 | return coderBool |
| 351 | } |
| 352 | case pref.EnumKind: |
| 353 | if ft.Kind() == reflect.Int32 { |
| 354 | return coderEnum |
| 355 | } |
| 356 | case pref.Int32Kind: |
| 357 | if ft.Kind() == reflect.Int32 { |
| 358 | return coderInt32 |
| 359 | } |
| 360 | case pref.Sint32Kind: |
| 361 | if ft.Kind() == reflect.Int32 { |
| 362 | return coderSint32 |
| 363 | } |
| 364 | case pref.Uint32Kind: |
| 365 | if ft.Kind() == reflect.Uint32 { |
| 366 | return coderUint32 |
| 367 | } |
| 368 | case pref.Int64Kind: |
| 369 | if ft.Kind() == reflect.Int64 { |
| 370 | return coderInt64 |
| 371 | } |
| 372 | case pref.Sint64Kind: |
| 373 | if ft.Kind() == reflect.Int64 { |
| 374 | return coderSint64 |
| 375 | } |
| 376 | case pref.Uint64Kind: |
| 377 | if ft.Kind() == reflect.Uint64 { |
| 378 | return coderUint64 |
| 379 | } |
| 380 | case pref.Sfixed32Kind: |
| 381 | if ft.Kind() == reflect.Int32 { |
| 382 | return coderSfixed32 |
| 383 | } |
| 384 | case pref.Fixed32Kind: |
| 385 | if ft.Kind() == reflect.Uint32 { |
| 386 | return coderFixed32 |
| 387 | } |
| 388 | case pref.FloatKind: |
| 389 | if ft.Kind() == reflect.Float32 { |
| 390 | return coderFloat |
| 391 | } |
| 392 | case pref.Sfixed64Kind: |
| 393 | if ft.Kind() == reflect.Int64 { |
| 394 | return coderSfixed64 |
| 395 | } |
| 396 | case pref.Fixed64Kind: |
| 397 | if ft.Kind() == reflect.Uint64 { |
| 398 | return coderFixed64 |
| 399 | } |
| 400 | case pref.DoubleKind: |
| 401 | if ft.Kind() == reflect.Float64 { |
| 402 | return coderDouble |
| 403 | } |
| 404 | case pref.StringKind: |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 405 | if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 406 | return coderStringValidateUTF8 |
| 407 | } |
| 408 | if ft.Kind() == reflect.String { |
| 409 | return coderString |
| 410 | } |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 411 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) { |
| 412 | return coderBytesValidateUTF8 |
| 413 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 414 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
| 415 | return coderBytes |
| 416 | } |
| 417 | case pref.BytesKind: |
| 418 | if ft.Kind() == reflect.String { |
| 419 | return coderString |
| 420 | } |
| 421 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
| 422 | return coderBytes |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | panic(fmt.Errorf("invalid type: no encoder for %v %v %v/%v", fd.FullName(), fd.Cardinality(), fd.Kind(), ft)) |
| 427 | } |
| 428 | |
| 429 | // encoderFuncsForValue returns interface{} value functions for a field, used for |
| 430 | // extension values and map encoding. |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 431 | func encoderFuncsForValue(fd pref.FieldDescriptor, ft reflect.Type) valueCoderFuncs { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 432 | switch { |
| 433 | case fd.Cardinality() == pref.Repeated && !fd.IsPacked(): |
| 434 | if ft.Kind() != reflect.Ptr || ft.Elem().Kind() != reflect.Slice { |
| 435 | break |
| 436 | } |
| 437 | ft := ft.Elem().Elem() |
| 438 | switch fd.Kind() { |
| 439 | case pref.BoolKind: |
| 440 | if ft.Kind() == reflect.Bool { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 441 | return coderBoolSliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 442 | } |
| 443 | case pref.EnumKind: |
| 444 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 445 | return coderEnumSliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 446 | } |
| 447 | case pref.Int32Kind: |
| 448 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 449 | return coderInt32SliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 450 | } |
| 451 | case pref.Sint32Kind: |
| 452 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 453 | return coderSint32SliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 454 | } |
| 455 | case pref.Uint32Kind: |
| 456 | if ft.Kind() == reflect.Uint32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 457 | return coderUint32SliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 458 | } |
| 459 | case pref.Int64Kind: |
| 460 | if ft.Kind() == reflect.Int64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 461 | return coderInt64SliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 462 | } |
| 463 | case pref.Sint64Kind: |
| 464 | if ft.Kind() == reflect.Int64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 465 | return coderSint64SliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 466 | } |
| 467 | case pref.Uint64Kind: |
| 468 | if ft.Kind() == reflect.Uint64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 469 | return coderUint64SliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 470 | } |
| 471 | case pref.Sfixed32Kind: |
| 472 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 473 | return coderSfixed32SliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 474 | } |
| 475 | case pref.Fixed32Kind: |
| 476 | if ft.Kind() == reflect.Uint32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 477 | return coderFixed32SliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 478 | } |
| 479 | case pref.FloatKind: |
| 480 | if ft.Kind() == reflect.Float32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 481 | return coderFloatSliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 482 | } |
| 483 | case pref.Sfixed64Kind: |
| 484 | if ft.Kind() == reflect.Int64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 485 | return coderSfixed64SliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 486 | } |
| 487 | case pref.Fixed64Kind: |
| 488 | if ft.Kind() == reflect.Uint64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 489 | return coderFixed64SliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 490 | } |
| 491 | case pref.DoubleKind: |
| 492 | if ft.Kind() == reflect.Float64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 493 | return coderDoubleSliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 494 | } |
| 495 | case pref.StringKind: |
| 496 | if ft.Kind() == reflect.String { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 497 | return coderStringSliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 498 | } |
| 499 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 500 | return coderBytesSliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 501 | } |
| 502 | case pref.BytesKind: |
| 503 | if ft.Kind() == reflect.String { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 504 | return coderStringSliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 505 | } |
| 506 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 507 | return coderBytesSliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 508 | } |
| 509 | case pref.MessageKind: |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 510 | return coderMessageSliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 511 | case pref.GroupKind: |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 512 | return coderGroupSliceValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 513 | } |
| 514 | case fd.Cardinality() == pref.Repeated && fd.IsPacked(): |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 515 | if ft.Kind() != reflect.Ptr || ft.Elem().Kind() != reflect.Slice { |
| 516 | break |
| 517 | } |
| 518 | ft := ft.Elem().Elem() |
| 519 | switch fd.Kind() { |
| 520 | case pref.BoolKind: |
| 521 | if ft.Kind() == reflect.Bool { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 522 | return coderBoolPackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 523 | } |
| 524 | case pref.EnumKind: |
| 525 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 526 | return coderEnumPackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 527 | } |
| 528 | case pref.Int32Kind: |
| 529 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 530 | return coderInt32PackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 531 | } |
| 532 | case pref.Sint32Kind: |
| 533 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 534 | return coderSint32PackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 535 | } |
| 536 | case pref.Uint32Kind: |
| 537 | if ft.Kind() == reflect.Uint32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 538 | return coderUint32PackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 539 | } |
| 540 | case pref.Int64Kind: |
| 541 | if ft.Kind() == reflect.Int64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 542 | return coderInt64PackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 543 | } |
| 544 | case pref.Sint64Kind: |
| 545 | if ft.Kind() == reflect.Int64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 546 | return coderSint64PackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 547 | } |
| 548 | case pref.Uint64Kind: |
| 549 | if ft.Kind() == reflect.Uint64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 550 | return coderUint64PackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 551 | } |
| 552 | case pref.Sfixed32Kind: |
| 553 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 554 | return coderSfixed32PackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 555 | } |
| 556 | case pref.Fixed32Kind: |
| 557 | if ft.Kind() == reflect.Uint32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 558 | return coderFixed32PackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 559 | } |
| 560 | case pref.FloatKind: |
| 561 | if ft.Kind() == reflect.Float32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 562 | return coderFloatPackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 563 | } |
| 564 | case pref.Sfixed64Kind: |
| 565 | if ft.Kind() == reflect.Int64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 566 | return coderSfixed64PackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 567 | } |
| 568 | case pref.Fixed64Kind: |
| 569 | if ft.Kind() == reflect.Uint64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 570 | return coderFixed64PackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 571 | } |
| 572 | case pref.DoubleKind: |
| 573 | if ft.Kind() == reflect.Float64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 574 | return coderDoublePackedSliceValue |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 575 | } |
| 576 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 577 | default: |
| 578 | switch fd.Kind() { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 579 | default: |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 580 | case pref.BoolKind: |
| 581 | if ft.Kind() == reflect.Bool { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 582 | return coderBoolValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 583 | } |
| 584 | case pref.EnumKind: |
| 585 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 586 | return coderEnumValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 587 | } |
| 588 | case pref.Int32Kind: |
| 589 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 590 | return coderInt32Value |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 591 | } |
| 592 | case pref.Sint32Kind: |
| 593 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 594 | return coderSint32Value |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 595 | } |
| 596 | case pref.Uint32Kind: |
| 597 | if ft.Kind() == reflect.Uint32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 598 | return coderUint32Value |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 599 | } |
| 600 | case pref.Int64Kind: |
| 601 | if ft.Kind() == reflect.Int64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 602 | return coderInt64Value |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 603 | } |
| 604 | case pref.Sint64Kind: |
| 605 | if ft.Kind() == reflect.Int64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 606 | return coderSint64Value |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 607 | } |
| 608 | case pref.Uint64Kind: |
| 609 | if ft.Kind() == reflect.Uint64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 610 | return coderUint64Value |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 611 | } |
| 612 | case pref.Sfixed32Kind: |
| 613 | if ft.Kind() == reflect.Int32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 614 | return coderSfixed32Value |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 615 | } |
| 616 | case pref.Fixed32Kind: |
| 617 | if ft.Kind() == reflect.Uint32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 618 | return coderFixed32Value |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 619 | } |
| 620 | case pref.FloatKind: |
| 621 | if ft.Kind() == reflect.Float32 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 622 | return coderFloatValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 623 | } |
| 624 | case pref.Sfixed64Kind: |
| 625 | if ft.Kind() == reflect.Int64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 626 | return coderSfixed64Value |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 627 | } |
| 628 | case pref.Fixed64Kind: |
| 629 | if ft.Kind() == reflect.Uint64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 630 | return coderFixed64Value |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 631 | } |
| 632 | case pref.DoubleKind: |
| 633 | if ft.Kind() == reflect.Float64 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 634 | return coderDoubleValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 635 | } |
| 636 | case pref.StringKind: |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 637 | if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 638 | return coderStringValueValidateUTF8 |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 639 | } |
| 640 | if ft.Kind() == reflect.String { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 641 | return coderStringValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 642 | } |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 643 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 644 | return coderBytesValueValidateUTF8 |
Joe Tsai | c51e2e0 | 2019-07-13 00:44:41 -0700 | [diff] [blame] | 645 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 646 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 647 | return coderBytesValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 648 | } |
| 649 | case pref.BytesKind: |
| 650 | if ft.Kind() == reflect.String { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 651 | return coderStringValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 652 | } |
| 653 | if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 654 | return coderBytesValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 655 | } |
| 656 | case pref.MessageKind: |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 657 | return coderMessageValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 658 | case pref.GroupKind: |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame^] | 659 | return coderGroupValue |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | panic(fmt.Errorf("invalid type: no encoder for %v %v %v/%v", fd.FullName(), fd.Cardinality(), fd.Kind(), ft)) |
| 663 | } |