mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-02-11 15:40:44 +00:00
This is change 5/5 in a series of commits changing protoV1.ExtensionDesc to directly implement protoreflect.ExtensionType. 1. [v2] Add protoimpl.ExtensionInfo as an alias for protoiface.ExtensionDescV1. 2. [v1] Update references to protoimpl.ExtensionInfo to use protoiface.ExtensionInfo. 3. [v2] Create protoimpl.ExtensionInfo (an alias to a new type in the impl package) and remove protoiface.ExtensionDescV1. 4. [v1] Remove unneeded explicit conversions between ExtensionDesc and ExtensionType (since the former now directly implements the latter). 5. [v2] Remove stub conversion functions. Change-Id: I1fdfb18c481e72a362a6f9ee0e440f8f909790ca Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189564 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
96 lines
2.8 KiB
Go
96 lines
2.8 KiB
Go
// 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 (
|
|
"reflect"
|
|
"strconv"
|
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
// 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{}
|
|
|
|
// enum is any enum type generated by protoc-gen-go
|
|
// and must be a named int32 type.
|
|
type enum = interface{}
|
|
|
|
// EnumOf returns the protoreflect.Enum interface over e.
|
|
func (Export) EnumOf(e enum) pref.Enum {
|
|
if ev, ok := e.(pref.Enum); ok {
|
|
return ev
|
|
}
|
|
return legacyWrapEnum(reflect.ValueOf(e))
|
|
}
|
|
|
|
// EnumTypeOf returns the protoreflect.EnumType for e.
|
|
func (Export) EnumTypeOf(e enum) pref.EnumType {
|
|
if ev, ok := e.(pref.Enum); ok {
|
|
return ev.Type()
|
|
}
|
|
return legacyLoadEnumType(reflect.TypeOf(e))
|
|
}
|
|
|
|
// EnumDescriptorOf returns the protoreflect.EnumDescriptor for e.
|
|
func (Export) EnumDescriptorOf(e enum) pref.EnumDescriptor {
|
|
if ev, ok := e.(pref.Enum); ok {
|
|
return ev.Descriptor()
|
|
}
|
|
return LegacyLoadEnumDesc(reflect.TypeOf(e))
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
// message is any message type generated by protoc-gen-go
|
|
// and must be a pointer to a named struct type.
|
|
type message = interface{}
|
|
|
|
// MessageOf returns the protoreflect.Message interface over m.
|
|
func (Export) MessageOf(m message) pref.Message {
|
|
if mv, ok := m.(pref.ProtoMessage); ok {
|
|
return mv.ProtoReflect()
|
|
}
|
|
return legacyWrapMessage(reflect.ValueOf(m)).ProtoReflect()
|
|
}
|
|
|
|
// MessageTypeOf returns the protoreflect.MessageType for m.
|
|
func (Export) MessageTypeOf(m message) pref.MessageType {
|
|
if mv, ok := m.(pref.ProtoMessage); ok {
|
|
return mv.ProtoReflect().Type()
|
|
}
|
|
return legacyLoadMessageInfo(reflect.TypeOf(m))
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
return LegacyLoadMessageDesc(reflect.TypeOf(m))
|
|
}
|
|
|
|
// 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 {
|
|
b, _ := prototext.MarshalOptions{AllowPartial: true}.Marshal(m)
|
|
return string(b)
|
|
}
|
|
|
|
// ExtensionDescFromType returns the legacy protoV1.ExtensionDesc for t.
|
|
func (Export) ExtensionDescFromType(t pref.ExtensionType) *ExtensionInfo {
|
|
return legacyExtensionDescFromType(t)
|
|
}
|