Damien Neil 954bd92c19 all: refactor Converter
A Converter converts between reflect.Values and protoreflect.Values.
The existing usage of Converter is somewhat confusing: The
internal/value package creates Converters for scalar types only, the
internal/impl package creates Converters for legacy messages and enums,
and the reflect/prototype package creates Converters for repeated fields.

Change the Converter type to an interface. The constructor for
Converter takes a FieldDescriptor and reflect.Type, and directly
handles conversions for all field types: Scalars, lists, maps, and
legacy types.

Move Converter into the internal/impl package, since that package
contains the necessary support for dealing with legacy messages and
enums. Drop the internal/value package.

Replace two uses of prototype.Extension with more focused
implementations, since the implementation is trivial with the
refactored Converter. Drop prototype.Extension for the moment since
it is now unused.

Change-Id: If0c570fefac002cc5925b3d56281b6eb17e90d5f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/187857
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 22:33:14 +00:00

198 lines
5.8 KiB
Go

// 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"
pref "google.golang.org/protobuf/reflect/protoreflect"
)
// Unwrapper unwraps the value to the underlying value.
// This is implemented by List and Map.
type Unwrapper interface {
ProtoUnwrap() interface{}
}
// A Converter coverts to/from Go reflect.Value types and protobuf protoreflect.Value types.
type Converter interface {
PBValueOf(reflect.Value) pref.Value
GoValueOf(pref.Value) reflect.Value
New() pref.Value
}
// NewConverter matches a Go type with a protobuf field and returns a Converter
// that converts between the two. Enums must be a named int32 kind that
// implements protoreflect.Enum, and messages must be pointer to a named
// struct type that implements protoreflect.ProtoMessage.
//
// 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 NewConverter(t reflect.Type, fd pref.FieldDescriptor) Converter {
switch {
case fd.IsList():
return newListConverter(t, fd)
case fd.IsMap():
return newMapConverter(t, fd)
default:
return newSingularConverter(t, fd)
}
panic(fmt.Sprintf("invalid Go type %v for field %v", t, fd.FullName()))
}
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))
byteType = reflect.TypeOf(byte(0))
)
type scalarConverter struct {
goType, pbType reflect.Type
def pref.Value
}
func newSingularConverter(t reflect.Type, fd pref.FieldDescriptor) Converter {
switch fd.Kind() {
case pref.BoolKind:
if t.Kind() == reflect.Bool {
return &scalarConverter{t, boolType, fd.Default()}
}
case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
if t.Kind() == reflect.Int32 {
return &scalarConverter{t, int32Type, fd.Default()}
}
case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
if t.Kind() == reflect.Int64 {
return &scalarConverter{t, int64Type, fd.Default()}
}
case pref.Uint32Kind, pref.Fixed32Kind:
if t.Kind() == reflect.Uint32 {
return &scalarConverter{t, uint32Type, fd.Default()}
}
case pref.Uint64Kind, pref.Fixed64Kind:
if t.Kind() == reflect.Uint64 {
return &scalarConverter{t, uint64Type, fd.Default()}
}
case pref.FloatKind:
if t.Kind() == reflect.Float32 {
return &scalarConverter{t, float32Type, fd.Default()}
}
case pref.DoubleKind:
if t.Kind() == reflect.Float64 {
return &scalarConverter{t, float64Type, fd.Default()}
}
case pref.StringKind:
if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
return &scalarConverter{t, stringType, fd.Default()}
}
case pref.BytesKind:
if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
return &scalarConverter{t, bytesType, fd.Default()}
}
case pref.EnumKind:
// Handle enums, which must be a named int32 type.
if t.Kind() == reflect.Int32 {
return newEnumConverter(t, fd)
}
case pref.MessageKind, pref.GroupKind:
return newMessageConverter(t)
}
panic(fmt.Sprintf("invalid Go type %v for field %v", t, fd.FullName()))
}
func (c *scalarConverter) PBValueOf(v reflect.Value) pref.Value {
if v.Type() != c.goType {
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
}
if c.goType.Kind() == reflect.String && c.pbType.Kind() == reflect.Slice && v.Len() == 0 {
return pref.ValueOf([]byte(nil)) // ensure empty string is []byte(nil)
}
return pref.ValueOf(v.Convert(c.pbType).Interface())
}
func (c *scalarConverter) GoValueOf(v pref.Value) reflect.Value {
rv := reflect.ValueOf(v.Interface())
if rv.Type() != c.pbType {
panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), c.pbType))
}
if c.pbType.Kind() == reflect.String && c.goType.Kind() == reflect.Slice && rv.Len() == 0 {
return reflect.Zero(c.goType) // ensure empty string is []byte(nil)
}
return rv.Convert(c.goType)
}
func (c *scalarConverter) New() pref.Value {
return c.def
}
type enumConverter struct {
goType reflect.Type
def pref.Value
}
func newEnumConverter(goType reflect.Type, fd pref.FieldDescriptor) Converter {
return &enumConverter{goType, fd.Default()}
}
func (c *enumConverter) PBValueOf(v reflect.Value) pref.Value {
if v.Type() != c.goType {
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
}
return pref.ValueOf(pref.EnumNumber(v.Int()))
}
func (c *enumConverter) GoValueOf(v pref.Value) reflect.Value {
return reflect.ValueOf(v.Enum()).Convert(c.goType)
}
func (c *enumConverter) New() pref.Value {
return c.def
}
type messageConverter struct {
goType reflect.Type
}
func newMessageConverter(goType reflect.Type) Converter {
return &messageConverter{goType}
}
func (c *messageConverter) PBValueOf(v reflect.Value) pref.Value {
if v.Type() != c.goType {
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
}
if m, ok := v.Interface().(pref.ProtoMessage); ok {
return pref.ValueOf(m.ProtoReflect())
}
return pref.ValueOf(legacyWrapMessage(v).ProtoReflect())
}
func (c *messageConverter) GoValueOf(v pref.Value) reflect.Value {
m := v.Message()
var rv reflect.Value
if u, ok := m.(Unwrapper); ok {
rv = reflect.ValueOf(u.ProtoUnwrap())
} else {
rv = reflect.ValueOf(m.Interface())
}
if rv.Type() != c.goType {
panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), c.goType))
}
return rv
}
func (c *messageConverter) New() pref.Value {
return c.PBValueOf(reflect.New(c.goType.Elem()))
}