blob: 6c522848fde14dbe348d4e1ad9ab06f85137445c [file] [log] [blame]
Joe Tsai90fe9962018-10-18 11:06:29 -07001// 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
Joe Tsai21ade492019-05-22 13:42:54 -04005package impl
Joe Tsai90fe9962018-10-18 11:06:29 -07006
7import (
8 "fmt"
Joe Tsai90fe9962018-10-18 11:06:29 -07009 "reflect"
Joe Tsai851185d2019-07-01 13:45:52 -070010 "strings"
Joe Tsai90fe9962018-10-18 11:06:29 -070011 "sync"
Joe Tsai90fe9962018-10-18 11:06:29 -070012
Joe Tsai851185d2019-07-01 13:45:52 -070013 "google.golang.org/protobuf/internal/descopts"
14 ptag "google.golang.org/protobuf/internal/encoding/tag"
15 "google.golang.org/protobuf/internal/filedesc"
Joe Tsai97a87392019-07-07 01:49:59 -070016 "google.golang.org/protobuf/internal/strs"
Joe Tsaid8881392019-06-06 13:01:53 -070017 "google.golang.org/protobuf/reflect/protoreflect"
Damien Neile89e6242019-05-13 23:55:40 -070018 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsai90fe9962018-10-18 11:06:29 -070019)
20
Joe Tsai21ade492019-05-22 13:42:54 -040021// legacyWrapMessage wraps v as a protoreflect.ProtoMessage,
Joe Tsaif0c01e42018-11-06 13:05:20 -080022// where v must be a *struct kind and not implement the v2 API already.
Joe Tsai21ade492019-05-22 13:42:54 -040023func legacyWrapMessage(v reflect.Value) pref.ProtoMessage {
Joe Tsaiea5ada12019-09-04 22:41:40 -070024 mt := legacyLoadMessageInfo(v.Type(), "")
Joe Tsai08e00302018-11-26 22:32:06 -080025 return mt.MessageOf(v.Interface()).Interface()
Joe Tsaif0c01e42018-11-06 13:05:20 -080026}
27
Joe Tsai21ade492019-05-22 13:42:54 -040028var legacyMessageTypeCache sync.Map // map[reflect.Type]*MessageInfo
Joe Tsaice6edd32018-10-19 16:27:46 -070029
Joe Tsai21ade492019-05-22 13:42:54 -040030// legacyLoadMessageInfo dynamically loads a *MessageInfo for t,
Joe Tsaif0c01e42018-11-06 13:05:20 -080031// where t must be a *struct kind and not implement the v2 API already.
Joe Tsaiea5ada12019-09-04 22:41:40 -070032// The provided name is used if it cannot be determined from the message.
33func legacyLoadMessageInfo(t reflect.Type, name pref.FullName) *MessageInfo {
Joe Tsai4fe96632019-05-22 05:12:36 -040034 // Fast-path: check if a MessageInfo is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040035 if mt, ok := legacyMessageTypeCache.Load(t); ok {
36 return mt.(*MessageInfo)
Joe Tsaice6edd32018-10-19 16:27:46 -070037 }
38
Joe Tsai4fe96632019-05-22 05:12:36 -040039 // Slow-path: derive message descriptor and initialize MessageInfo.
Damien Neil16163b42019-08-06 15:43:25 -070040 mi := &MessageInfo{
Joe Tsaiea5ada12019-09-04 22:41:40 -070041 Desc: legacyLoadMessageDesc(t, name),
Damien Neil16163b42019-08-06 15:43:25 -070042 GoReflectType: t,
Joe Tsaib2f66be2019-05-22 00:42:45 -040043 }
Damien Neil16163b42019-08-06 15:43:25 -070044 if mi, ok := legacyMessageTypeCache.LoadOrStore(t, mi); ok {
45 return mi.(*MessageInfo)
Joe Tsaib9365042019-03-19 14:14:29 -070046 }
Damien Neil16163b42019-08-06 15:43:25 -070047 return mi
Joe Tsaice6edd32018-10-19 16:27:46 -070048}
49
Joe Tsaid8881392019-06-06 13:01:53 -070050var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor
Joe Tsai90fe9962018-10-18 11:06:29 -070051
Joe Tsai21ade492019-05-22 13:42:54 -040052// LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type,
Joe Tsaice6edd32018-10-19 16:27:46 -070053// which must be a *struct kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -070054//
55// This is exported for testing purposes.
Joe Tsai21ade492019-05-22 13:42:54 -040056func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor {
Joe Tsaiea5ada12019-09-04 22:41:40 -070057 return legacyLoadMessageDesc(t, "")
58}
59func legacyLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070060 // Fast-path: check if a MessageDescriptor is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040061 if mi, ok := legacyMessageDescCache.Load(t); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -070062 return mi.(pref.MessageDescriptor)
63 }
64
Joe Tsaid8881392019-06-06 13:01:53 -070065 // Slow-path: initialize MessageDescriptor from the raw descriptor.
Joe Tsai90fe9962018-10-18 11:06:29 -070066 mv := reflect.New(t.Elem()).Interface()
67 if _, ok := mv.(pref.ProtoMessage); ok {
68 panic(fmt.Sprintf("%v already implements proto.Message", t))
69 }
Joe Tsaid8881392019-06-06 13:01:53 -070070 mdV1, ok := mv.(messageV1)
71 if !ok {
Joe Tsaiea5ada12019-09-04 22:41:40 -070072 return aberrantLoadMessageDesc(t, name)
Joe Tsaid8881392019-06-06 13:01:53 -070073 }
74 b, idxs := mdV1.Descriptor()
Joe Tsai90fe9962018-10-18 11:06:29 -070075
Joe Tsaid8881392019-06-06 13:01:53 -070076 md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
77 for _, i := range idxs[1:] {
78 md = md.Messages().Get(i)
Joe Tsai90fe9962018-10-18 11:06:29 -070079 }
Joe Tsaiea5ada12019-09-04 22:41:40 -070080 if name != "" && md.FullName() != name {
81 panic(fmt.Sprintf("mismatching message name: got %v, want %v", md.FullName(), name))
82 }
Joe Tsaid8881392019-06-06 13:01:53 -070083 if md, ok := legacyMessageDescCache.LoadOrStore(t, md); ok {
84 return md.(protoreflect.MessageDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -070085 }
Joe Tsaid8881392019-06-06 13:01:53 -070086 return md
Joe Tsai90fe9962018-10-18 11:06:29 -070087}
Joe Tsai851185d2019-07-01 13:45:52 -070088
Joe Tsai32e8a522019-07-02 10:51:24 -070089var (
90 aberrantMessageDescLock sync.Mutex
91 aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor
92)
Joe Tsai851185d2019-07-01 13:45:52 -070093
Joe Tsaia8044502019-07-06 17:38:44 -070094// aberrantLoadMessageDesc returns an EnumDescriptor derived from the Go type,
Joe Tsai851185d2019-07-01 13:45:52 -070095// which must not implement protoreflect.ProtoMessage or messageV1.
96//
97// This is a best-effort derivation of the message descriptor using the protobuf
98// tags on the struct fields.
Joe Tsaiea5ada12019-09-04 22:41:40 -070099func aberrantLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai32e8a522019-07-02 10:51:24 -0700100 aberrantMessageDescLock.Lock()
101 defer aberrantMessageDescLock.Unlock()
102 if aberrantMessageDescCache == nil {
103 aberrantMessageDescCache = make(map[reflect.Type]protoreflect.MessageDescriptor)
104 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700105 return aberrantLoadMessageDescReentrant(t, name)
Joe Tsai32e8a522019-07-02 10:51:24 -0700106}
Joe Tsaiea5ada12019-09-04 22:41:40 -0700107func aberrantLoadMessageDescReentrant(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai851185d2019-07-01 13:45:52 -0700108 // Fast-path: check if an MessageDescriptor is cached for this concrete type.
Joe Tsai32e8a522019-07-02 10:51:24 -0700109 if md, ok := aberrantMessageDescCache[t]; ok {
110 return md
Joe Tsai851185d2019-07-01 13:45:52 -0700111 }
112
Joe Tsai851185d2019-07-01 13:45:52 -0700113 // Slow-path: construct a descriptor from the Go struct type (best-effort).
Joe Tsai32e8a522019-07-02 10:51:24 -0700114 // Cache the MessageDescriptor early on so that we can resolve internal
115 // cyclic references.
116 md := &filedesc.Message{L2: new(filedesc.MessageL2)}
Joe Tsaiea5ada12019-09-04 22:41:40 -0700117 md.L0.FullName = aberrantDeriveMessageName(t.Elem(), name)
Joe Tsai851185d2019-07-01 13:45:52 -0700118 md.L0.ParentFile = filedesc.SurrogateProto2
Joe Tsai32e8a522019-07-02 10:51:24 -0700119 aberrantMessageDescCache[t] = md
Joe Tsai851185d2019-07-01 13:45:52 -0700120
Joe Tsai851185d2019-07-01 13:45:52 -0700121 // Try to determine if the message is using proto3 by checking scalars.
122 for i := 0; i < t.Elem().NumField(); i++ {
123 f := t.Elem().Field(i)
124 if tag := f.Tag.Get("protobuf"); tag != "" {
125 switch f.Type.Kind() {
126 case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
127 md.L0.ParentFile = filedesc.SurrogateProto3
128 }
129 for _, s := range strings.Split(tag, ",") {
130 if s == "proto3" {
131 md.L0.ParentFile = filedesc.SurrogateProto3
132 }
133 }
134 }
135 }
136
137 // Obtain a list of oneof wrapper types.
138 var oneofWrappers []reflect.Type
139 if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok {
140 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3]
141 for _, v := range vs.Interface().([]interface{}) {
142 oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
143 }
144 }
145 if fn, ok := t.MethodByName("XXX_OneofWrappers"); ok {
146 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
147 for _, v := range vs.Interface().([]interface{}) {
148 oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
149 }
150 }
151
152 // Obtain a list of the extension ranges.
153 if fn, ok := t.MethodByName("ExtensionRangeArray"); ok {
154 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
155 for i := 0; i < vs.Len(); i++ {
156 v := vs.Index(i)
157 md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, [2]pref.FieldNumber{
158 pref.FieldNumber(v.FieldByName("Start").Int()),
159 pref.FieldNumber(v.FieldByName("End").Int() + 1),
160 })
161 md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, nil)
162 }
163 }
164
165 // Derive the message fields by inspecting the struct fields.
166 for i := 0; i < t.Elem().NumField(); i++ {
167 f := t.Elem().Field(i)
168 if tag := f.Tag.Get("protobuf"); tag != "" {
169 tagKey := f.Tag.Get("protobuf_key")
170 tagVal := f.Tag.Get("protobuf_val")
171 aberrantAppendField(md, f.Type, tag, tagKey, tagVal)
172 }
173 if tag := f.Tag.Get("protobuf_oneof"); tag != "" {
174 n := len(md.L2.Oneofs.List)
175 md.L2.Oneofs.List = append(md.L2.Oneofs.List, filedesc.Oneof{})
176 od := &md.L2.Oneofs.List[n]
177 od.L0.FullName = md.FullName().Append(pref.Name(tag))
178 od.L0.ParentFile = md.L0.ParentFile
179 od.L0.Parent = md
180 od.L0.Index = n
181
182 for _, t := range oneofWrappers {
183 if t.Implements(f.Type) {
184 f := t.Elem().Field(0)
185 if tag := f.Tag.Get("protobuf"); tag != "" {
186 aberrantAppendField(md, f.Type, tag, "", "")
187 fd := &md.L2.Fields.List[len(md.L2.Fields.List)-1]
188 fd.L1.ContainingOneof = od
189 od.L1.Fields.List = append(od.L1.Fields.List, fd)
190 }
191 }
192 }
193 }
194 }
195
196 // TODO: Use custom Marshal/Unmarshal methods for the fast-path?
197
198 return md
199}
200
Joe Tsaiea5ada12019-09-04 22:41:40 -0700201func aberrantDeriveMessageName(t reflect.Type, name pref.FullName) pref.FullName {
202 if name.IsValid() {
203 return name
204 }
Joe Tsaie87cf532019-09-10 12:20:00 -0700205 func() {
206 defer func() { recover() }() // swallow possible nil panics
207 if m, ok := reflect.New(t).Interface().(interface{ XXX_MessageName() string }); ok {
208 name = pref.FullName(m.XXX_MessageName())
Joe Tsaiea5ada12019-09-04 22:41:40 -0700209 }
Joe Tsaie87cf532019-09-10 12:20:00 -0700210 }()
211 if name.IsValid() {
212 return name
Joe Tsaiea5ada12019-09-04 22:41:40 -0700213 }
214 return aberrantDeriveFullName(t)
215}
216
Joe Tsai851185d2019-07-01 13:45:52 -0700217func aberrantAppendField(md *filedesc.Message, goType reflect.Type, tag, tagKey, tagVal string) {
218 t := goType
219 isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
220 isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
221 if isOptional || isRepeated {
222 t = t.Elem()
223 }
224 fd := ptag.Unmarshal(tag, t, placeholderEnumValues{}).(*filedesc.Field)
225
226 // Append field descriptor to the message.
227 n := len(md.L2.Fields.List)
228 md.L2.Fields.List = append(md.L2.Fields.List, *fd)
229 fd = &md.L2.Fields.List[n]
230 fd.L0.FullName = md.FullName().Append(fd.Name())
231 fd.L0.ParentFile = md.L0.ParentFile
232 fd.L0.Parent = md
233 fd.L0.Index = n
234
235 if fd.L1.IsWeak || fd.L1.HasPacked {
236 fd.L1.Options = func() pref.ProtoMessage {
237 opts := descopts.Field.ProtoReflect().New()
238 if fd.L1.IsWeak {
239 opts.Set(opts.Descriptor().Fields().ByName("weak"), protoreflect.ValueOf(true))
240 }
241 if fd.L1.HasPacked {
242 opts.Set(opts.Descriptor().Fields().ByName("packed"), protoreflect.ValueOf(fd.L1.IsPacked))
243 }
244 return opts.Interface()
245 }
246 }
247
248 // Populate Enum and Message.
249 if fd.Enum() == nil && fd.Kind() == pref.EnumKind {
250 switch v := reflect.Zero(t).Interface().(type) {
251 case pref.Enum:
252 fd.L1.Enum = v.Descriptor()
253 default:
254 fd.L1.Enum = LegacyLoadEnumDesc(t)
255 }
256 }
257 if fd.Message() == nil && (fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind) {
258 switch v := reflect.Zero(t).Interface().(type) {
259 case pref.ProtoMessage:
260 fd.L1.Message = v.ProtoReflect().Descriptor()
Joe Tsai32e8a522019-07-02 10:51:24 -0700261 case messageV1:
262 fd.L1.Message = LegacyLoadMessageDesc(t)
Joe Tsai851185d2019-07-01 13:45:52 -0700263 default:
264 if t.Kind() == reflect.Map {
265 n := len(md.L1.Messages.List)
266 md.L1.Messages.List = append(md.L1.Messages.List, filedesc.Message{L2: new(filedesc.MessageL2)})
267 md2 := &md.L1.Messages.List[n]
Joe Tsai97a87392019-07-07 01:49:59 -0700268 md2.L0.FullName = md.FullName().Append(pref.Name(strs.MapEntryName(string(fd.Name()))))
Joe Tsai851185d2019-07-01 13:45:52 -0700269 md2.L0.ParentFile = md.L0.ParentFile
270 md2.L0.Parent = md
271 md2.L0.Index = n
272
273 md2.L2.IsMapEntry = true
274 md2.L2.Options = func() pref.ProtoMessage {
275 opts := descopts.Message.ProtoReflect().New()
276 opts.Set(opts.Descriptor().Fields().ByName("map_entry"), protoreflect.ValueOf(true))
277 return opts.Interface()
278 }
279
280 aberrantAppendField(md2, t.Key(), tagKey, "", "")
281 aberrantAppendField(md2, t.Elem(), tagVal, "", "")
282
283 fd.L1.Message = md2
284 break
285 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700286 fd.L1.Message = aberrantLoadMessageDescReentrant(t, "")
Joe Tsai851185d2019-07-01 13:45:52 -0700287 }
288 }
289}
290
291type placeholderEnumValues struct {
292 protoreflect.EnumValueDescriptors
293}
294
295func (placeholderEnumValues) ByNumber(n pref.EnumNumber) pref.EnumValueDescriptor {
296 return filedesc.PlaceholderEnumValue(pref.FullName(fmt.Sprintf("UNKNOWN_%d", n)))
297}