2018-11-27 06:32:06 +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 (
|
2019-05-01 19:29:25 +00:00
|
|
|
"reflect"
|
2019-03-16 07:05:34 +00:00
|
|
|
"strconv"
|
|
|
|
|
2019-05-14 19:44:37 +00:00
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
2019-05-14 06:55:40 +00:00
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
2018-11-27 06:32:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Export is a zero-length named type that exists only to export a set of
|
|
|
|
// functions that we do not want to appear in godoc.
|
|
|
|
type Export struct{}
|
|
|
|
|
2019-05-01 19:29:25 +00:00
|
|
|
// enum is any enum type generated by protoc-gen-go
|
|
|
|
// and must be a named int32 type.
|
|
|
|
type enum = interface{}
|
|
|
|
|
2018-11-27 06:32:06 +00:00
|
|
|
// EnumOf returns the protoreflect.Enum interface over e.
|
2019-05-01 19:29:25 +00:00
|
|
|
func (Export) EnumOf(e enum) pref.Enum {
|
2019-01-09 00:18:07 +00:00
|
|
|
if ev, ok := e.(pref.Enum); ok {
|
|
|
|
return ev
|
2018-11-27 06:32:06 +00:00
|
|
|
}
|
2019-05-22 17:42:54 +00:00
|
|
|
return legacyWrapEnum(reflect.ValueOf(e))
|
2018-11-27 06:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EnumTypeOf returns the protoreflect.EnumType for e.
|
2019-05-01 19:29:25 +00:00
|
|
|
func (Export) EnumTypeOf(e enum) pref.EnumType {
|
2019-01-09 00:18:07 +00:00
|
|
|
if ev, ok := e.(pref.Enum); ok {
|
2019-08-06 22:43:25 +00:00
|
|
|
return ev.Type()
|
2018-11-27 06:32:06 +00:00
|
|
|
}
|
2019-05-22 17:42:54 +00:00
|
|
|
return legacyLoadEnumType(reflect.TypeOf(e))
|
2018-11-27 06:32:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 19:29:25 +00:00
|
|
|
// EnumDescriptorOf returns the protoreflect.EnumDescriptor for e.
|
|
|
|
func (Export) EnumDescriptorOf(e enum) pref.EnumDescriptor {
|
|
|
|
if ev, ok := e.(pref.Enum); ok {
|
|
|
|
return ev.Descriptor()
|
|
|
|
}
|
2019-05-22 17:42:54 +00:00
|
|
|
return LegacyLoadEnumDesc(reflect.TypeOf(e))
|
2019-05-01 19:29:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-16 07:05:34 +00:00
|
|
|
// EnumStringOf returns the enum value as a string, either as the name if
|
|
|
|
// the number is resolvable, or the number formatted as a string.
|
|
|
|
func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
|
|
|
|
ev := ed.Values().ByNumber(n)
|
|
|
|
if ev != nil {
|
|
|
|
return string(ev.Name())
|
|
|
|
}
|
|
|
|
return strconv.Itoa(int(n))
|
|
|
|
}
|
|
|
|
|
2019-05-01 19:29:25 +00:00
|
|
|
// message is any message type generated by protoc-gen-go
|
|
|
|
// and must be a pointer to a named struct type.
|
|
|
|
type message = interface{}
|
|
|
|
|
2018-11-27 06:32:06 +00:00
|
|
|
// MessageOf returns the protoreflect.Message interface over m.
|
2019-05-01 19:29:25 +00:00
|
|
|
func (Export) MessageOf(m message) pref.Message {
|
2018-11-27 06:32:06 +00:00
|
|
|
if mv, ok := m.(pref.ProtoMessage); ok {
|
|
|
|
return mv.ProtoReflect()
|
|
|
|
}
|
2019-05-22 17:42:54 +00:00
|
|
|
return legacyWrapMessage(reflect.ValueOf(m)).ProtoReflect()
|
2018-11-27 06:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MessageTypeOf returns the protoreflect.MessageType for m.
|
2019-05-01 19:29:25 +00:00
|
|
|
func (Export) MessageTypeOf(m message) pref.MessageType {
|
2018-11-27 06:32:06 +00:00
|
|
|
if mv, ok := m.(pref.ProtoMessage); ok {
|
2019-08-06 22:43:25 +00:00
|
|
|
return mv.ProtoReflect().Type()
|
2018-11-27 06:32:06 +00:00
|
|
|
}
|
2019-08-06 22:43:25 +00:00
|
|
|
return legacyLoadMessageInfo(reflect.TypeOf(m))
|
2018-11-27 06:32:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 19:29:25 +00:00
|
|
|
// MessageDescriptorOf returns the protoreflect.MessageDescriptor for m.
|
|
|
|
func (Export) MessageDescriptorOf(m message) pref.MessageDescriptor {
|
|
|
|
if mv, ok := m.(pref.ProtoMessage); ok {
|
|
|
|
return mv.ProtoReflect().Descriptor()
|
2018-11-27 06:32:06 +00:00
|
|
|
}
|
2019-05-22 17:42:54 +00:00
|
|
|
return LegacyLoadMessageDesc(reflect.TypeOf(m))
|
2018-11-27 06:32:06 +00:00
|
|
|
}
|
2019-03-20 16:46:22 +00:00
|
|
|
|
|
|
|
// MessageStringOf returns the message value as a string,
|
|
|
|
// which is the message serialized in the protobuf text format.
|
|
|
|
func (Export) MessageStringOf(m pref.ProtoMessage) string {
|
2019-05-14 19:44:37 +00:00
|
|
|
b, _ := prototext.MarshalOptions{AllowPartial: true}.Marshal(m)
|
2019-03-20 16:46:22 +00:00
|
|
|
return string(b)
|
|
|
|
}
|
2019-05-22 17:42:54 +00:00
|
|
|
|
2019-08-08 22:45:59 +00:00
|
|
|
// ExtensionDescFromType returns the legacy protoV1.ExtensionDesc for t.
|
|
|
|
func (Export) ExtensionDescFromType(t pref.ExtensionType) *ExtensionInfo {
|
2019-05-22 17:42:54 +00:00
|
|
|
return legacyExtensionDescFromType(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExtensionTypeFromDesc returns the v2 protoreflect.ExtensionType for d.
|
2019-08-08 22:45:59 +00:00
|
|
|
//
|
|
|
|
// TODO: Remove after updating v1 to no longer call this.
|
|
|
|
func (Export) ExtensionTypeFromDesc(d *ExtensionInfo) pref.ExtensionType {
|
|
|
|
return d
|
2019-05-22 17:42:54 +00:00
|
|
|
}
|