2018-11-05 11:42:22 -08: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 value provides functionality for wrapping Go values to implement
|
|
|
|
// protoreflect values.
|
|
|
|
package value
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2019-05-13 23:55:40 -07:00
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
piface "google.golang.org/protobuf/runtime/protoiface"
|
2018-11-05 11:42:22 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Unwrapper unwraps the value to the underlying value.
|
all: rename Vector as List
The terminology Vector does not occur in protobuf documentation at all,
so we should rename the Go use of the term to something more recognizable.
As such, all instances that match the regexp "[Vv]ect(or)?" were replaced.
The C++ documentation uses the term "Repeated", which is a reasonable name.
However, the term became overloaded in 2014, when maps were added as a feature
and implementated under the hood as repeated fields. This is confusing as it
means "repeated" could either refer to repeated fields proper (i.e., explicitly
marked with the "repeated" label in the proto file) or map fields. In the case
of the C++ reflective API, this is not a problem since repeated fields proper
and map fields are interacted with through the same RepeatedField type.
In Go, we do not use a single type to handle both types of repeated fields:
1) We are coming up with the Go protobuf reflection API for the first time
and so do not need to piggy-back on the repeated fields API to remain backwards
compatible since no former usages of Go protobuf reflection exists.
2) Map fields are commonly represented in Go as the Go map type, which do not
preserve ordering information. As such it is fundamentally impossible to present
an unordered map as a consistently ordered list. Thus, Go needs two different
interfaces for lists and maps.
Given the above situation, "Repeated" is not a great term to use since it
refers to two different things (when we only want one of the meanings).
To distinguish between the two, we'll use the terms "List" and "Map" instead.
There is some precedence for the term "List" in the protobuf codebase
(e.g., "getRepeatedInt32List").
Change-Id: Iddcdb6b78e1e60c14fa4ca213c15f45e214b967b
Reviewed-on: https://go-review.googlesource.com/c/149657
Reviewed-by: Damien Neil <dneil@google.com>
2018-11-14 14:05:19 -08:00
|
|
|
// This is implemented by List and Map.
|
2018-11-05 11:42:22 -08:00
|
|
|
type Unwrapper interface {
|
2018-11-29 14:54:05 -08:00
|
|
|
ProtoUnwrap() interface{}
|
2018-11-05 11:42:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
2019-01-08 16:18:07 -08:00
|
|
|
enumIfaceV2 = reflect.TypeOf((*pref.Enum)(nil)).Elem()
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
messageIfaceV1 = reflect.TypeOf((*piface.MessageV1)(nil)).Elem()
|
2018-11-05 11:42:22 -08:00
|
|
|
messageIfaceV2 = reflect.TypeOf((*pref.ProtoMessage)(nil)).Elem()
|
|
|
|
|
|
|
|
byteType = reflect.TypeOf(byte(0))
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewConverter matches a Go type with a protobuf kind and returns a Converter
|
|
|
|
// that converts between the two. NewConverter panics if it unable to provide a
|
|
|
|
// conversion between the two. The Converter methods also panic when they are
|
|
|
|
// called on incorrect Go types.
|
|
|
|
//
|
|
|
|
// 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, k pref.Kind) Converter {
|
2018-11-26 22:32:06 -08:00
|
|
|
return NewLegacyConverter(t, k, nil)
|
2018-11-05 11:42:22 -08:00
|
|
|
}
|
|
|
|
|
2018-11-26 22:32:06 -08:00
|
|
|
// LegacyWrapper is a set of wrapper methods that wraps legacy v1 Go types
|
|
|
|
// to implement the v2 reflection APIs.
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
type (
|
2018-11-26 22:32:06 -08:00
|
|
|
LegacyWrapper interface {
|
|
|
|
EnumOf(interface{}) LegacyEnum
|
|
|
|
EnumTypeOf(interface{}) pref.EnumType
|
2019-05-01 12:29:25 -07:00
|
|
|
EnumDescriptorOf(interface{}) pref.EnumDescriptor
|
2018-11-26 22:32:06 -08:00
|
|
|
|
|
|
|
MessageOf(interface{}) LegacyMessage
|
|
|
|
MessageTypeOf(interface{}) pref.MessageType
|
2019-05-01 12:29:25 -07:00
|
|
|
MessageDescriptorOf(interface{}) pref.MessageDescriptor
|
2018-11-26 22:32:06 -08:00
|
|
|
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
// TODO: Remove these eventually.
|
|
|
|
// See the TODOs in internal/impl/legacy_extension.go.
|
|
|
|
ExtensionDescFromType(pref.ExtensionType) *piface.ExtensionDescV1
|
|
|
|
ExtensionTypeFromDesc(*piface.ExtensionDescV1) pref.ExtensionType
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
LegacyEnum = interface {
|
|
|
|
pref.Enum
|
|
|
|
ProtoUnwrap() interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
LegacyMessage = interface {
|
|
|
|
pref.Message
|
|
|
|
ProtoUnwrap() interface{}
|
|
|
|
}
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
)
|
|
|
|
|
2018-11-05 11:42:22 -08:00
|
|
|
// NewLegacyConverter is identical to NewConverter,
|
|
|
|
// but supports wrapping legacy v1 messages to implement the v2 message API
|
2018-11-26 22:32:06 -08:00
|
|
|
// using the provided LegacyWrapper.
|
|
|
|
func NewLegacyConverter(t reflect.Type, k pref.Kind, w LegacyWrapper) Converter {
|
2018-11-05 11:42:22 -08:00
|
|
|
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:
|
2019-05-01 12:29:25 -07:00
|
|
|
// Handle enums, which must be a named int32 type.
|
|
|
|
if t.PkgPath() != "" && t.Kind() == reflect.Int32 {
|
|
|
|
var et pref.EnumType
|
|
|
|
if t.Implements(enumIfaceV2) {
|
|
|
|
et = &enumType{reflect.Zero(t).Interface().(pref.Enum).Descriptor(), t}
|
|
|
|
} else {
|
|
|
|
et = w.EnumTypeOf(reflect.Zero(t).Interface())
|
2018-11-05 11:42:22 -08:00
|
|
|
}
|
|
|
|
return Converter{
|
2018-11-10 14:12:21 -08:00
|
|
|
PBValueOf: func(v reflect.Value) pref.Value {
|
2018-11-05 11:42:22 -08:00
|
|
|
if v.Type() != t {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
|
|
|
|
}
|
|
|
|
return pref.ValueOf(pref.EnumNumber(v.Int()))
|
|
|
|
},
|
2018-11-10 14:12:21 -08:00
|
|
|
GoValueOf: func(v pref.Value) reflect.Value {
|
2018-11-05 11:42:22 -08:00
|
|
|
return reflect.ValueOf(v.Enum()).Convert(t)
|
|
|
|
},
|
2018-11-10 14:12:21 -08:00
|
|
|
EnumType: et,
|
2019-05-01 12:29:25 -07:00
|
|
|
IsLegacy: !t.Implements(enumIfaceV2),
|
2018-11-05 11:42:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case pref.MessageKind, pref.GroupKind:
|
|
|
|
// Handle v2 messages, which must satisfy the proto.Message interface.
|
|
|
|
if t.Kind() == reflect.Ptr && t.Implements(messageIfaceV2) {
|
2019-05-01 12:29:25 -07:00
|
|
|
md := reflect.Zero(t).Interface().(pref.ProtoMessage).ProtoReflect().Descriptor()
|
|
|
|
mt := &messageType{md, t}
|
2019-05-15 11:39:13 -06:00
|
|
|
return NewMessageConverter(mt)
|
2018-11-05 11:42:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle v1 messages, which we need to wrap as a v2 message.
|
2018-11-26 22:32:06 -08:00
|
|
|
if w != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
|
|
|
|
mt := w.MessageTypeOf(reflect.Zero(t).Interface())
|
2018-11-05 11:42:22 -08:00
|
|
|
return Converter{
|
2018-11-10 14:12:21 -08:00
|
|
|
PBValueOf: func(v reflect.Value) pref.Value {
|
2018-11-05 11:42:22 -08:00
|
|
|
if v.Type() != t {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
|
|
|
|
}
|
2018-11-26 22:32:06 -08:00
|
|
|
return pref.ValueOf(w.MessageOf(v.Interface()))
|
2018-11-05 11:42:22 -08:00
|
|
|
},
|
2018-11-10 14:12:21 -08:00
|
|
|
GoValueOf: func(v pref.Value) reflect.Value {
|
2018-11-29 14:54:05 -08:00
|
|
|
rv := reflect.ValueOf(v.Message().(Unwrapper).ProtoUnwrap())
|
2018-11-05 11:42:22 -08:00
|
|
|
if rv.Type() != t {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t))
|
|
|
|
}
|
|
|
|
return rv
|
|
|
|
},
|
2018-11-10 14:12:21 -08:00
|
|
|
MessageType: mt,
|
|
|
|
IsLegacy: true,
|
2018-11-05 11:42:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("invalid Go type %v for protobuf kind %v", t, k))
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeScalarConverter(goType, pbType reflect.Type) Converter {
|
|
|
|
return Converter{
|
2018-11-10 14:12:21 -08:00
|
|
|
PBValueOf: func(v reflect.Value) pref.Value {
|
2018-11-05 11:42:22 -08:00
|
|
|
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())
|
|
|
|
},
|
2018-11-10 14:12:21 -08:00
|
|
|
GoValueOf: func(v pref.Value) reflect.Value {
|
2018-11-05 11:42:22 -08:00
|
|
|
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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 11:39:13 -06:00
|
|
|
// NewEnumConverter returns a converter for an EnumType, whose GoType must implement protoreflect.Enum.
|
|
|
|
func NewEnumConverter(et pref.EnumType) Converter {
|
|
|
|
t := et.GoType()
|
|
|
|
if !t.Implements(enumIfaceV2) {
|
|
|
|
panic(fmt.Sprintf("invalid type: %v does not implement %v", t, enumIfaceV2))
|
|
|
|
}
|
|
|
|
return Converter{
|
|
|
|
PBValueOf: func(v reflect.Value) pref.Value {
|
|
|
|
if v.Type() != t {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
|
|
|
|
}
|
|
|
|
e := v.Interface().(pref.Enum)
|
|
|
|
return pref.ValueOf(e.Number())
|
|
|
|
},
|
|
|
|
GoValueOf: func(v pref.Value) reflect.Value {
|
|
|
|
return reflect.ValueOf(et.New(v.Enum()))
|
|
|
|
},
|
|
|
|
EnumType: et,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessageConverter returns a converter for a MessageType, whose GoType must implement protoreflect.ProtoMessage.
|
|
|
|
func NewMessageConverter(mt pref.MessageType) Converter {
|
|
|
|
t := mt.GoType()
|
|
|
|
if !t.Implements(messageIfaceV2) {
|
|
|
|
panic(fmt.Sprintf("invalid type: %v does not implement %v", t, messageIfaceV2))
|
|
|
|
}
|
|
|
|
return Converter{
|
|
|
|
PBValueOf: func(v reflect.Value) pref.Value {
|
|
|
|
if v.Type() != t {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
|
|
|
|
}
|
|
|
|
return pref.ValueOf(v.Interface().(pref.ProtoMessage).ProtoReflect())
|
|
|
|
},
|
|
|
|
GoValueOf: func(v pref.Value) reflect.Value {
|
|
|
|
rv := reflect.ValueOf(v.Message().Interface())
|
|
|
|
if rv.Type() != t {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t))
|
|
|
|
}
|
|
|
|
return rv
|
|
|
|
},
|
|
|
|
MessageType: mt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 11:42:22 -08:00
|
|
|
// Converter provides functions for converting to/from Go reflect.Value types
|
|
|
|
// and protobuf protoreflect.Value types.
|
|
|
|
type Converter struct {
|
2018-11-10 14:12:21 -08:00
|
|
|
PBValueOf func(reflect.Value) pref.Value
|
|
|
|
GoValueOf func(pref.Value) reflect.Value
|
|
|
|
EnumType pref.EnumType
|
|
|
|
MessageType pref.MessageType
|
|
|
|
IsLegacy bool
|
2018-11-05 11:42:22 -08:00
|
|
|
}
|
2019-05-01 12:29:25 -07:00
|
|
|
|
|
|
|
// TODO: This needs to be centralized in a package.
|
|
|
|
type enumType struct {
|
|
|
|
// TODO: Remove me as an embedded field.
|
|
|
|
pref.EnumDescriptor
|
|
|
|
typ reflect.Type // must implement protoreflect.Enum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *enumType) Descriptor() pref.EnumDescriptor { return t.EnumDescriptor }
|
|
|
|
func (t *enumType) GoType() reflect.Type { return t.typ }
|
|
|
|
func (t *enumType) New(n pref.EnumNumber) pref.Enum {
|
|
|
|
return reflect.ValueOf(n).Convert(t.typ).Interface().(pref.Enum)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This needs to be centralized in a package.
|
|
|
|
type messageType struct {
|
|
|
|
// TODO: Remove me as an embedded field.
|
|
|
|
pref.MessageDescriptor
|
|
|
|
typ reflect.Type // must implement protoreflect.ProtoMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *messageType) Descriptor() pref.MessageDescriptor { return t.MessageDescriptor }
|
|
|
|
func (t *messageType) GoType() reflect.Type { return t.typ }
|
|
|
|
func (t *messageType) New() pref.Message {
|
|
|
|
return reflect.New(t.typ.Elem()).Interface().(pref.ProtoMessage).ProtoReflect()
|
|
|
|
}
|