...
1
2
3
4
5 package impl
6
7 import (
8 "fmt"
9 "reflect"
10 "strconv"
11
12 "google.golang.org/protobuf/encoding/prototext"
13 "google.golang.org/protobuf/internal/errors"
14 "google.golang.org/protobuf/proto"
15 "google.golang.org/protobuf/reflect/protoreflect"
16 "google.golang.org/protobuf/runtime/protoiface"
17 )
18
19
20
21 type Export struct{}
22
23
24
25 func (Export) NewError(f string, x ...interface{}) error {
26 return errors.New(f, x...)
27 }
28
29
30
31 type enum = interface{}
32
33
34
35 func (Export) EnumOf(e enum) protoreflect.Enum {
36 switch e := e.(type) {
37 case nil:
38 return nil
39 case protoreflect.Enum:
40 return e
41 default:
42 return legacyWrapEnum(reflect.ValueOf(e))
43 }
44 }
45
46
47
48 func (Export) EnumDescriptorOf(e enum) protoreflect.EnumDescriptor {
49 switch e := e.(type) {
50 case nil:
51 return nil
52 case protoreflect.Enum:
53 return e.Descriptor()
54 default:
55 return LegacyLoadEnumDesc(reflect.TypeOf(e))
56 }
57 }
58
59
60
61 func (Export) EnumTypeOf(e enum) protoreflect.EnumType {
62 switch e := e.(type) {
63 case nil:
64 return nil
65 case protoreflect.Enum:
66 return e.Type()
67 default:
68 return legacyLoadEnumType(reflect.TypeOf(e))
69 }
70 }
71
72
73
74 func (Export) EnumStringOf(ed protoreflect.EnumDescriptor, n protoreflect.EnumNumber) string {
75 ev := ed.Values().ByNumber(n)
76 if ev != nil {
77 return string(ev.Name())
78 }
79 return strconv.Itoa(int(n))
80 }
81
82
83
84 type message = interface{}
85
86
87 type legacyMessageWrapper struct{ m protoreflect.ProtoMessage }
88
89 func (m legacyMessageWrapper) Reset() { proto.Reset(m.m) }
90 func (m legacyMessageWrapper) String() string { return Export{}.MessageStringOf(m.m) }
91 func (m legacyMessageWrapper) ProtoMessage() {}
92
93
94
95 func (Export) ProtoMessageV1Of(m message) protoiface.MessageV1 {
96 switch mv := m.(type) {
97 case nil:
98 return nil
99 case protoiface.MessageV1:
100 return mv
101 case unwrapper:
102 return Export{}.ProtoMessageV1Of(mv.protoUnwrap())
103 case protoreflect.ProtoMessage:
104 return legacyMessageWrapper{mv}
105 default:
106 panic(fmt.Sprintf("message %T is neither a v1 or v2 Message", m))
107 }
108 }
109
110 func (Export) protoMessageV2Of(m message) protoreflect.ProtoMessage {
111 switch mv := m.(type) {
112 case nil:
113 return nil
114 case protoreflect.ProtoMessage:
115 return mv
116 case legacyMessageWrapper:
117 return mv.m
118 case protoiface.MessageV1:
119 return nil
120 default:
121 panic(fmt.Sprintf("message %T is neither a v1 or v2 Message", m))
122 }
123 }
124
125
126
127 func (Export) ProtoMessageV2Of(m message) protoreflect.ProtoMessage {
128 if m == nil {
129 return nil
130 }
131 if mv := (Export{}).protoMessageV2Of(m); mv != nil {
132 return mv
133 }
134 return legacyWrapMessage(reflect.ValueOf(m)).Interface()
135 }
136
137
138
139 func (Export) MessageOf(m message) protoreflect.Message {
140 if m == nil {
141 return nil
142 }
143 if mv := (Export{}).protoMessageV2Of(m); mv != nil {
144 return mv.ProtoReflect()
145 }
146 return legacyWrapMessage(reflect.ValueOf(m))
147 }
148
149
150
151 func (Export) MessageDescriptorOf(m message) protoreflect.MessageDescriptor {
152 if m == nil {
153 return nil
154 }
155 if mv := (Export{}).protoMessageV2Of(m); mv != nil {
156 return mv.ProtoReflect().Descriptor()
157 }
158 return LegacyLoadMessageDesc(reflect.TypeOf(m))
159 }
160
161
162
163 func (Export) MessageTypeOf(m message) protoreflect.MessageType {
164 if m == nil {
165 return nil
166 }
167 if mv := (Export{}).protoMessageV2Of(m); mv != nil {
168 return mv.ProtoReflect().Type()
169 }
170 return legacyLoadMessageType(reflect.TypeOf(m), "")
171 }
172
173
174
175 func (Export) MessageStringOf(m protoreflect.ProtoMessage) string {
176 return prototext.MarshalOptions{Multiline: false}.Format(m)
177 }
178
View as plain text