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"
|
|
|
|
|
|
|
|
pref "github.com/golang/protobuf/v2/reflect/protoreflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Unwrapper unwraps the value to the underlying value.
|
|
|
|
// This is implemented by Vector and Map.
|
|
|
|
type Unwrapper interface {
|
|
|
|
Unwrap() interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// messageV1 is the protoV1.Message interface.
|
|
|
|
type messageV1 = interface {
|
|
|
|
Reset()
|
|
|
|
String() string
|
|
|
|
ProtoMessage()
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
boolType = reflect.TypeOf(bool(false))
|
|
|
|
int32Type = reflect.TypeOf(int32(0))
|
|
|
|
int64Type = reflect.TypeOf(int64(0))
|
|
|
|
uint32Type = reflect.TypeOf(uint32(0))
|
|
|
|
uint64Type = reflect.TypeOf(uint64(0))
|
|
|
|
float32Type = reflect.TypeOf(float32(0))
|
|
|
|
float64Type = reflect.TypeOf(float64(0))
|
|
|
|
stringType = reflect.TypeOf(string(""))
|
|
|
|
bytesType = reflect.TypeOf([]byte(nil))
|
|
|
|
|
|
|
|
enumIfaceV2 = reflect.TypeOf((*pref.ProtoEnum)(nil)).Elem()
|
|
|
|
messageIfaceV1 = reflect.TypeOf((*messageV1)(nil)).Elem()
|
|
|
|
messageIfaceV2 = reflect.TypeOf((*pref.ProtoMessage)(nil)).Elem()
|
|
|
|
|
|
|
|
byteType = reflect.TypeOf(byte(0))
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
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
|
|
|
return NewLegacyConverter(t, k, nil, nil, nil)
|
2018-11-05 11:42:22 -08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// Legacy enums and messages do not self-report their own protoreflect types.
|
|
|
|
// Thus, the caller needs to provide functions for retrieving those when
|
|
|
|
// a v1 enum or message is encountered.
|
|
|
|
type (
|
|
|
|
enumTypeOf = func(reflect.Type) pref.EnumType
|
|
|
|
messageTypeOf = func(reflect.Type) pref.MessageType
|
|
|
|
messageValueOf = func(reflect.Value) pref.ProtoMessage
|
|
|
|
)
|
|
|
|
|
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
|
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
|
|
|
// using the provided enumTypeOf, messageTypeOf and messageValueOf functions.
|
2018-11-05 11:42:22 -08:00
|
|
|
// The wrapped message must implement Unwrapper.
|
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
|
|
|
func NewLegacyConverter(t reflect.Type, k pref.Kind, etOf enumTypeOf, mtOf messageTypeOf, mvOf messageValueOf) 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:
|
|
|
|
// Handle v2 enums, which must satisfy the proto.Enum interface.
|
|
|
|
if t.Kind() != reflect.Ptr && t.Implements(enumIfaceV2) {
|
|
|
|
et := reflect.Zero(t).Interface().(pref.ProtoEnum).ProtoReflect().Type()
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
e := v.Interface().(pref.ProtoEnum)
|
|
|
|
return pref.ValueOf(e.ProtoReflect().Number())
|
|
|
|
},
|
2018-11-10 14:12:21 -08:00
|
|
|
GoValueOf: func(v pref.Value) reflect.Value {
|
2018-11-09 17:02:57 -08:00
|
|
|
rv := reflect.ValueOf(et.New(v.Enum()))
|
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
|
|
|
EnumType: et,
|
2018-11-05 11:42:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle v1 enums, which we identify as simply a named int32 type.
|
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
|
|
|
if etOf != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 {
|
|
|
|
et := etOf(t)
|
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,
|
|
|
|
IsLegacy: true,
|
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) {
|
|
|
|
mt := reflect.Zero(t).Interface().(pref.ProtoMessage).ProtoReflect().Type()
|
|
|
|
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(v.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.Message())
|
|
|
|
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,
|
2018-11-05 11:42:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle v1 messages, which we need to wrap as a v2 message.
|
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
|
|
|
if mtOf != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
|
|
|
|
mt := mtOf(t)
|
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))
|
|
|
|
}
|
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
|
|
|
return pref.ValueOf(mvOf(v).ProtoReflect())
|
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-05 11:42:22 -08:00
|
|
|
rv := reflect.ValueOf(v.Message().(Unwrapper).Unwrap())
|
|
|
|
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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|