2019-07-17 23:52:10 +00: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"
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2019-07-17 23:52:10 +00:00
|
|
|
)
|
|
|
|
|
2019-09-03 23:30:39 +00:00
|
|
|
// unwrapper unwraps the value to the underlying value.
|
2019-07-17 23:52:10 +00:00
|
|
|
// This is implemented by List and Map.
|
2019-09-03 23:30:39 +00:00
|
|
|
type unwrapper interface {
|
2024-05-15 11:11:52 +00:00
|
|
|
protoUnwrap() any
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Converter coverts to/from Go reflect.Value types and protobuf protoreflect.Value types.
|
|
|
|
type Converter interface {
|
all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.
Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.
Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.
Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)
Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-07 19:21:41 +00:00
|
|
|
// PBValueOf converts a reflect.Value to a protoreflect.Value.
|
2022-05-23 20:12:23 +00:00
|
|
|
PBValueOf(reflect.Value) protoreflect.Value
|
all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.
Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.
Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.
Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)
Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-07 19:21:41 +00:00
|
|
|
|
|
|
|
// GoValueOf converts a protoreflect.Value to a reflect.Value.
|
2022-05-23 20:12:23 +00:00
|
|
|
GoValueOf(protoreflect.Value) reflect.Value
|
all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.
Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.
Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.
Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)
Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-07 19:21:41 +00:00
|
|
|
|
2019-08-29 21:08:28 +00:00
|
|
|
// IsValidPB returns whether a protoreflect.Value is compatible with this type.
|
2022-05-23 20:12:23 +00:00
|
|
|
IsValidPB(protoreflect.Value) bool
|
2019-08-29 21:08:28 +00:00
|
|
|
|
|
|
|
// IsValidGo returns whether a reflect.Value is compatible with this type.
|
|
|
|
IsValidGo(reflect.Value) bool
|
|
|
|
|
all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.
Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.
Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.
Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)
Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-07 19:21:41 +00:00
|
|
|
// New returns a new field value.
|
|
|
|
// For scalars, it returns the default value of the field.
|
|
|
|
// For composite types, it returns a new mutable value.
|
2022-05-23 20:12:23 +00:00
|
|
|
New() protoreflect.Value
|
all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.
Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.
Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.
Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)
Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-07 19:21:41 +00:00
|
|
|
|
|
|
|
// Zero returns a new field value.
|
|
|
|
// For scalars, it returns the default value of the field.
|
|
|
|
// For composite types, it returns an immutable, empty value.
|
2022-05-23 20:12:23 +00:00
|
|
|
Zero() protoreflect.Value
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-05-23 20:12:23 +00:00
|
|
|
func NewConverter(t reflect.Type, fd protoreflect.FieldDescriptor) Converter {
|
2019-07-17 23:52:10 +00:00
|
|
|
switch {
|
|
|
|
case fd.IsList():
|
|
|
|
return newListConverter(t, fd)
|
|
|
|
case fd.IsMap():
|
|
|
|
return newMapConverter(t, fd)
|
|
|
|
default:
|
|
|
|
return newSingularConverter(t, fd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 17:48:38 +00:00
|
|
|
var (
|
2022-05-23 20:12:23 +00:00
|
|
|
boolZero = protoreflect.ValueOfBool(false)
|
|
|
|
int32Zero = protoreflect.ValueOfInt32(0)
|
|
|
|
int64Zero = protoreflect.ValueOfInt64(0)
|
|
|
|
uint32Zero = protoreflect.ValueOfUint32(0)
|
|
|
|
uint64Zero = protoreflect.ValueOfUint64(0)
|
|
|
|
float32Zero = protoreflect.ValueOfFloat32(0)
|
|
|
|
float64Zero = protoreflect.ValueOfFloat64(0)
|
|
|
|
stringZero = protoreflect.ValueOfString("")
|
|
|
|
bytesZero = protoreflect.ValueOfBytes(nil)
|
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 17:48:38 +00:00
|
|
|
)
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func newSingularConverter(t reflect.Type, fd protoreflect.FieldDescriptor) Converter {
|
|
|
|
defVal := func(fd protoreflect.FieldDescriptor, zero protoreflect.Value) protoreflect.Value {
|
|
|
|
if fd.Cardinality() == protoreflect.Repeated {
|
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 17:48:38 +00:00
|
|
|
// Default isn't defined for repeated fields.
|
|
|
|
return zero
|
|
|
|
}
|
|
|
|
return fd.Default()
|
|
|
|
}
|
2019-07-17 23:52:10 +00:00
|
|
|
switch fd.Kind() {
|
2022-05-23 20:12:23 +00:00
|
|
|
case protoreflect.BoolKind:
|
2019-07-17 23:52:10 +00:00
|
|
|
if t.Kind() == reflect.Bool {
|
2019-08-23 00:01:56 +00:00
|
|
|
return &boolConverter{t, defVal(fd, boolZero)}
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
|
2019-07-17 23:52:10 +00:00
|
|
|
if t.Kind() == reflect.Int32 {
|
2019-08-23 00:01:56 +00:00
|
|
|
return &int32Converter{t, defVal(fd, int32Zero)}
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
|
2019-07-17 23:52:10 +00:00
|
|
|
if t.Kind() == reflect.Int64 {
|
2019-08-23 00:01:56 +00:00
|
|
|
return &int64Converter{t, defVal(fd, int64Zero)}
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
|
2019-07-17 23:52:10 +00:00
|
|
|
if t.Kind() == reflect.Uint32 {
|
2019-08-23 00:01:56 +00:00
|
|
|
return &uint32Converter{t, defVal(fd, uint32Zero)}
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
|
2019-07-17 23:52:10 +00:00
|
|
|
if t.Kind() == reflect.Uint64 {
|
2019-08-23 00:01:56 +00:00
|
|
|
return &uint64Converter{t, defVal(fd, uint64Zero)}
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
case protoreflect.FloatKind:
|
2019-07-17 23:52:10 +00:00
|
|
|
if t.Kind() == reflect.Float32 {
|
2019-08-23 00:01:56 +00:00
|
|
|
return &float32Converter{t, defVal(fd, float32Zero)}
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
case protoreflect.DoubleKind:
|
2019-07-17 23:52:10 +00:00
|
|
|
if t.Kind() == reflect.Float64 {
|
2019-08-23 00:01:56 +00:00
|
|
|
return &float64Converter{t, defVal(fd, float64Zero)}
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
case protoreflect.StringKind:
|
2019-07-17 23:52:10 +00:00
|
|
|
if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
|
2019-08-23 00:01:56 +00:00
|
|
|
return &stringConverter{t, defVal(fd, stringZero)}
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
case protoreflect.BytesKind:
|
2019-07-17 23:52:10 +00:00
|
|
|
if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
|
2019-08-23 00:01:56 +00:00
|
|
|
return &bytesConverter{t, defVal(fd, bytesZero)}
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
case protoreflect.EnumKind:
|
2019-07-17 23:52:10 +00:00
|
|
|
// Handle enums, which must be a named int32 type.
|
|
|
|
if t.Kind() == reflect.Int32 {
|
|
|
|
return newEnumConverter(t, fd)
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
case protoreflect.MessageKind, protoreflect.GroupKind:
|
2019-07-17 23:52:10 +00:00
|
|
|
return newMessageConverter(t)
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("invalid Go type %v for field %v", t, fd.FullName()))
|
|
|
|
}
|
|
|
|
|
2019-08-23 00:01:56 +00:00
|
|
|
type boolConverter struct {
|
|
|
|
goType reflect.Type
|
2022-05-23 20:12:23 +00:00
|
|
|
def protoreflect.Value
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *boolConverter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-07-17 23:52:10 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfBool(v.Bool())
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *boolConverter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
return reflect.ValueOf(v.Bool()).Convert(c.goType)
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *boolConverter) IsValidPB(v protoreflect.Value) bool {
|
2019-08-29 21:08:28 +00:00
|
|
|
_, ok := v.Interface().(bool)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (c *boolConverter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *boolConverter) New() protoreflect.Value { return c.def }
|
|
|
|
func (c *boolConverter) Zero() protoreflect.Value { return c.def }
|
2019-08-23 00:01:56 +00:00
|
|
|
|
|
|
|
type int32Converter struct {
|
|
|
|
goType reflect.Type
|
2022-05-23 20:12:23 +00:00
|
|
|
def protoreflect.Value
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *int32Converter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfInt32(int32(v.Int()))
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *int32Converter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
return reflect.ValueOf(int32(v.Int())).Convert(c.goType)
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *int32Converter) IsValidPB(v protoreflect.Value) bool {
|
2019-08-29 21:08:28 +00:00
|
|
|
_, ok := v.Interface().(int32)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (c *int32Converter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *int32Converter) New() protoreflect.Value { return c.def }
|
|
|
|
func (c *int32Converter) Zero() protoreflect.Value { return c.def }
|
2019-07-17 23:52:10 +00:00
|
|
|
|
2019-08-23 00:01:56 +00:00
|
|
|
type int64Converter struct {
|
|
|
|
goType reflect.Type
|
2022-05-23 20:12:23 +00:00
|
|
|
def protoreflect.Value
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *int64Converter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfInt64(int64(v.Int()))
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *int64Converter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
return reflect.ValueOf(int64(v.Int())).Convert(c.goType)
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *int64Converter) IsValidPB(v protoreflect.Value) bool {
|
2019-08-29 21:08:28 +00:00
|
|
|
_, ok := v.Interface().(int64)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (c *int64Converter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *int64Converter) New() protoreflect.Value { return c.def }
|
|
|
|
func (c *int64Converter) Zero() protoreflect.Value { return c.def }
|
2019-08-23 00:01:56 +00:00
|
|
|
|
|
|
|
type uint32Converter struct {
|
|
|
|
goType reflect.Type
|
2022-05-23 20:12:23 +00:00
|
|
|
def protoreflect.Value
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *uint32Converter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfUint32(uint32(v.Uint()))
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *uint32Converter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
return reflect.ValueOf(uint32(v.Uint())).Convert(c.goType)
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *uint32Converter) IsValidPB(v protoreflect.Value) bool {
|
2019-08-29 21:08:28 +00:00
|
|
|
_, ok := v.Interface().(uint32)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (c *uint32Converter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *uint32Converter) New() protoreflect.Value { return c.def }
|
|
|
|
func (c *uint32Converter) Zero() protoreflect.Value { return c.def }
|
2019-08-23 00:01:56 +00:00
|
|
|
|
|
|
|
type uint64Converter struct {
|
|
|
|
goType reflect.Type
|
2022-05-23 20:12:23 +00:00
|
|
|
def protoreflect.Value
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *uint64Converter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
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 17:48:38 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfUint64(uint64(v.Uint()))
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *uint64Converter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
return reflect.ValueOf(uint64(v.Uint())).Convert(c.goType)
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *uint64Converter) IsValidPB(v protoreflect.Value) bool {
|
2019-08-29 21:08:28 +00:00
|
|
|
_, ok := v.Interface().(uint64)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (c *uint64Converter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *uint64Converter) New() protoreflect.Value { return c.def }
|
|
|
|
func (c *uint64Converter) Zero() protoreflect.Value { return c.def }
|
2019-07-17 23:52:10 +00:00
|
|
|
|
2019-08-23 00:01:56 +00:00
|
|
|
type float32Converter struct {
|
|
|
|
goType reflect.Type
|
2022-05-23 20:12:23 +00:00
|
|
|
def protoreflect.Value
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *float32Converter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfFloat32(float32(v.Float()))
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *float32Converter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
return reflect.ValueOf(float32(v.Float())).Convert(c.goType)
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *float32Converter) IsValidPB(v protoreflect.Value) bool {
|
2019-08-29 21:08:28 +00:00
|
|
|
_, ok := v.Interface().(float32)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (c *float32Converter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *float32Converter) New() protoreflect.Value { return c.def }
|
|
|
|
func (c *float32Converter) Zero() protoreflect.Value { return c.def }
|
2019-08-23 00:01:56 +00:00
|
|
|
|
|
|
|
type float64Converter struct {
|
|
|
|
goType reflect.Type
|
2022-05-23 20:12:23 +00:00
|
|
|
def protoreflect.Value
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *float64Converter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfFloat64(float64(v.Float()))
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *float64Converter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
return reflect.ValueOf(float64(v.Float())).Convert(c.goType)
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *float64Converter) IsValidPB(v protoreflect.Value) bool {
|
2019-08-29 21:08:28 +00:00
|
|
|
_, ok := v.Interface().(float64)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (c *float64Converter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *float64Converter) New() protoreflect.Value { return c.def }
|
|
|
|
func (c *float64Converter) Zero() protoreflect.Value { return c.def }
|
2019-08-23 00:01:56 +00:00
|
|
|
|
|
|
|
type stringConverter struct {
|
|
|
|
goType reflect.Type
|
2022-05-23 20:12:23 +00:00
|
|
|
def protoreflect.Value
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *stringConverter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfString(v.Convert(stringType).String())
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *stringConverter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2024-08-20 11:59:54 +00:00
|
|
|
// protoreflect.Value.String never panics, so we go through an interface
|
2019-08-23 00:01:56 +00:00
|
|
|
// conversion here to check the type.
|
|
|
|
s := v.Interface().(string)
|
|
|
|
if c.goType.Kind() == reflect.Slice && s == "" {
|
|
|
|
return reflect.Zero(c.goType) // ensure empty string is []byte(nil)
|
|
|
|
}
|
|
|
|
return reflect.ValueOf(s).Convert(c.goType)
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *stringConverter) IsValidPB(v protoreflect.Value) bool {
|
2019-08-29 21:08:28 +00:00
|
|
|
_, ok := v.Interface().(string)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (c *stringConverter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *stringConverter) New() protoreflect.Value { return c.def }
|
|
|
|
func (c *stringConverter) Zero() protoreflect.Value { return c.def }
|
2019-08-23 00:01:56 +00:00
|
|
|
|
|
|
|
type bytesConverter struct {
|
|
|
|
goType reflect.Type
|
2022-05-23 20:12:23 +00:00
|
|
|
def protoreflect.Value
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *bytesConverter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
|
|
|
}
|
|
|
|
if c.goType.Kind() == reflect.String && v.Len() == 0 {
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfBytes(nil) // ensure empty string is []byte(nil)
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfBytes(v.Convert(bytesType).Bytes())
|
2019-08-23 00:01:56 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *bytesConverter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2019-08-23 00:01:56 +00:00
|
|
|
return reflect.ValueOf(v.Bytes()).Convert(c.goType)
|
all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.
Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.
Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.
Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)
Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-07 19:21:41 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *bytesConverter) IsValidPB(v protoreflect.Value) bool {
|
2019-08-29 21:08:28 +00:00
|
|
|
_, ok := v.Interface().([]byte)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (c *bytesConverter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *bytesConverter) New() protoreflect.Value { return c.def }
|
|
|
|
func (c *bytesConverter) Zero() protoreflect.Value { return c.def }
|
all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.
Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.
Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.
Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)
Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-07 19:21:41 +00:00
|
|
|
|
2019-07-17 23:52:10 +00:00
|
|
|
type enumConverter struct {
|
|
|
|
goType reflect.Type
|
2022-05-23 20:12:23 +00:00
|
|
|
def protoreflect.Value
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func newEnumConverter(goType reflect.Type, fd protoreflect.FieldDescriptor) Converter {
|
|
|
|
var def protoreflect.Value
|
|
|
|
if fd.Cardinality() == protoreflect.Repeated {
|
|
|
|
def = protoreflect.ValueOfEnum(fd.Enum().Values().Get(0).Number())
|
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 17:48:38 +00:00
|
|
|
} else {
|
|
|
|
def = fd.Default()
|
|
|
|
}
|
|
|
|
return &enumConverter{goType, def}
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *enumConverter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-07-17 23:52:10 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfEnum(protoreflect.EnumNumber(v.Int()))
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *enumConverter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2019-07-17 23:52:10 +00:00
|
|
|
return reflect.ValueOf(v.Enum()).Convert(c.goType)
|
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *enumConverter) IsValidPB(v protoreflect.Value) bool {
|
|
|
|
_, ok := v.Interface().(protoreflect.EnumNumber)
|
2019-08-29 21:08:28 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *enumConverter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *enumConverter) New() protoreflect.Value {
|
2019-07-17 23:52:10 +00:00
|
|
|
return c.def
|
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *enumConverter) Zero() protoreflect.Value {
|
all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.
Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.
Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.
Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)
Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-07 19:21:41 +00:00
|
|
|
return c.def
|
|
|
|
}
|
|
|
|
|
2019-07-17 23:52:10 +00:00
|
|
|
type messageConverter struct {
|
|
|
|
goType reflect.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMessageConverter(goType reflect.Type) Converter {
|
|
|
|
return &messageConverter{goType}
|
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *messageConverter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-07-17 23:52:10 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
|
|
|
}
|
internal/impl: add runtime support for aberrant messages
Implement support in the protobuf runtime to better understand
message types that are not generated by the official generator.
In particular:
* Add a best-effort implementation of protobuf reflection for
"non-nullable" fields which are supposed to be represented by *T,
but are instead represented by a T. "Non-nullable" message fields
report presence based on whether the message is the zero Go value.
* We do NOT implement support for "non-nullable" fields in the
table-driven implementation since we assume that the aberrant messages
that we care about have a Marshal and Unmarshal method.
* We better handle custom messages that implement Marshal and Unmarshal,
but do NOT implement Merge. In that case, we implement merge in terms of
a back-to-back marshal and unmarshal.
* We better tolerate the situations where a protobuf message field
cannot be mapped to a Go struct field since the latter is missing.
In such cases, reflection treats the field as if it were unpopulated.
Setting such fields will panic.
This change allows the runtime to handle all message types declared
in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection,
Marshal, Unmarshal, Reset, Merge, and Equal all work.
The only types that still do not fully work are:
* "k8s.io/api/authentication/v1".ExtraValue
* "k8s.io/api/authentication/v1beta1".ExtraValue
* "k8s.io/api/authorization/v1".ExtraValue
* "k8s.io/api/authorization/v1beta1".ExtraValue
* "k8s.io/api/certificates/v1".ExtraValue
* "k8s.io/api/certificates/v1beta1".ExtraValue
* "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime
* "k8s.io/apimachinery/pkg/apis/meta/v1".Time
* "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs
While Marshal, Unmarshal, Reset, and Merge continue to work,
protobuf reflection and any functionality that depends on it
(e.g., prototext, protojson, Equal, etc.) will not work.
Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869
Trust: Joe Tsai <joetsai@digital-static.net>
Trust: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 10:50:41 +00:00
|
|
|
if c.isNonPointer() {
|
|
|
|
if v.CanAddr() {
|
|
|
|
v = v.Addr() // T => *T
|
|
|
|
} else {
|
|
|
|
v = reflect.Zero(reflect.PtrTo(v.Type()))
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
if m, ok := v.Interface().(protoreflect.ProtoMessage); ok {
|
|
|
|
return protoreflect.ValueOfMessage(m.ProtoReflect())
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfMessage(legacyWrapMessage(v))
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *messageConverter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2019-07-17 23:52:10 +00:00
|
|
|
m := v.Message()
|
|
|
|
var rv reflect.Value
|
2019-09-03 23:30:39 +00:00
|
|
|
if u, ok := m.(unwrapper); ok {
|
|
|
|
rv = reflect.ValueOf(u.protoUnwrap())
|
2019-07-17 23:52:10 +00:00
|
|
|
} else {
|
|
|
|
rv = reflect.ValueOf(m.Interface())
|
|
|
|
}
|
internal/impl: add runtime support for aberrant messages
Implement support in the protobuf runtime to better understand
message types that are not generated by the official generator.
In particular:
* Add a best-effort implementation of protobuf reflection for
"non-nullable" fields which are supposed to be represented by *T,
but are instead represented by a T. "Non-nullable" message fields
report presence based on whether the message is the zero Go value.
* We do NOT implement support for "non-nullable" fields in the
table-driven implementation since we assume that the aberrant messages
that we care about have a Marshal and Unmarshal method.
* We better handle custom messages that implement Marshal and Unmarshal,
but do NOT implement Merge. In that case, we implement merge in terms of
a back-to-back marshal and unmarshal.
* We better tolerate the situations where a protobuf message field
cannot be mapped to a Go struct field since the latter is missing.
In such cases, reflection treats the field as if it were unpopulated.
Setting such fields will panic.
This change allows the runtime to handle all message types declared
in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection,
Marshal, Unmarshal, Reset, Merge, and Equal all work.
The only types that still do not fully work are:
* "k8s.io/api/authentication/v1".ExtraValue
* "k8s.io/api/authentication/v1beta1".ExtraValue
* "k8s.io/api/authorization/v1".ExtraValue
* "k8s.io/api/authorization/v1beta1".ExtraValue
* "k8s.io/api/certificates/v1".ExtraValue
* "k8s.io/api/certificates/v1beta1".ExtraValue
* "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime
* "k8s.io/apimachinery/pkg/apis/meta/v1".Time
* "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs
While Marshal, Unmarshal, Reset, and Merge continue to work,
protobuf reflection and any functionality that depends on it
(e.g., prototext, protojson, Equal, etc.) will not work.
Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869
Trust: Joe Tsai <joetsai@digital-static.net>
Trust: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 10:50:41 +00:00
|
|
|
if c.isNonPointer() {
|
|
|
|
if rv.Type() != reflect.PtrTo(c.goType) {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), reflect.PtrTo(c.goType)))
|
|
|
|
}
|
|
|
|
if !rv.IsNil() {
|
|
|
|
rv = rv.Elem() // *T => T
|
|
|
|
} else {
|
|
|
|
rv = reflect.Zero(rv.Type().Elem())
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 23:52:10 +00:00
|
|
|
if rv.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), c.goType))
|
|
|
|
}
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *messageConverter) IsValidPB(v protoreflect.Value) bool {
|
2019-08-29 21:08:28 +00:00
|
|
|
m := v.Message()
|
|
|
|
var rv reflect.Value
|
2019-09-03 23:30:39 +00:00
|
|
|
if u, ok := m.(unwrapper); ok {
|
|
|
|
rv = reflect.ValueOf(u.protoUnwrap())
|
2019-08-29 21:08:28 +00:00
|
|
|
} else {
|
|
|
|
rv = reflect.ValueOf(m.Interface())
|
|
|
|
}
|
internal/impl: add runtime support for aberrant messages
Implement support in the protobuf runtime to better understand
message types that are not generated by the official generator.
In particular:
* Add a best-effort implementation of protobuf reflection for
"non-nullable" fields which are supposed to be represented by *T,
but are instead represented by a T. "Non-nullable" message fields
report presence based on whether the message is the zero Go value.
* We do NOT implement support for "non-nullable" fields in the
table-driven implementation since we assume that the aberrant messages
that we care about have a Marshal and Unmarshal method.
* We better handle custom messages that implement Marshal and Unmarshal,
but do NOT implement Merge. In that case, we implement merge in terms of
a back-to-back marshal and unmarshal.
* We better tolerate the situations where a protobuf message field
cannot be mapped to a Go struct field since the latter is missing.
In such cases, reflection treats the field as if it were unpopulated.
Setting such fields will panic.
This change allows the runtime to handle all message types declared
in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection,
Marshal, Unmarshal, Reset, Merge, and Equal all work.
The only types that still do not fully work are:
* "k8s.io/api/authentication/v1".ExtraValue
* "k8s.io/api/authentication/v1beta1".ExtraValue
* "k8s.io/api/authorization/v1".ExtraValue
* "k8s.io/api/authorization/v1beta1".ExtraValue
* "k8s.io/api/certificates/v1".ExtraValue
* "k8s.io/api/certificates/v1beta1".ExtraValue
* "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime
* "k8s.io/apimachinery/pkg/apis/meta/v1".Time
* "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs
While Marshal, Unmarshal, Reset, and Merge continue to work,
protobuf reflection and any functionality that depends on it
(e.g., prototext, protojson, Equal, etc.) will not work.
Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869
Trust: Joe Tsai <joetsai@digital-static.net>
Trust: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 10:50:41 +00:00
|
|
|
if c.isNonPointer() {
|
|
|
|
return rv.Type() == reflect.PtrTo(c.goType)
|
|
|
|
}
|
2019-08-29 21:08:28 +00:00
|
|
|
return rv.Type() == c.goType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *messageConverter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *messageConverter) New() protoreflect.Value {
|
internal/impl: add runtime support for aberrant messages
Implement support in the protobuf runtime to better understand
message types that are not generated by the official generator.
In particular:
* Add a best-effort implementation of protobuf reflection for
"non-nullable" fields which are supposed to be represented by *T,
but are instead represented by a T. "Non-nullable" message fields
report presence based on whether the message is the zero Go value.
* We do NOT implement support for "non-nullable" fields in the
table-driven implementation since we assume that the aberrant messages
that we care about have a Marshal and Unmarshal method.
* We better handle custom messages that implement Marshal and Unmarshal,
but do NOT implement Merge. In that case, we implement merge in terms of
a back-to-back marshal and unmarshal.
* We better tolerate the situations where a protobuf message field
cannot be mapped to a Go struct field since the latter is missing.
In such cases, reflection treats the field as if it were unpopulated.
Setting such fields will panic.
This change allows the runtime to handle all message types declared
in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection,
Marshal, Unmarshal, Reset, Merge, and Equal all work.
The only types that still do not fully work are:
* "k8s.io/api/authentication/v1".ExtraValue
* "k8s.io/api/authentication/v1beta1".ExtraValue
* "k8s.io/api/authorization/v1".ExtraValue
* "k8s.io/api/authorization/v1beta1".ExtraValue
* "k8s.io/api/certificates/v1".ExtraValue
* "k8s.io/api/certificates/v1beta1".ExtraValue
* "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime
* "k8s.io/apimachinery/pkg/apis/meta/v1".Time
* "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs
While Marshal, Unmarshal, Reset, and Merge continue to work,
protobuf reflection and any functionality that depends on it
(e.g., prototext, protojson, Equal, etc.) will not work.
Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869
Trust: Joe Tsai <joetsai@digital-static.net>
Trust: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 10:50:41 +00:00
|
|
|
if c.isNonPointer() {
|
|
|
|
return c.PBValueOf(reflect.New(c.goType).Elem())
|
|
|
|
}
|
2019-07-17 23:52:10 +00:00
|
|
|
return c.PBValueOf(reflect.New(c.goType.Elem()))
|
|
|
|
}
|
all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.
Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.
Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.
Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)
Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-07 19:21:41 +00:00
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *messageConverter) Zero() protoreflect.Value {
|
all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.
Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.
Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.
Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)
Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-07 19:21:41 +00:00
|
|
|
return c.PBValueOf(reflect.Zero(c.goType))
|
|
|
|
}
|
internal/impl: add runtime support for aberrant messages
Implement support in the protobuf runtime to better understand
message types that are not generated by the official generator.
In particular:
* Add a best-effort implementation of protobuf reflection for
"non-nullable" fields which are supposed to be represented by *T,
but are instead represented by a T. "Non-nullable" message fields
report presence based on whether the message is the zero Go value.
* We do NOT implement support for "non-nullable" fields in the
table-driven implementation since we assume that the aberrant messages
that we care about have a Marshal and Unmarshal method.
* We better handle custom messages that implement Marshal and Unmarshal,
but do NOT implement Merge. In that case, we implement merge in terms of
a back-to-back marshal and unmarshal.
* We better tolerate the situations where a protobuf message field
cannot be mapped to a Go struct field since the latter is missing.
In such cases, reflection treats the field as if it were unpopulated.
Setting such fields will panic.
This change allows the runtime to handle all message types declared
in the "go.etcd.io/etcd" and "k8s.io" modules where protobuf reflection,
Marshal, Unmarshal, Reset, Merge, and Equal all work.
The only types that still do not fully work are:
* "k8s.io/api/authentication/v1".ExtraValue
* "k8s.io/api/authentication/v1beta1".ExtraValue
* "k8s.io/api/authorization/v1".ExtraValue
* "k8s.io/api/authorization/v1beta1".ExtraValue
* "k8s.io/api/certificates/v1".ExtraValue
* "k8s.io/api/certificates/v1beta1".ExtraValue
* "k8s.io/apimachinery/pkg/apis/meta/v1".MicroTime
* "k8s.io/apimachinery/pkg/apis/meta/v1".Time
* "k8s.io/apimachinery/pkg/apis/meta/v1".Verbs
While Marshal, Unmarshal, Reset, and Merge continue to work,
protobuf reflection and any functionality that depends on it
(e.g., prototext, protojson, Equal, etc.) will not work.
Change-Id: I67a9d2f1bec35248045ad0c16220d02fc2e0e172
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/300869
Trust: Joe Tsai <joetsai@digital-static.net>
Trust: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
2021-03-11 10:50:41 +00:00
|
|
|
|
|
|
|
// isNonPointer reports whether the type is a non-pointer type.
|
|
|
|
// This never occurs for generated message types.
|
|
|
|
func (c *messageConverter) isNonPointer() bool {
|
|
|
|
return c.goType.Kind() != reflect.Ptr
|
|
|
|
}
|