1
2
3
4
5 package impl
6
7 import (
8 "fmt"
9 "reflect"
10 "strconv"
11 "strings"
12 "sync"
13 "sync/atomic"
14
15 "google.golang.org/protobuf/internal/genid"
16 "google.golang.org/protobuf/reflect/protoreflect"
17 "google.golang.org/protobuf/reflect/protoregistry"
18 )
19
20
21
22
23
24
25
26 type MessageInfo struct {
27
28 GoReflectType reflect.Type
29
30
31 Desc protoreflect.MessageDescriptor
32
33
34
35 Exporter exporter
36
37
38 OneofWrappers []interface{}
39
40 initMu sync.Mutex
41 initDone uint32
42
43 reflectMessageInfo
44 coderMessageInfo
45 }
46
47
48
49
50 type exporter func(v interface{}, i int) interface{}
51
52
53
54
55 func getMessageInfo(mt reflect.Type) *MessageInfo {
56 m, ok := reflect.Zero(mt).Interface().(protoreflect.ProtoMessage)
57 if !ok {
58 return nil
59 }
60 mr, ok := m.ProtoReflect().(interface{ ProtoMessageInfo() *MessageInfo })
61 if !ok {
62 return nil
63 }
64 return mr.ProtoMessageInfo()
65 }
66
67 func (mi *MessageInfo) init() {
68
69
70
71 if atomic.LoadUint32(&mi.initDone) == 0 {
72 mi.initOnce()
73 }
74 }
75
76 func (mi *MessageInfo) initOnce() {
77 mi.initMu.Lock()
78 defer mi.initMu.Unlock()
79 if mi.initDone == 1 {
80 return
81 }
82
83 t := mi.GoReflectType
84 if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
85 panic(fmt.Sprintf("got %v, want *struct kind", t))
86 }
87 t = t.Elem()
88
89 si := mi.makeStructInfo(t)
90 mi.makeReflectFuncs(t, si)
91 mi.makeCoderMethods(t, si)
92
93 atomic.StoreUint32(&mi.initDone, 1)
94 }
95
96
97
98
99 func (mi *MessageInfo) getPointer(m protoreflect.Message) (p pointer, ok bool) {
100 switch m := m.(type) {
101 case *messageState:
102 return m.pointer(), m.messageInfo() == mi
103 case *messageReflectWrapper:
104 return m.pointer(), m.messageInfo() == mi
105 }
106 return pointer{}, false
107 }
108
109 type (
110 SizeCache = int32
111 WeakFields = map[int32]protoreflect.ProtoMessage
112 UnknownFields = unknownFieldsA
113 unknownFieldsA = []byte
114 unknownFieldsB = *[]byte
115 ExtensionFields = map[int32]ExtensionField
116 )
117
118 var (
119 sizecacheType = reflect.TypeOf(SizeCache(0))
120 weakFieldsType = reflect.TypeOf(WeakFields(nil))
121 unknownFieldsAType = reflect.TypeOf(unknownFieldsA(nil))
122 unknownFieldsBType = reflect.TypeOf(unknownFieldsB(nil))
123 extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
124 )
125
126 type structInfo struct {
127 sizecacheOffset offset
128 sizecacheType reflect.Type
129 weakOffset offset
130 weakType reflect.Type
131 unknownOffset offset
132 unknownType reflect.Type
133 extensionOffset offset
134 extensionType reflect.Type
135
136 fieldsByNumber map[protoreflect.FieldNumber]reflect.StructField
137 oneofsByName map[protoreflect.Name]reflect.StructField
138 oneofWrappersByType map[reflect.Type]protoreflect.FieldNumber
139 oneofWrappersByNumber map[protoreflect.FieldNumber]reflect.Type
140 }
141
142 func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
143 si := structInfo{
144 sizecacheOffset: invalidOffset,
145 weakOffset: invalidOffset,
146 unknownOffset: invalidOffset,
147 extensionOffset: invalidOffset,
148
149 fieldsByNumber: map[protoreflect.FieldNumber]reflect.StructField{},
150 oneofsByName: map[protoreflect.Name]reflect.StructField{},
151 oneofWrappersByType: map[reflect.Type]protoreflect.FieldNumber{},
152 oneofWrappersByNumber: map[protoreflect.FieldNumber]reflect.Type{},
153 }
154
155 fieldLoop:
156 for i := 0; i < t.NumField(); i++ {
157 switch f := t.Field(i); f.Name {
158 case genid.SizeCache_goname, genid.SizeCacheA_goname:
159 if f.Type == sizecacheType {
160 si.sizecacheOffset = offsetOf(f, mi.Exporter)
161 si.sizecacheType = f.Type
162 }
163 case genid.WeakFields_goname, genid.WeakFieldsA_goname:
164 if f.Type == weakFieldsType {
165 si.weakOffset = offsetOf(f, mi.Exporter)
166 si.weakType = f.Type
167 }
168 case genid.UnknownFields_goname, genid.UnknownFieldsA_goname:
169 if f.Type == unknownFieldsAType || f.Type == unknownFieldsBType {
170 si.unknownOffset = offsetOf(f, mi.Exporter)
171 si.unknownType = f.Type
172 }
173 case genid.ExtensionFields_goname, genid.ExtensionFieldsA_goname, genid.ExtensionFieldsB_goname:
174 if f.Type == extensionFieldsType {
175 si.extensionOffset = offsetOf(f, mi.Exporter)
176 si.extensionType = f.Type
177 }
178 default:
179 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
180 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
181 n, _ := strconv.ParseUint(s, 10, 64)
182 si.fieldsByNumber[protoreflect.FieldNumber(n)] = f
183 continue fieldLoop
184 }
185 }
186 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
187 si.oneofsByName[protoreflect.Name(s)] = f
188 continue fieldLoop
189 }
190 }
191 }
192
193
194 oneofWrappers := mi.OneofWrappers
195 methods := make([]reflect.Method, 0, 2)
196 if m, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
197 methods = append(methods, m)
198 }
199 if m, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
200 methods = append(methods, m)
201 }
202 for _, fn := range methods {
203 for _, v := range fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))}) {
204 if vs, ok := v.Interface().([]interface{}); ok {
205 oneofWrappers = vs
206 }
207 }
208 }
209 for _, v := range oneofWrappers {
210 tf := reflect.TypeOf(v).Elem()
211 f := tf.Field(0)
212 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
213 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
214 n, _ := strconv.ParseUint(s, 10, 64)
215 si.oneofWrappersByType[tf] = protoreflect.FieldNumber(n)
216 si.oneofWrappersByNumber[protoreflect.FieldNumber(n)] = tf
217 break
218 }
219 }
220 }
221
222 return si
223 }
224
225 func (mi *MessageInfo) New() protoreflect.Message {
226 m := reflect.New(mi.GoReflectType.Elem()).Interface()
227 if r, ok := m.(protoreflect.ProtoMessage); ok {
228 return r.ProtoReflect()
229 }
230 return mi.MessageOf(m)
231 }
232 func (mi *MessageInfo) Zero() protoreflect.Message {
233 return mi.MessageOf(reflect.Zero(mi.GoReflectType).Interface())
234 }
235 func (mi *MessageInfo) Descriptor() protoreflect.MessageDescriptor {
236 return mi.Desc
237 }
238 func (mi *MessageInfo) Enum(i int) protoreflect.EnumType {
239 mi.init()
240 fd := mi.Desc.Fields().Get(i)
241 return Export{}.EnumTypeOf(mi.fieldTypes[fd.Number()])
242 }
243 func (mi *MessageInfo) Message(i int) protoreflect.MessageType {
244 mi.init()
245 fd := mi.Desc.Fields().Get(i)
246 switch {
247 case fd.IsWeak():
248 mt, _ := protoregistry.GlobalTypes.FindMessageByName(fd.Message().FullName())
249 return mt
250 case fd.IsMap():
251 return mapEntryType{fd.Message(), mi.fieldTypes[fd.Number()]}
252 default:
253 return Export{}.MessageTypeOf(mi.fieldTypes[fd.Number()])
254 }
255 }
256
257 type mapEntryType struct {
258 desc protoreflect.MessageDescriptor
259 valType interface{}
260 }
261
262 func (mt mapEntryType) New() protoreflect.Message {
263 return nil
264 }
265 func (mt mapEntryType) Zero() protoreflect.Message {
266 return nil
267 }
268 func (mt mapEntryType) Descriptor() protoreflect.MessageDescriptor {
269 return mt.desc
270 }
271 func (mt mapEntryType) Enum(i int) protoreflect.EnumType {
272 fd := mt.desc.Fields().Get(i)
273 if fd.Enum() == nil {
274 return nil
275 }
276 return Export{}.EnumTypeOf(mt.valType)
277 }
278 func (mt mapEntryType) Message(i int) protoreflect.MessageType {
279 fd := mt.desc.Fields().Get(i)
280 if fd.Message() == nil {
281 return nil
282 }
283 return Export{}.MessageTypeOf(mt.valType)
284 }
285
View as plain text