blob: c70e30f0be76fb720ee079fedf2c51020f1cf16c [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 {
24 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 Tsai21ade492019-05-22 13:42:54 -040032func legacyLoadMessageInfo(t reflect.Type) *MessageInfo {
Joe Tsai4fe96632019-05-22 05:12:36 -040033 // Fast-path: check if a MessageInfo is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040034 if mt, ok := legacyMessageTypeCache.Load(t); ok {
35 return mt.(*MessageInfo)
Joe Tsaice6edd32018-10-19 16:27:46 -070036 }
37
Joe Tsai4fe96632019-05-22 05:12:36 -040038 // Slow-path: derive message descriptor and initialize MessageInfo.
Damien Neil16163b42019-08-06 15:43:25 -070039 mi := &MessageInfo{
40 Desc: LegacyLoadMessageDesc(t),
41 GoReflectType: t,
Joe Tsaib2f66be2019-05-22 00:42:45 -040042 }
Damien Neil16163b42019-08-06 15:43:25 -070043 if mi, ok := legacyMessageTypeCache.LoadOrStore(t, mi); ok {
44 return mi.(*MessageInfo)
Joe Tsaib9365042019-03-19 14:14:29 -070045 }
Damien Neil16163b42019-08-06 15:43:25 -070046 return mi
Joe Tsaice6edd32018-10-19 16:27:46 -070047}
48
Joe Tsaid8881392019-06-06 13:01:53 -070049var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor
Joe Tsai90fe9962018-10-18 11:06:29 -070050
Joe Tsai21ade492019-05-22 13:42:54 -040051// LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type,
Joe Tsaice6edd32018-10-19 16:27:46 -070052// which must be a *struct kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -070053//
54// This is exported for testing purposes.
Joe Tsai21ade492019-05-22 13:42:54 -040055func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070056 // Fast-path: check if a MessageDescriptor is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040057 if mi, ok := legacyMessageDescCache.Load(t); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -070058 return mi.(pref.MessageDescriptor)
59 }
60
Joe Tsaid8881392019-06-06 13:01:53 -070061 // Slow-path: initialize MessageDescriptor from the raw descriptor.
Joe Tsai90fe9962018-10-18 11:06:29 -070062 mv := reflect.New(t.Elem()).Interface()
63 if _, ok := mv.(pref.ProtoMessage); ok {
64 panic(fmt.Sprintf("%v already implements proto.Message", t))
65 }
Joe Tsaid8881392019-06-06 13:01:53 -070066 mdV1, ok := mv.(messageV1)
67 if !ok {
Joe Tsai32e8a522019-07-02 10:51:24 -070068 return aberrantLoadMessageDesc(t)
Joe Tsaid8881392019-06-06 13:01:53 -070069 }
70 b, idxs := mdV1.Descriptor()
Joe Tsai90fe9962018-10-18 11:06:29 -070071
Joe Tsaid8881392019-06-06 13:01:53 -070072 md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
73 for _, i := range idxs[1:] {
74 md = md.Messages().Get(i)
Joe Tsai90fe9962018-10-18 11:06:29 -070075 }
Joe Tsaid8881392019-06-06 13:01:53 -070076 if md, ok := legacyMessageDescCache.LoadOrStore(t, md); ok {
77 return md.(protoreflect.MessageDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -070078 }
Joe Tsaid8881392019-06-06 13:01:53 -070079 return md
Joe Tsai90fe9962018-10-18 11:06:29 -070080}
Joe Tsai851185d2019-07-01 13:45:52 -070081
Joe Tsai32e8a522019-07-02 10:51:24 -070082var (
83 aberrantMessageDescLock sync.Mutex
84 aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor
85)
Joe Tsai851185d2019-07-01 13:45:52 -070086
Joe Tsaia8044502019-07-06 17:38:44 -070087// aberrantLoadMessageDesc returns an EnumDescriptor derived from the Go type,
Joe Tsai851185d2019-07-01 13:45:52 -070088// which must not implement protoreflect.ProtoMessage or messageV1.
89//
90// This is a best-effort derivation of the message descriptor using the protobuf
91// tags on the struct fields.
Joe Tsai32e8a522019-07-02 10:51:24 -070092func aberrantLoadMessageDesc(t reflect.Type) pref.MessageDescriptor {
93 aberrantMessageDescLock.Lock()
94 defer aberrantMessageDescLock.Unlock()
95 if aberrantMessageDescCache == nil {
96 aberrantMessageDescCache = make(map[reflect.Type]protoreflect.MessageDescriptor)
97 }
98 return aberrantLoadMessageDescReentrant(t)
99}
100func aberrantLoadMessageDescReentrant(t reflect.Type) pref.MessageDescriptor {
Joe Tsai851185d2019-07-01 13:45:52 -0700101 // Fast-path: check if an MessageDescriptor is cached for this concrete type.
Joe Tsai32e8a522019-07-02 10:51:24 -0700102 if md, ok := aberrantMessageDescCache[t]; ok {
103 return md
Joe Tsai851185d2019-07-01 13:45:52 -0700104 }
105
Joe Tsai851185d2019-07-01 13:45:52 -0700106 // Slow-path: construct a descriptor from the Go struct type (best-effort).
Joe Tsai32e8a522019-07-02 10:51:24 -0700107 // Cache the MessageDescriptor early on so that we can resolve internal
108 // cyclic references.
109 md := &filedesc.Message{L2: new(filedesc.MessageL2)}
Joe Tsai851185d2019-07-01 13:45:52 -0700110 md.L0.FullName = aberrantDeriveFullName(t.Elem())
111 md.L0.ParentFile = filedesc.SurrogateProto2
Joe Tsai32e8a522019-07-02 10:51:24 -0700112 aberrantMessageDescCache[t] = md
Joe Tsai851185d2019-07-01 13:45:52 -0700113
Joe Tsai851185d2019-07-01 13:45:52 -0700114 // Try to determine if the message is using proto3 by checking scalars.
115 for i := 0; i < t.Elem().NumField(); i++ {
116 f := t.Elem().Field(i)
117 if tag := f.Tag.Get("protobuf"); tag != "" {
118 switch f.Type.Kind() {
119 case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
120 md.L0.ParentFile = filedesc.SurrogateProto3
121 }
122 for _, s := range strings.Split(tag, ",") {
123 if s == "proto3" {
124 md.L0.ParentFile = filedesc.SurrogateProto3
125 }
126 }
127 }
128 }
129
130 // Obtain a list of oneof wrapper types.
131 var oneofWrappers []reflect.Type
132 if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok {
133 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3]
134 for _, v := range vs.Interface().([]interface{}) {
135 oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
136 }
137 }
138 if fn, ok := t.MethodByName("XXX_OneofWrappers"); ok {
139 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
140 for _, v := range vs.Interface().([]interface{}) {
141 oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
142 }
143 }
144
145 // Obtain a list of the extension ranges.
146 if fn, ok := t.MethodByName("ExtensionRangeArray"); ok {
147 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
148 for i := 0; i < vs.Len(); i++ {
149 v := vs.Index(i)
150 md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, [2]pref.FieldNumber{
151 pref.FieldNumber(v.FieldByName("Start").Int()),
152 pref.FieldNumber(v.FieldByName("End").Int() + 1),
153 })
154 md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, nil)
155 }
156 }
157
158 // Derive the message fields by inspecting the struct fields.
159 for i := 0; i < t.Elem().NumField(); i++ {
160 f := t.Elem().Field(i)
161 if tag := f.Tag.Get("protobuf"); tag != "" {
162 tagKey := f.Tag.Get("protobuf_key")
163 tagVal := f.Tag.Get("protobuf_val")
164 aberrantAppendField(md, f.Type, tag, tagKey, tagVal)
165 }
166 if tag := f.Tag.Get("protobuf_oneof"); tag != "" {
167 n := len(md.L2.Oneofs.List)
168 md.L2.Oneofs.List = append(md.L2.Oneofs.List, filedesc.Oneof{})
169 od := &md.L2.Oneofs.List[n]
170 od.L0.FullName = md.FullName().Append(pref.Name(tag))
171 od.L0.ParentFile = md.L0.ParentFile
172 od.L0.Parent = md
173 od.L0.Index = n
174
175 for _, t := range oneofWrappers {
176 if t.Implements(f.Type) {
177 f := t.Elem().Field(0)
178 if tag := f.Tag.Get("protobuf"); tag != "" {
179 aberrantAppendField(md, f.Type, tag, "", "")
180 fd := &md.L2.Fields.List[len(md.L2.Fields.List)-1]
181 fd.L1.ContainingOneof = od
182 od.L1.Fields.List = append(od.L1.Fields.List, fd)
183 }
184 }
185 }
186 }
187 }
188
189 // TODO: Use custom Marshal/Unmarshal methods for the fast-path?
190
191 return md
192}
193
194func aberrantAppendField(md *filedesc.Message, goType reflect.Type, tag, tagKey, tagVal string) {
195 t := goType
196 isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
197 isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
198 if isOptional || isRepeated {
199 t = t.Elem()
200 }
201 fd := ptag.Unmarshal(tag, t, placeholderEnumValues{}).(*filedesc.Field)
202
203 // Append field descriptor to the message.
204 n := len(md.L2.Fields.List)
205 md.L2.Fields.List = append(md.L2.Fields.List, *fd)
206 fd = &md.L2.Fields.List[n]
207 fd.L0.FullName = md.FullName().Append(fd.Name())
208 fd.L0.ParentFile = md.L0.ParentFile
209 fd.L0.Parent = md
210 fd.L0.Index = n
211
212 if fd.L1.IsWeak || fd.L1.HasPacked {
213 fd.L1.Options = func() pref.ProtoMessage {
214 opts := descopts.Field.ProtoReflect().New()
215 if fd.L1.IsWeak {
216 opts.Set(opts.Descriptor().Fields().ByName("weak"), protoreflect.ValueOf(true))
217 }
218 if fd.L1.HasPacked {
219 opts.Set(opts.Descriptor().Fields().ByName("packed"), protoreflect.ValueOf(fd.L1.IsPacked))
220 }
221 return opts.Interface()
222 }
223 }
224
225 // Populate Enum and Message.
226 if fd.Enum() == nil && fd.Kind() == pref.EnumKind {
227 switch v := reflect.Zero(t).Interface().(type) {
228 case pref.Enum:
229 fd.L1.Enum = v.Descriptor()
230 default:
231 fd.L1.Enum = LegacyLoadEnumDesc(t)
232 }
233 }
234 if fd.Message() == nil && (fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind) {
235 switch v := reflect.Zero(t).Interface().(type) {
236 case pref.ProtoMessage:
237 fd.L1.Message = v.ProtoReflect().Descriptor()
Joe Tsai32e8a522019-07-02 10:51:24 -0700238 case messageV1:
239 fd.L1.Message = LegacyLoadMessageDesc(t)
Joe Tsai851185d2019-07-01 13:45:52 -0700240 default:
241 if t.Kind() == reflect.Map {
242 n := len(md.L1.Messages.List)
243 md.L1.Messages.List = append(md.L1.Messages.List, filedesc.Message{L2: new(filedesc.MessageL2)})
244 md2 := &md.L1.Messages.List[n]
Joe Tsai97a87392019-07-07 01:49:59 -0700245 md2.L0.FullName = md.FullName().Append(pref.Name(strs.MapEntryName(string(fd.Name()))))
Joe Tsai851185d2019-07-01 13:45:52 -0700246 md2.L0.ParentFile = md.L0.ParentFile
247 md2.L0.Parent = md
248 md2.L0.Index = n
249
250 md2.L2.IsMapEntry = true
251 md2.L2.Options = func() pref.ProtoMessage {
252 opts := descopts.Message.ProtoReflect().New()
253 opts.Set(opts.Descriptor().Fields().ByName("map_entry"), protoreflect.ValueOf(true))
254 return opts.Interface()
255 }
256
257 aberrantAppendField(md2, t.Key(), tagKey, "", "")
258 aberrantAppendField(md2, t.Elem(), tagVal, "", "")
259
260 fd.L1.Message = md2
261 break
262 }
Joe Tsai32e8a522019-07-02 10:51:24 -0700263 fd.L1.Message = aberrantLoadMessageDescReentrant(t)
Joe Tsai851185d2019-07-01 13:45:52 -0700264 }
265 }
266}
267
268type placeholderEnumValues struct {
269 protoreflect.EnumValueDescriptors
270}
271
272func (placeholderEnumValues) ByNumber(n pref.EnumNumber) pref.EnumValueDescriptor {
273 return filedesc.PlaceholderEnumValue(pref.FullName(fmt.Sprintf("UNKNOWN_%d", n)))
274}