blob: a149612d4a83399f61ebc14bdc2ad5a443e2c94f [file] [log] [blame]
Joe Tsai08e00302018-11-26 22:32:06 -08001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package impl
6
7import (
Joe Tsai0fc49f82019-05-01 12:29:25 -07008 "reflect"
Joe Tsai8e506a82019-03-16 00:05:34 -07009 "strconv"
10
Damien Neil5c5b5312019-05-14 12:44:37 -070011 "google.golang.org/protobuf/encoding/prototext"
Damien Neile89e6242019-05-13 23:55:40 -070012 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsai21ade492019-05-22 13:42:54 -040013 piface "google.golang.org/protobuf/runtime/protoiface"
Joe Tsai08e00302018-11-26 22:32:06 -080014)
15
16// Export is a zero-length named type that exists only to export a set of
17// functions that we do not want to appear in godoc.
18type Export struct{}
19
Joe Tsai0fc49f82019-05-01 12:29:25 -070020// enum is any enum type generated by protoc-gen-go
21// and must be a named int32 type.
22type enum = interface{}
23
Joe Tsai08e00302018-11-26 22:32:06 -080024// EnumOf returns the protoreflect.Enum interface over e.
Joe Tsai0fc49f82019-05-01 12:29:25 -070025func (Export) EnumOf(e enum) pref.Enum {
Damien Neila8593ba2019-01-08 16:18:07 -080026 if ev, ok := e.(pref.Enum); ok {
27 return ev
Joe Tsai08e00302018-11-26 22:32:06 -080028 }
Joe Tsai21ade492019-05-22 13:42:54 -040029 return legacyWrapEnum(reflect.ValueOf(e))
Joe Tsai08e00302018-11-26 22:32:06 -080030}
31
32// EnumTypeOf returns the protoreflect.EnumType for e.
Joe Tsai0fc49f82019-05-01 12:29:25 -070033func (Export) EnumTypeOf(e enum) pref.EnumType {
Damien Neila8593ba2019-01-08 16:18:07 -080034 if ev, ok := e.(pref.Enum); ok {
Damien Neil16163b42019-08-06 15:43:25 -070035 return ev.Type()
Joe Tsai08e00302018-11-26 22:32:06 -080036 }
Joe Tsai21ade492019-05-22 13:42:54 -040037 return legacyLoadEnumType(reflect.TypeOf(e))
Joe Tsai08e00302018-11-26 22:32:06 -080038}
39
Joe Tsai0fc49f82019-05-01 12:29:25 -070040// EnumDescriptorOf returns the protoreflect.EnumDescriptor for e.
41func (Export) EnumDescriptorOf(e enum) pref.EnumDescriptor {
42 if ev, ok := e.(pref.Enum); ok {
43 return ev.Descriptor()
44 }
Joe Tsai21ade492019-05-22 13:42:54 -040045 return LegacyLoadEnumDesc(reflect.TypeOf(e))
Joe Tsai0fc49f82019-05-01 12:29:25 -070046}
47
Joe Tsai8e506a82019-03-16 00:05:34 -070048// EnumStringOf returns the enum value as a string, either as the name if
49// the number is resolvable, or the number formatted as a string.
50func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
51 ev := ed.Values().ByNumber(n)
52 if ev != nil {
53 return string(ev.Name())
54 }
55 return strconv.Itoa(int(n))
56}
57
Joe Tsai0fc49f82019-05-01 12:29:25 -070058// message is any message type generated by protoc-gen-go
59// and must be a pointer to a named struct type.
60type message = interface{}
61
Joe Tsai08e00302018-11-26 22:32:06 -080062// MessageOf returns the protoreflect.Message interface over m.
Joe Tsai0fc49f82019-05-01 12:29:25 -070063func (Export) MessageOf(m message) pref.Message {
Joe Tsai08e00302018-11-26 22:32:06 -080064 if mv, ok := m.(pref.ProtoMessage); ok {
65 return mv.ProtoReflect()
66 }
Joe Tsai21ade492019-05-22 13:42:54 -040067 return legacyWrapMessage(reflect.ValueOf(m)).ProtoReflect()
Joe Tsai08e00302018-11-26 22:32:06 -080068}
69
70// MessageTypeOf returns the protoreflect.MessageType for m.
Joe Tsai0fc49f82019-05-01 12:29:25 -070071func (Export) MessageTypeOf(m message) pref.MessageType {
Joe Tsai08e00302018-11-26 22:32:06 -080072 if mv, ok := m.(pref.ProtoMessage); ok {
Damien Neil16163b42019-08-06 15:43:25 -070073 return mv.ProtoReflect().Type()
Joe Tsai08e00302018-11-26 22:32:06 -080074 }
Damien Neil16163b42019-08-06 15:43:25 -070075 return legacyLoadMessageInfo(reflect.TypeOf(m))
Joe Tsai08e00302018-11-26 22:32:06 -080076}
77
Joe Tsai0fc49f82019-05-01 12:29:25 -070078// MessageDescriptorOf returns the protoreflect.MessageDescriptor for m.
79func (Export) MessageDescriptorOf(m message) pref.MessageDescriptor {
80 if mv, ok := m.(pref.ProtoMessage); ok {
81 return mv.ProtoReflect().Descriptor()
Joe Tsai08e00302018-11-26 22:32:06 -080082 }
Joe Tsai21ade492019-05-22 13:42:54 -040083 return LegacyLoadMessageDesc(reflect.TypeOf(m))
Joe Tsai08e00302018-11-26 22:32:06 -080084}
Joe Tsai1321a0e2019-03-20 09:46:22 -070085
86// MessageStringOf returns the message value as a string,
87// which is the message serialized in the protobuf text format.
88func (Export) MessageStringOf(m pref.ProtoMessage) string {
Damien Neil5c5b5312019-05-14 12:44:37 -070089 b, _ := prototext.MarshalOptions{AllowPartial: true}.Marshal(m)
Joe Tsai1321a0e2019-03-20 09:46:22 -070090 return string(b)
91}
Joe Tsai21ade492019-05-22 13:42:54 -040092
93// ExtensionDescFromType returns the legacy protoiface.ExtensionDescV1 for t.
94func (Export) ExtensionDescFromType(t pref.ExtensionType) *piface.ExtensionDescV1 {
95 return legacyExtensionDescFromType(t)
96}
97
98// ExtensionTypeFromDesc returns the v2 protoreflect.ExtensionType for d.
99func (Export) ExtensionTypeFromDesc(d *piface.ExtensionDescV1) pref.ExtensionType {
100 return legacyExtensionTypeFromDesc(d)
101}