| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- // Copyright 2018 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"
- "github.com/golang/protobuf/v2/internal/flags"
- pref "github.com/golang/protobuf/v2/reflect/protoreflect"
- )
- type fieldInfo struct {
- // TODO: specialize marshal and unmarshal functions?
- has func(pointer) bool
- get func(pointer) pref.Value
- set func(pointer, pref.Value)
- clear func(pointer)
- mutable func(pointer) pref.Mutable
- }
- func fieldInfoForWeak(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
- if !flags.Proto1Legacy {
- panic("weak fields not supported")
- }
- // TODO: support weak fields.
- panic(fmt.Sprintf("invalid field: %v", fd))
- }
- func fieldInfoForOneof(fd pref.FieldDescriptor, fs reflect.StructField, ot reflect.Type) fieldInfo {
- // TODO: support oneof fields.
- panic(fmt.Sprintf("invalid field: %v", fd))
- }
- func fieldInfoForMap(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
- // TODO: support map fields.
- panic(fmt.Sprintf("invalid field: %v", fd))
- }
- func fieldInfoForVector(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
- // TODO: support vector fields.
- panic(fmt.Sprintf("invalid field: %v", fd))
- }
- var emptyBytes = reflect.ValueOf([]byte{})
- func fieldInfoForScalar(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
- ft := fs.Type
- nullable := fd.Syntax() == pref.Proto2
- if nullable {
- if ft.Kind() != reflect.Ptr && ft.Kind() != reflect.Slice {
- panic(fmt.Sprintf("invalid type: got %v, want pointer", ft))
- }
- if ft.Kind() == reflect.Ptr {
- ft = ft.Elem()
- }
- }
- conv := matchGoTypePBKind(ft, fd.Kind())
- fieldOffset := offsetOf(fs)
- // TODO: Implement unsafe fast path?
- return fieldInfo{
- has: func(p pointer) bool {
- rv := p.apply(fieldOffset).asType(fs.Type).Elem()
- if nullable {
- return !rv.IsNil()
- }
- switch rv.Kind() {
- case reflect.Bool:
- return rv.Bool()
- case reflect.Int32, reflect.Int64:
- return rv.Int() > 0
- case reflect.Uint32, reflect.Uint64:
- return rv.Uint() > 0
- case reflect.Float32, reflect.Float64:
- return rv.Float() > 0
- case reflect.String, reflect.Slice:
- return rv.Len() > 0
- default:
- panic(fmt.Sprintf("invalid type: %v", rv.Type())) // should never happen
- }
- },
- get: func(p pointer) pref.Value {
- rv := p.apply(fieldOffset).asType(fs.Type).Elem()
- if nullable {
- if rv.IsNil() {
- pv := fd.Default()
- if fd.Kind() == pref.BytesKind && len(pv.Bytes()) > 0 {
- return pref.ValueOf(append([]byte(nil), pv.Bytes()...)) // copy default bytes for safety
- }
- return pv
- }
- if rv.Kind() == reflect.Ptr {
- rv = rv.Elem()
- }
- }
- return conv.toPB(rv)
- },
- set: func(p pointer, v pref.Value) {
- rv := p.apply(fieldOffset).asType(fs.Type).Elem()
- if nullable && rv.Kind() == reflect.Ptr {
- if rv.IsNil() {
- rv.Set(reflect.New(ft))
- }
- rv = rv.Elem()
- }
- rv.Set(conv.toGo(v))
- if nullable && rv.Kind() == reflect.Slice && rv.IsNil() {
- rv.Set(emptyBytes)
- }
- },
- clear: func(p pointer) {
- rv := p.apply(fieldOffset).asType(fs.Type).Elem()
- rv.Set(reflect.Zero(rv.Type()))
- },
- mutable: func(p pointer) pref.Mutable {
- panic("invalid mutable call")
- },
- }
- }
- func fieldInfoForMessage(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
- // TODO: support vector fields.
- panic(fmt.Sprintf("invalid field: %v", fd))
- }
- // messageV1 is the protoV1.Message interface.
- type messageV1 interface {
- Reset()
- String() string
- ProtoMessage()
- }
- var (
- boolType = reflect.TypeOf(bool(false))
- int32Type = reflect.TypeOf(int32(0))
- int64Type = reflect.TypeOf(int64(0))
- uint32Type = reflect.TypeOf(uint32(0))
- uint64Type = reflect.TypeOf(uint64(0))
- float32Type = reflect.TypeOf(float32(0))
- float64Type = reflect.TypeOf(float64(0))
- stringType = reflect.TypeOf(string(""))
- bytesType = reflect.TypeOf([]byte(nil))
- enumIfaceV2 = reflect.TypeOf((*pref.ProtoEnum)(nil)).Elem()
- messageIfaceV1 = reflect.TypeOf((*messageV1)(nil)).Elem()
- messageIfaceV2 = reflect.TypeOf((*pref.ProtoMessage)(nil)).Elem()
- byteType = reflect.TypeOf(byte(0))
- )
- // matchGoTypePBKind matches a Go type with the protobuf kind.
- //
- // This matcher deliberately supports a wider range of Go types than what
- // protoc-gen-go historically generated to be able to automatically wrap some
- // v1 messages generated by other forks of protoc-gen-go.
- func matchGoTypePBKind(t reflect.Type, k pref.Kind) converter {
- switch k {
- case pref.BoolKind:
- if t.Kind() == reflect.Bool {
- return makeScalarConverter(t, boolType)
- }
- case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
- if t.Kind() == reflect.Int32 {
- return makeScalarConverter(t, int32Type)
- }
- case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
- if t.Kind() == reflect.Int64 {
- return makeScalarConverter(t, int64Type)
- }
- case pref.Uint32Kind, pref.Fixed32Kind:
- if t.Kind() == reflect.Uint32 {
- return makeScalarConverter(t, uint32Type)
- }
- case pref.Uint64Kind, pref.Fixed64Kind:
- if t.Kind() == reflect.Uint64 {
- return makeScalarConverter(t, uint64Type)
- }
- case pref.FloatKind:
- if t.Kind() == reflect.Float32 {
- return makeScalarConverter(t, float32Type)
- }
- case pref.DoubleKind:
- if t.Kind() == reflect.Float64 {
- return makeScalarConverter(t, float64Type)
- }
- case pref.StringKind:
- if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
- return makeScalarConverter(t, stringType)
- }
- case pref.BytesKind:
- if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
- return makeScalarConverter(t, bytesType)
- }
- case pref.EnumKind:
- // Handle v2 enums, which must satisfy the proto.Enum interface.
- if t.Kind() != reflect.Ptr && t.Implements(enumIfaceV2) {
- // TODO: implement this.
- }
- // Handle v1 enums, which we identify as simply a named int32 type.
- if t.Kind() == reflect.Int32 && t.PkgPath() != "" {
- // TODO: need logic to wrap a legacy enum to implement this.
- }
- case pref.MessageKind, pref.GroupKind:
- // Handle v2 messages, which must satisfy the proto.Message interface.
- if t.Kind() == reflect.Ptr && t.Implements(messageIfaceV2) {
- // TODO: implement this.
- }
- // Handle v1 messages, which we need to wrap as a v2 message.
- if t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
- // TODO: need logic to wrap a legacy message.
- }
- }
- panic(fmt.Sprintf("invalid Go type %v for protobuf kind %v", t, k))
- }
- // converter provides functions for converting to/from Go reflect.Value types
- // and protobuf protoreflect.Value types.
- type converter struct {
- toPB func(reflect.Value) pref.Value
- toGo func(pref.Value) reflect.Value
- }
- func makeScalarConverter(goType, pbType reflect.Type) converter {
- return converter{
- toPB: func(v reflect.Value) pref.Value {
- if v.Type() != goType {
- panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), goType))
- }
- if goType.Kind() == reflect.String && pbType.Kind() == reflect.Slice && v.Len() == 0 {
- return pref.ValueOf([]byte(nil)) // ensure empty string is []byte(nil)
- }
- return pref.ValueOf(v.Convert(pbType).Interface())
- },
- toGo: func(v pref.Value) reflect.Value {
- rv := reflect.ValueOf(v.Interface())
- if rv.Type() != pbType {
- panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), pbType))
- }
- if pbType.Kind() == reflect.String && goType.Kind() == reflect.Slice && rv.Len() == 0 {
- return reflect.Zero(goType) // ensure empty string is []byte(nil)
- }
- return rv.Convert(goType)
- },
- }
- }
|