2022-07-07 20:11:50 +00:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package impl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/encoding/protowire"
|
|
|
|
"google.golang.org/protobuf/internal/strs"
|
2022-08-26 00:05:39 +00:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2022-07-07 20:11:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// pointerCoderFuncs is a set of pointer encoding functions.
|
|
|
|
type pointerCoderFuncs struct {
|
|
|
|
mi *MessageInfo
|
|
|
|
size func(p pointer, f *coderFieldInfo, opts marshalOptions) int
|
|
|
|
marshal func(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error)
|
|
|
|
unmarshal func(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error)
|
|
|
|
isInit func(p pointer, f *coderFieldInfo) error
|
|
|
|
merge func(dst, src pointer, f *coderFieldInfo, opts mergeOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
// valueCoderFuncs is a set of protoreflect.Value encoding functions.
|
|
|
|
type valueCoderFuncs struct {
|
2022-08-26 00:05:39 +00:00
|
|
|
size func(v protoreflect.Value, tagsize int, opts marshalOptions) int
|
|
|
|
marshal func(b []byte, v protoreflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error)
|
|
|
|
unmarshal func(b []byte, v protoreflect.Value, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (protoreflect.Value, unmarshalOutput, error)
|
|
|
|
isInit func(v protoreflect.Value) error
|
|
|
|
merge func(dst, src protoreflect.Value, opts mergeOptions) protoreflect.Value
|
2022-07-07 20:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fieldCoder returns pointer functions for a field, used for operating on
|
|
|
|
// struct fields.
|
2022-08-26 00:05:39 +00:00
|
|
|
func fieldCoder(fd protoreflect.FieldDescriptor, ft reflect.Type) (*MessageInfo, pointerCoderFuncs) {
|
2022-07-07 20:11:50 +00:00
|
|
|
switch {
|
|
|
|
case fd.IsMap():
|
|
|
|
return encoderFuncsForMap(fd, ft)
|
2022-08-26 00:05:39 +00:00
|
|
|
case fd.Cardinality() == protoreflect.Repeated && !fd.IsPacked():
|
2022-07-07 20:11:50 +00:00
|
|
|
// Repeated fields (not packed).
|
|
|
|
if ft.Kind() != reflect.Slice {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ft := ft.Elem()
|
|
|
|
switch fd.Kind() {
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BoolKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Bool {
|
|
|
|
return nil, coderBoolSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.EnumKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderEnumSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderInt32Slice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderSint32Slice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint32 {
|
|
|
|
return nil, coderUint32Slice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderInt64Slice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderSint64Slice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint64 {
|
|
|
|
return nil, coderUint64Slice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderSfixed32Slice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint32 {
|
|
|
|
return nil, coderFixed32Slice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.FloatKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Float32 {
|
|
|
|
return nil, coderFloatSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderSfixed64Slice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint64 {
|
|
|
|
return nil, coderFixed64Slice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.DoubleKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Float64 {
|
|
|
|
return nil, coderDoubleSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.StringKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) {
|
|
|
|
return nil, coderStringSliceValidateUTF8
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.String {
|
|
|
|
return nil, coderStringSlice
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) {
|
|
|
|
return nil, coderBytesSliceValidateUTF8
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
|
|
|
|
return nil, coderBytesSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BytesKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.String {
|
|
|
|
return nil, coderStringSlice
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
|
|
|
|
return nil, coderBytesSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.MessageKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return getMessageInfo(ft), makeMessageSliceFieldCoder(fd, ft)
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.GroupKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return getMessageInfo(ft), makeGroupSliceFieldCoder(fd, ft)
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case fd.Cardinality() == protoreflect.Repeated && fd.IsPacked():
|
2022-07-07 20:11:50 +00:00
|
|
|
// Packed repeated fields.
|
|
|
|
//
|
|
|
|
// Only repeated fields of primitive numeric types
|
|
|
|
// (Varint, Fixed32, or Fixed64 wire type) can be packed.
|
|
|
|
if ft.Kind() != reflect.Slice {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ft := ft.Elem()
|
|
|
|
switch fd.Kind() {
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BoolKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Bool {
|
|
|
|
return nil, coderBoolPackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.EnumKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderEnumPackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderInt32PackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderSint32PackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint32 {
|
|
|
|
return nil, coderUint32PackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderInt64PackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderSint64PackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint64 {
|
|
|
|
return nil, coderUint64PackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderSfixed32PackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint32 {
|
|
|
|
return nil, coderFixed32PackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.FloatKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Float32 {
|
|
|
|
return nil, coderFloatPackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderSfixed64PackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint64 {
|
|
|
|
return nil, coderFixed64PackedSlice
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.DoubleKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Float64 {
|
|
|
|
return nil, coderDoublePackedSlice
|
|
|
|
}
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case fd.Kind() == protoreflect.MessageKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return getMessageInfo(ft), makeMessageFieldCoder(fd, ft)
|
2022-08-26 00:05:39 +00:00
|
|
|
case fd.Kind() == protoreflect.GroupKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return getMessageInfo(ft), makeGroupFieldCoder(fd, ft)
|
2022-08-26 00:05:39 +00:00
|
|
|
case fd.Syntax() == protoreflect.Proto3 && fd.ContainingOneof() == nil:
|
2022-07-07 20:11:50 +00:00
|
|
|
// Populated oneof fields always encode even if set to the zero value,
|
|
|
|
// which normally are not encoded in proto3.
|
|
|
|
switch fd.Kind() {
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BoolKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Bool {
|
|
|
|
return nil, coderBoolNoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.EnumKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderEnumNoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderInt32NoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderSint32NoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint32 {
|
|
|
|
return nil, coderUint32NoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderInt64NoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderSint64NoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint64 {
|
|
|
|
return nil, coderUint64NoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderSfixed32NoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint32 {
|
|
|
|
return nil, coderFixed32NoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.FloatKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Float32 {
|
|
|
|
return nil, coderFloatNoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderSfixed64NoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint64 {
|
|
|
|
return nil, coderFixed64NoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.DoubleKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Float64 {
|
|
|
|
return nil, coderDoubleNoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.StringKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) {
|
|
|
|
return nil, coderStringNoZeroValidateUTF8
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.String {
|
|
|
|
return nil, coderStringNoZero
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) {
|
|
|
|
return nil, coderBytesNoZeroValidateUTF8
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
|
|
|
|
return nil, coderBytesNoZero
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BytesKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.String {
|
|
|
|
return nil, coderStringNoZero
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
|
|
|
|
return nil, coderBytesNoZero
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case ft.Kind() == reflect.Ptr:
|
|
|
|
ft := ft.Elem()
|
|
|
|
switch fd.Kind() {
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BoolKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Bool {
|
|
|
|
return nil, coderBoolPtr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.EnumKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderEnumPtr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderInt32Ptr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderSint32Ptr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint32 {
|
|
|
|
return nil, coderUint32Ptr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderInt64Ptr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderSint64Ptr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint64 {
|
|
|
|
return nil, coderUint64Ptr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderSfixed32Ptr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint32 {
|
|
|
|
return nil, coderFixed32Ptr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.FloatKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Float32 {
|
|
|
|
return nil, coderFloatPtr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderSfixed64Ptr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint64 {
|
|
|
|
return nil, coderFixed64Ptr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.DoubleKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Float64 {
|
|
|
|
return nil, coderDoublePtr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.StringKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) {
|
|
|
|
return nil, coderStringPtrValidateUTF8
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.String {
|
|
|
|
return nil, coderStringPtr
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BytesKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.String {
|
|
|
|
return nil, coderStringPtr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
switch fd.Kind() {
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BoolKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Bool {
|
|
|
|
return nil, coderBool
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.EnumKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderEnum
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderInt32
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderSint32
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint32 {
|
|
|
|
return nil, coderUint32
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderInt64
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderSint64
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint64 {
|
|
|
|
return nil, coderUint64
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int32 {
|
|
|
|
return nil, coderSfixed32
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint32 {
|
|
|
|
return nil, coderFixed32
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.FloatKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Float32 {
|
|
|
|
return nil, coderFloat
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Int64 {
|
|
|
|
return nil, coderSfixed64
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Uint64 {
|
|
|
|
return nil, coderFixed64
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.DoubleKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.Float64 {
|
|
|
|
return nil, coderDouble
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.StringKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) {
|
|
|
|
return nil, coderStringValidateUTF8
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.String {
|
|
|
|
return nil, coderString
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) {
|
|
|
|
return nil, coderBytesValidateUTF8
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
|
|
|
|
return nil, coderBytes
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BytesKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if ft.Kind() == reflect.String {
|
|
|
|
return nil, coderString
|
|
|
|
}
|
|
|
|
if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
|
|
|
|
return nil, coderBytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("invalid type: no encoder for %v %v %v/%v", fd.FullName(), fd.Cardinality(), fd.Kind(), ft))
|
|
|
|
}
|
|
|
|
|
|
|
|
// encoderFuncsForValue returns value functions for a field, used for
|
|
|
|
// extension values and map encoding.
|
2022-08-26 00:05:39 +00:00
|
|
|
func encoderFuncsForValue(fd protoreflect.FieldDescriptor) valueCoderFuncs {
|
2022-07-07 20:11:50 +00:00
|
|
|
switch {
|
2022-08-26 00:05:39 +00:00
|
|
|
case fd.Cardinality() == protoreflect.Repeated && !fd.IsPacked():
|
2022-07-07 20:11:50 +00:00
|
|
|
switch fd.Kind() {
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BoolKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderBoolSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.EnumKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderEnumSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderInt32SliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSint32SliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderUint32SliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderInt64SliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSint64SliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderUint64SliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSfixed32SliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderFixed32SliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.FloatKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderFloatSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSfixed64SliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderFixed64SliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.DoubleKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderDoubleSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.StringKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
// We don't have a UTF-8 validating coder for repeated string fields.
|
|
|
|
// Value coders are used for extensions and maps.
|
|
|
|
// Extensions are never proto3, and maps never contain lists.
|
|
|
|
return coderStringSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BytesKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderBytesSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.MessageKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderMessageSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.GroupKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderGroupSliceValue
|
|
|
|
}
|
2022-08-26 00:05:39 +00:00
|
|
|
case fd.Cardinality() == protoreflect.Repeated && fd.IsPacked():
|
2022-07-07 20:11:50 +00:00
|
|
|
switch fd.Kind() {
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BoolKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderBoolPackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.EnumKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderEnumPackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderInt32PackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSint32PackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderUint32PackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderInt64PackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSint64PackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderUint64PackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSfixed32PackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderFixed32PackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.FloatKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderFloatPackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSfixed64PackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderFixed64PackedSliceValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.DoubleKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderDoublePackedSliceValue
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
switch fd.Kind() {
|
|
|
|
default:
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BoolKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderBoolValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.EnumKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderEnumValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderInt32Value
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSint32Value
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderUint32Value
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Int64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderInt64Value
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSint64Value
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Uint64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderUint64Value
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSfixed32Value
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed32Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderFixed32Value
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.FloatKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderFloatValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Sfixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderSfixed64Value
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.Fixed64Kind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderFixed64Value
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.DoubleKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderDoubleValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.StringKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
if strs.EnforceUTF8(fd) {
|
|
|
|
return coderStringValueValidateUTF8
|
|
|
|
}
|
|
|
|
return coderStringValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.BytesKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderBytesValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.MessageKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderMessageValue
|
2022-08-26 00:05:39 +00:00
|
|
|
case protoreflect.GroupKind:
|
2022-07-07 20:11:50 +00:00
|
|
|
return coderGroupValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("invalid field: no encoder for %v %v %v", fd.FullName(), fd.Cardinality(), fd.Kind()))
|
|
|
|
}
|