2019-07-17 16:52:10 -07:00
|
|
|
// 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))
|
|
|
|
)
|
|
|
|
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
var (
|
|
|
|
boolZero = pref.ValueOf(bool(false))
|
|
|
|
int32Zero = pref.ValueOf(int32(0))
|
|
|
|
int64Zero = pref.ValueOf(int64(0))
|
|
|
|
uint32Zero = pref.ValueOf(uint32(0))
|
|
|
|
uint64Zero = pref.ValueOf(uint64(0))
|
|
|
|
float32Zero = pref.ValueOf(float32(0))
|
|
|
|
float64Zero = pref.ValueOf(float64(0))
|
|
|
|
stringZero = pref.ValueOf(string(""))
|
|
|
|
bytesZero = pref.ValueOf([]byte(nil))
|
|
|
|
)
|
|
|
|
|
2019-07-17 16:52:10 -07:00
|
|
|
type scalarConverter struct {
|
|
|
|
goType, pbType reflect.Type
|
|
|
|
def pref.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSingularConverter(t reflect.Type, fd pref.FieldDescriptor) Converter {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
defVal := func(fd pref.FieldDescriptor, zero pref.Value) pref.Value {
|
|
|
|
if fd.Cardinality() == pref.Repeated {
|
|
|
|
// Default isn't defined for repeated fields.
|
|
|
|
return zero
|
|
|
|
}
|
|
|
|
return fd.Default()
|
|
|
|
}
|
2019-07-17 16:52:10 -07:00
|
|
|
switch fd.Kind() {
|
|
|
|
case pref.BoolKind:
|
|
|
|
if t.Kind() == reflect.Bool {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
return &scalarConverter{t, boolType, defVal(fd, boolZero)}
|
2019-07-17 16:52:10 -07:00
|
|
|
}
|
|
|
|
case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
|
|
|
|
if t.Kind() == reflect.Int32 {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
return &scalarConverter{t, int32Type, defVal(fd, int32Zero)}
|
2019-07-17 16:52:10 -07:00
|
|
|
}
|
|
|
|
case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
|
|
|
|
if t.Kind() == reflect.Int64 {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
return &scalarConverter{t, int64Type, defVal(fd, int64Zero)}
|
2019-07-17 16:52:10 -07:00
|
|
|
}
|
|
|
|
case pref.Uint32Kind, pref.Fixed32Kind:
|
|
|
|
if t.Kind() == reflect.Uint32 {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
return &scalarConverter{t, uint32Type, defVal(fd, uint32Zero)}
|
2019-07-17 16:52:10 -07:00
|
|
|
}
|
|
|
|
case pref.Uint64Kind, pref.Fixed64Kind:
|
|
|
|
if t.Kind() == reflect.Uint64 {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
return &scalarConverter{t, uint64Type, defVal(fd, uint64Zero)}
|
2019-07-17 16:52:10 -07:00
|
|
|
}
|
|
|
|
case pref.FloatKind:
|
|
|
|
if t.Kind() == reflect.Float32 {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
return &scalarConverter{t, float32Type, defVal(fd, float32Zero)}
|
2019-07-17 16:52:10 -07:00
|
|
|
}
|
|
|
|
case pref.DoubleKind:
|
|
|
|
if t.Kind() == reflect.Float64 {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
return &scalarConverter{t, float64Type, defVal(fd, float64Zero)}
|
2019-07-17 16:52:10 -07:00
|
|
|
}
|
|
|
|
case pref.StringKind:
|
|
|
|
if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
return &scalarConverter{t, stringType, defVal(fd, stringZero)}
|
2019-07-17 16:52:10 -07:00
|
|
|
}
|
|
|
|
case pref.BytesKind:
|
|
|
|
if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
return &scalarConverter{t, bytesType, defVal(fd, bytesZero)}
|
2019-07-17 16:52:10 -07:00
|
|
|
}
|
|
|
|
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 {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
if c.pbType == bytesType {
|
|
|
|
return pref.ValueOf(append(([]byte)(nil), c.def.Bytes()...))
|
|
|
|
}
|
2019-07-17 16:52:10 -07:00
|
|
|
return c.def
|
|
|
|
}
|
|
|
|
|
|
|
|
type enumConverter struct {
|
|
|
|
goType reflect.Type
|
|
|
|
def pref.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEnumConverter(goType reflect.Type, fd pref.FieldDescriptor) Converter {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 10:48:38 -07:00
|
|
|
var def pref.Value
|
|
|
|
if fd.Cardinality() == pref.Repeated {
|
|
|
|
def = pref.ValueOf(fd.Enum().Values().Get(0).Number())
|
|
|
|
} else {
|
|
|
|
def = fd.Default()
|
|
|
|
}
|
|
|
|
return &enumConverter{goType, def}
|
2019-07-17 16:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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()))
|
|
|
|
}
|