protobuf-go/internal/impl/api_export.go
Damien Neil f1e905b042 all: unify protoV1.ExtensionDesc and proto.ExtensionType
Change protoV1.ExtensionDesc to directly implement ExtensionType
rather than delegating to one.

Unify the previous types protoiface.ExtensionDescV1 and
filetype.Extension in impl.ExtensionInfo. The protoV1.ExtensionDesc
type becomes an alias to ExtensionInfo.

This gives us:

  - Just one implementation of ExtensionType.
  - Generated foopb.E_Ext vars are canonical ExtensionTypes.
  - Generated foopb.E_Ext vars are also v1.ExtensionDescs for backwards
    compatibility.
  - Conversion between legacy and modern representations happens
    transparently when lazily initializing an ExtensionInfo.

Overall, a simplification for users of generated code, since they can
mostly ignore the ExtensionDesc/ExtentionType distinction and use the
same value in either the old or new API.

This is change 3/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: I96ee890541ec11b2412e1a72c9d7b96e4d7f66b4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189563
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-20 21:32:57 +00:00

103 lines
3.1 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)
}
// ExtensionTypeFromDesc returns the v2 protoreflect.ExtensionType for d.
//
// TODO: Remove after updating v1 to no longer call this.
func (Export) ExtensionTypeFromDesc(d *ExtensionInfo) pref.ExtensionType {
return d
}