blob: cb1a11110bbbf09977caef6ad6ae4ec97dde6b98 [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"
Damien Neil47d58932019-09-30 15:34:27 -070015 "google.golang.org/protobuf/internal/errors"
Joe Tsai851185d2019-07-01 13:45:52 -070016 "google.golang.org/protobuf/internal/filedesc"
Joe Tsai97a87392019-07-07 01:49:59 -070017 "google.golang.org/protobuf/internal/strs"
Joe Tsaid8881392019-06-06 13:01:53 -070018 "google.golang.org/protobuf/reflect/protoreflect"
Damien Neile89e6242019-05-13 23:55:40 -070019 pref "google.golang.org/protobuf/reflect/protoreflect"
Damien Neil37ef6912019-09-25 16:51:15 -070020 piface "google.golang.org/protobuf/runtime/protoiface"
Joe Tsai90fe9962018-10-18 11:06:29 -070021)
22
Joe Tsai21ade492019-05-22 13:42:54 -040023// legacyWrapMessage wraps v as a protoreflect.ProtoMessage,
Joe Tsaif0c01e42018-11-06 13:05:20 -080024// where v must be a *struct kind and not implement the v2 API already.
Joe Tsai21ade492019-05-22 13:42:54 -040025func legacyWrapMessage(v reflect.Value) pref.ProtoMessage {
Damien Neil47d58932019-09-30 15:34:27 -070026 typ := v.Type()
27 if typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Struct {
28 return aberrantMessage{v: v}
29 }
30 mt := legacyLoadMessageInfo(typ, "")
Joe Tsai08e00302018-11-26 22:32:06 -080031 return mt.MessageOf(v.Interface()).Interface()
Joe Tsaif0c01e42018-11-06 13:05:20 -080032}
33
Joe Tsai21ade492019-05-22 13:42:54 -040034var legacyMessageTypeCache sync.Map // map[reflect.Type]*MessageInfo
Joe Tsaice6edd32018-10-19 16:27:46 -070035
Joe Tsai21ade492019-05-22 13:42:54 -040036// legacyLoadMessageInfo dynamically loads a *MessageInfo for t,
Joe Tsaif0c01e42018-11-06 13:05:20 -080037// where t must be a *struct kind and not implement the v2 API already.
Joe Tsaiea5ada12019-09-04 22:41:40 -070038// The provided name is used if it cannot be determined from the message.
39func legacyLoadMessageInfo(t reflect.Type, name pref.FullName) *MessageInfo {
Joe Tsai4fe96632019-05-22 05:12:36 -040040 // Fast-path: check if a MessageInfo is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040041 if mt, ok := legacyMessageTypeCache.Load(t); ok {
42 return mt.(*MessageInfo)
Joe Tsaice6edd32018-10-19 16:27:46 -070043 }
44
Joe Tsai4fe96632019-05-22 05:12:36 -040045 // Slow-path: derive message descriptor and initialize MessageInfo.
Damien Neil16163b42019-08-06 15:43:25 -070046 mi := &MessageInfo{
Joe Tsaiea5ada12019-09-04 22:41:40 -070047 Desc: legacyLoadMessageDesc(t, name),
Damien Neil16163b42019-08-06 15:43:25 -070048 GoReflectType: t,
Joe Tsaib2f66be2019-05-22 00:42:45 -040049 }
Damien Neil37ef6912019-09-25 16:51:15 -070050
51 v := reflect.Zero(t).Interface()
Damien Neil47d58932019-09-30 15:34:27 -070052 if _, ok := v.(legacyMarshaler); ok {
53 mi.methods.MarshalAppend = legacyMarshalAppend
54 mi.methods.Size = legacySize
Damien Neilc7f2bee2019-11-06 15:29:05 -080055
56 // We have no way to tell whether the type's Marshal method
57 // supports deterministic serialization or not, but this
58 // preserves the v1 implementation's behavior of always
59 // calling Marshal methods when present.
60 mi.methods.Flags |= piface.SupportMarshalDeterministic
Damien Neil37ef6912019-09-25 16:51:15 -070061 }
Damien Neil47d58932019-09-30 15:34:27 -070062 if _, ok := v.(legacyUnmarshaler); ok {
63 mi.methods.Unmarshal = legacyUnmarshal
Damien Neil37ef6912019-09-25 16:51:15 -070064 }
65
Damien Neil16163b42019-08-06 15:43:25 -070066 if mi, ok := legacyMessageTypeCache.LoadOrStore(t, mi); ok {
67 return mi.(*MessageInfo)
Joe Tsaib9365042019-03-19 14:14:29 -070068 }
Damien Neil16163b42019-08-06 15:43:25 -070069 return mi
Joe Tsaice6edd32018-10-19 16:27:46 -070070}
71
Joe Tsaid8881392019-06-06 13:01:53 -070072var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor
Joe Tsai90fe9962018-10-18 11:06:29 -070073
Joe Tsai21ade492019-05-22 13:42:54 -040074// LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type,
Joe Tsaice6edd32018-10-19 16:27:46 -070075// which must be a *struct kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -070076//
77// This is exported for testing purposes.
Joe Tsai21ade492019-05-22 13:42:54 -040078func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor {
Joe Tsaiea5ada12019-09-04 22:41:40 -070079 return legacyLoadMessageDesc(t, "")
80}
81func legacyLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070082 // Fast-path: check if a MessageDescriptor is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040083 if mi, ok := legacyMessageDescCache.Load(t); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -070084 return mi.(pref.MessageDescriptor)
85 }
86
Joe Tsaid8881392019-06-06 13:01:53 -070087 // Slow-path: initialize MessageDescriptor from the raw descriptor.
Damien Neil47d58932019-09-30 15:34:27 -070088 mv := reflect.Zero(t).Interface()
Joe Tsai90fe9962018-10-18 11:06:29 -070089 if _, ok := mv.(pref.ProtoMessage); ok {
90 panic(fmt.Sprintf("%v already implements proto.Message", t))
91 }
Joe Tsaid8881392019-06-06 13:01:53 -070092 mdV1, ok := mv.(messageV1)
93 if !ok {
Joe Tsaiea5ada12019-09-04 22:41:40 -070094 return aberrantLoadMessageDesc(t, name)
Joe Tsaid8881392019-06-06 13:01:53 -070095 }
Damien Neil16057752019-11-11 16:30:04 -080096
97 // If this is a dynamic message type where there isn't a 1-1 mapping between
98 // Go and protobuf types, calling the Descriptor method on the zero value of
99 // the message type isn't likely to work. If it panics, swallow the panic and
100 // continue as if the Descriptor method wasn't present.
101 b, idxs := func() ([]byte, []int) {
102 defer func() {
103 recover()
104 }()
105 return mdV1.Descriptor()
106 }()
107 if b == nil {
108 return aberrantLoadMessageDesc(t, name)
109 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700110
Damien Neil4151cae2019-12-09 14:31:23 -0800111 // If the Go type has no fields, then this might be a proto3 empty message
112 // from before the size cache was added. If there are any fields, check to
113 // see that at least one of them looks like something we generated.
114 if nfield := t.Elem().NumField(); nfield > 0 {
115 hasProtoField := false
116 for i := 0; i < nfield; i++ {
117 f := t.Elem().Field(i)
118 if tag := f.Tag.Get("protobuf"); tag != "" || strings.HasPrefix(f.Name, "XXX_") {
119 hasProtoField = true
120 break
121 }
122 }
123 if !hasProtoField {
124 return aberrantLoadMessageDesc(t, name)
125 }
126 }
127
Joe Tsaid8881392019-06-06 13:01:53 -0700128 md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
129 for _, i := range idxs[1:] {
130 md = md.Messages().Get(i)
Joe Tsai90fe9962018-10-18 11:06:29 -0700131 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700132 if name != "" && md.FullName() != name {
133 panic(fmt.Sprintf("mismatching message name: got %v, want %v", md.FullName(), name))
134 }
Joe Tsaid8881392019-06-06 13:01:53 -0700135 if md, ok := legacyMessageDescCache.LoadOrStore(t, md); ok {
136 return md.(protoreflect.MessageDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -0700137 }
Joe Tsaid8881392019-06-06 13:01:53 -0700138 return md
Joe Tsai90fe9962018-10-18 11:06:29 -0700139}
Joe Tsai851185d2019-07-01 13:45:52 -0700140
Joe Tsai32e8a522019-07-02 10:51:24 -0700141var (
142 aberrantMessageDescLock sync.Mutex
143 aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor
144)
Joe Tsai851185d2019-07-01 13:45:52 -0700145
Damien Neil47d58932019-09-30 15:34:27 -0700146// aberrantLoadMessageDesc returns an MessageDescriptor derived from the Go type,
Joe Tsai851185d2019-07-01 13:45:52 -0700147// which must not implement protoreflect.ProtoMessage or messageV1.
148//
149// This is a best-effort derivation of the message descriptor using the protobuf
150// tags on the struct fields.
Joe Tsaiea5ada12019-09-04 22:41:40 -0700151func aberrantLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai32e8a522019-07-02 10:51:24 -0700152 aberrantMessageDescLock.Lock()
153 defer aberrantMessageDescLock.Unlock()
154 if aberrantMessageDescCache == nil {
155 aberrantMessageDescCache = make(map[reflect.Type]protoreflect.MessageDescriptor)
156 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700157 return aberrantLoadMessageDescReentrant(t, name)
Joe Tsai32e8a522019-07-02 10:51:24 -0700158}
Joe Tsaiea5ada12019-09-04 22:41:40 -0700159func aberrantLoadMessageDescReentrant(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai851185d2019-07-01 13:45:52 -0700160 // Fast-path: check if an MessageDescriptor is cached for this concrete type.
Joe Tsai32e8a522019-07-02 10:51:24 -0700161 if md, ok := aberrantMessageDescCache[t]; ok {
162 return md
Joe Tsai851185d2019-07-01 13:45:52 -0700163 }
164
Joe Tsai851185d2019-07-01 13:45:52 -0700165 // Slow-path: construct a descriptor from the Go struct type (best-effort).
Joe Tsai32e8a522019-07-02 10:51:24 -0700166 // Cache the MessageDescriptor early on so that we can resolve internal
167 // cyclic references.
168 md := &filedesc.Message{L2: new(filedesc.MessageL2)}
Damien Neil47d58932019-09-30 15:34:27 -0700169 md.L0.FullName = aberrantDeriveMessageName(t, name)
Joe Tsai851185d2019-07-01 13:45:52 -0700170 md.L0.ParentFile = filedesc.SurrogateProto2
Joe Tsai32e8a522019-07-02 10:51:24 -0700171 aberrantMessageDescCache[t] = md
Joe Tsai851185d2019-07-01 13:45:52 -0700172
Damien Neil47d58932019-09-30 15:34:27 -0700173 if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
174 return md
175 }
176
Joe Tsai851185d2019-07-01 13:45:52 -0700177 // Try to determine if the message is using proto3 by checking scalars.
178 for i := 0; i < t.Elem().NumField(); i++ {
179 f := t.Elem().Field(i)
180 if tag := f.Tag.Get("protobuf"); tag != "" {
181 switch f.Type.Kind() {
182 case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
183 md.L0.ParentFile = filedesc.SurrogateProto3
184 }
185 for _, s := range strings.Split(tag, ",") {
186 if s == "proto3" {
187 md.L0.ParentFile = filedesc.SurrogateProto3
188 }
189 }
190 }
191 }
192
193 // Obtain a list of oneof wrapper types.
194 var oneofWrappers []reflect.Type
195 if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok {
196 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3]
197 for _, v := range vs.Interface().([]interface{}) {
198 oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
199 }
200 }
201 if fn, ok := t.MethodByName("XXX_OneofWrappers"); ok {
202 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
203 for _, v := range vs.Interface().([]interface{}) {
204 oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
205 }
206 }
207
208 // Obtain a list of the extension ranges.
209 if fn, ok := t.MethodByName("ExtensionRangeArray"); ok {
210 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
211 for i := 0; i < vs.Len(); i++ {
212 v := vs.Index(i)
213 md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, [2]pref.FieldNumber{
214 pref.FieldNumber(v.FieldByName("Start").Int()),
215 pref.FieldNumber(v.FieldByName("End").Int() + 1),
216 })
217 md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, nil)
218 }
219 }
220
221 // Derive the message fields by inspecting the struct fields.
222 for i := 0; i < t.Elem().NumField(); i++ {
223 f := t.Elem().Field(i)
224 if tag := f.Tag.Get("protobuf"); tag != "" {
225 tagKey := f.Tag.Get("protobuf_key")
226 tagVal := f.Tag.Get("protobuf_val")
227 aberrantAppendField(md, f.Type, tag, tagKey, tagVal)
228 }
229 if tag := f.Tag.Get("protobuf_oneof"); tag != "" {
230 n := len(md.L2.Oneofs.List)
231 md.L2.Oneofs.List = append(md.L2.Oneofs.List, filedesc.Oneof{})
232 od := &md.L2.Oneofs.List[n]
233 od.L0.FullName = md.FullName().Append(pref.Name(tag))
234 od.L0.ParentFile = md.L0.ParentFile
235 od.L0.Parent = md
236 od.L0.Index = n
237
238 for _, t := range oneofWrappers {
239 if t.Implements(f.Type) {
240 f := t.Elem().Field(0)
241 if tag := f.Tag.Get("protobuf"); tag != "" {
242 aberrantAppendField(md, f.Type, tag, "", "")
243 fd := &md.L2.Fields.List[len(md.L2.Fields.List)-1]
244 fd.L1.ContainingOneof = od
245 od.L1.Fields.List = append(od.L1.Fields.List, fd)
246 }
247 }
248 }
249 }
250 }
251
Joe Tsai851185d2019-07-01 13:45:52 -0700252 return md
253}
254
Joe Tsaiea5ada12019-09-04 22:41:40 -0700255func aberrantDeriveMessageName(t reflect.Type, name pref.FullName) pref.FullName {
256 if name.IsValid() {
257 return name
258 }
Joe Tsaie87cf532019-09-10 12:20:00 -0700259 func() {
260 defer func() { recover() }() // swallow possible nil panics
Damien Neil47d58932019-09-30 15:34:27 -0700261 if m, ok := reflect.Zero(t).Interface().(interface{ XXX_MessageName() string }); ok {
Joe Tsaie87cf532019-09-10 12:20:00 -0700262 name = pref.FullName(m.XXX_MessageName())
Joe Tsaiea5ada12019-09-04 22:41:40 -0700263 }
Joe Tsaie87cf532019-09-10 12:20:00 -0700264 }()
265 if name.IsValid() {
266 return name
Joe Tsaiea5ada12019-09-04 22:41:40 -0700267 }
Damien Neil47d58932019-09-30 15:34:27 -0700268 if t.Kind() == reflect.Ptr {
269 t = t.Elem()
270 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700271 return aberrantDeriveFullName(t)
272}
273
Joe Tsai851185d2019-07-01 13:45:52 -0700274func aberrantAppendField(md *filedesc.Message, goType reflect.Type, tag, tagKey, tagVal string) {
275 t := goType
276 isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
277 isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
278 if isOptional || isRepeated {
279 t = t.Elem()
280 }
281 fd := ptag.Unmarshal(tag, t, placeholderEnumValues{}).(*filedesc.Field)
282
283 // Append field descriptor to the message.
284 n := len(md.L2.Fields.List)
285 md.L2.Fields.List = append(md.L2.Fields.List, *fd)
286 fd = &md.L2.Fields.List[n]
287 fd.L0.FullName = md.FullName().Append(fd.Name())
288 fd.L0.ParentFile = md.L0.ParentFile
289 fd.L0.Parent = md
290 fd.L0.Index = n
291
292 if fd.L1.IsWeak || fd.L1.HasPacked {
293 fd.L1.Options = func() pref.ProtoMessage {
294 opts := descopts.Field.ProtoReflect().New()
295 if fd.L1.IsWeak {
Joe Tsai84177c92019-09-17 13:38:48 -0700296 opts.Set(opts.Descriptor().Fields().ByName("weak"), protoreflect.ValueOfBool(true))
Joe Tsai851185d2019-07-01 13:45:52 -0700297 }
298 if fd.L1.HasPacked {
Joe Tsai84177c92019-09-17 13:38:48 -0700299 opts.Set(opts.Descriptor().Fields().ByName("packed"), protoreflect.ValueOfBool(fd.L1.IsPacked))
Joe Tsai851185d2019-07-01 13:45:52 -0700300 }
301 return opts.Interface()
302 }
303 }
304
305 // Populate Enum and Message.
306 if fd.Enum() == nil && fd.Kind() == pref.EnumKind {
307 switch v := reflect.Zero(t).Interface().(type) {
308 case pref.Enum:
309 fd.L1.Enum = v.Descriptor()
310 default:
311 fd.L1.Enum = LegacyLoadEnumDesc(t)
312 }
313 }
314 if fd.Message() == nil && (fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind) {
315 switch v := reflect.Zero(t).Interface().(type) {
316 case pref.ProtoMessage:
317 fd.L1.Message = v.ProtoReflect().Descriptor()
Joe Tsai32e8a522019-07-02 10:51:24 -0700318 case messageV1:
319 fd.L1.Message = LegacyLoadMessageDesc(t)
Joe Tsai851185d2019-07-01 13:45:52 -0700320 default:
321 if t.Kind() == reflect.Map {
322 n := len(md.L1.Messages.List)
323 md.L1.Messages.List = append(md.L1.Messages.List, filedesc.Message{L2: new(filedesc.MessageL2)})
324 md2 := &md.L1.Messages.List[n]
Joe Tsai97a87392019-07-07 01:49:59 -0700325 md2.L0.FullName = md.FullName().Append(pref.Name(strs.MapEntryName(string(fd.Name()))))
Joe Tsai851185d2019-07-01 13:45:52 -0700326 md2.L0.ParentFile = md.L0.ParentFile
327 md2.L0.Parent = md
328 md2.L0.Index = n
329
Damien Neile9187322019-11-07 15:30:44 -0800330 md2.L1.IsMapEntry = true
Joe Tsai851185d2019-07-01 13:45:52 -0700331 md2.L2.Options = func() pref.ProtoMessage {
332 opts := descopts.Message.ProtoReflect().New()
Joe Tsai84177c92019-09-17 13:38:48 -0700333 opts.Set(opts.Descriptor().Fields().ByName("map_entry"), protoreflect.ValueOfBool(true))
Joe Tsai851185d2019-07-01 13:45:52 -0700334 return opts.Interface()
335 }
336
337 aberrantAppendField(md2, t.Key(), tagKey, "", "")
338 aberrantAppendField(md2, t.Elem(), tagVal, "", "")
339
340 fd.L1.Message = md2
341 break
342 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700343 fd.L1.Message = aberrantLoadMessageDescReentrant(t, "")
Joe Tsai851185d2019-07-01 13:45:52 -0700344 }
345 }
346}
347
348type placeholderEnumValues struct {
349 protoreflect.EnumValueDescriptors
350}
351
352func (placeholderEnumValues) ByNumber(n pref.EnumNumber) pref.EnumValueDescriptor {
353 return filedesc.PlaceholderEnumValue(pref.FullName(fmt.Sprintf("UNKNOWN_%d", n)))
354}
Damien Neil47d58932019-09-30 15:34:27 -0700355
356// legacyMarshaler is the proto.Marshaler interface superseded by protoiface.Methoder.
357type legacyMarshaler interface {
358 Marshal() ([]byte, error)
359}
360
361// legacyUnmarshaler is the proto.Unmarshaler interface superseded by protoiface.Methoder.
362type legacyUnmarshaler interface {
363 Unmarshal([]byte) error
364}
365
366var legacyProtoMethods = &piface.Methods{
367 Size: legacySize,
368 MarshalAppend: legacyMarshalAppend,
369 Unmarshal: legacyUnmarshal,
Damien Neilc7f2bee2019-11-06 15:29:05 -0800370
371 // We have no way to tell whether the type's Marshal method
372 // supports deterministic serialization or not, but this
373 // preserves the v1 implementation's behavior of always
374 // calling Marshal methods when present.
375 Flags: piface.SupportMarshalDeterministic,
Damien Neil47d58932019-09-30 15:34:27 -0700376}
377
378func legacySize(m protoreflect.Message, opts piface.MarshalOptions) int {
379 b, _ := legacyMarshalAppend(nil, m, opts)
380 return len(b)
381}
382
383func legacyMarshalAppend(b []byte, m protoreflect.Message, opts piface.MarshalOptions) ([]byte, error) {
384 v := m.(unwrapper).protoUnwrap()
385 marshaler, ok := v.(legacyMarshaler)
386 if !ok {
387 return nil, errors.New("%T does not implement Marshal", v)
388 }
389 out, err := marshaler.Marshal()
390 if b != nil {
391 out = append(b, out...)
392 }
393 return out, err
394}
395
396func legacyUnmarshal(b []byte, m protoreflect.Message, opts piface.UnmarshalOptions) error {
397 v := m.(unwrapper).protoUnwrap()
398 unmarshaler, ok := v.(legacyUnmarshaler)
399 if !ok {
400 return errors.New("%T does not implement Marshal", v)
401 }
402 return unmarshaler.Unmarshal(b)
403}
404
405// aberrantMessageType implements MessageType for all types other than pointer-to-struct.
406type aberrantMessageType struct {
407 t reflect.Type
408}
409
410func (mt aberrantMessageType) New() pref.Message {
411 return aberrantMessage{reflect.Zero(mt.t)}
412}
413func (mt aberrantMessageType) Zero() pref.Message {
414 return aberrantMessage{reflect.Zero(mt.t)}
415}
416func (mt aberrantMessageType) GoType() reflect.Type {
417 return mt.t
418}
419func (mt aberrantMessageType) Descriptor() pref.MessageDescriptor {
420 return LegacyLoadMessageDesc(mt.t)
421}
422
423// aberrantMessage implements Message for all types other than pointer-to-struct.
424//
425// When the underlying type implements legacyMarshaler or legacyUnmarshaler,
426// the aberrant Message can be marshaled or unmarshaled. Otherwise, there is
427// not much that can be done with values of this type.
428type aberrantMessage struct {
429 v reflect.Value
430}
431
432func (m aberrantMessage) ProtoReflect() pref.Message {
433 return m
434}
435
436func (m aberrantMessage) Descriptor() pref.MessageDescriptor {
437 return LegacyLoadMessageDesc(m.v.Type())
438}
439func (m aberrantMessage) Type() pref.MessageType {
440 return aberrantMessageType{m.v.Type()}
441}
442func (m aberrantMessage) New() pref.Message {
443 return aberrantMessage{reflect.Zero(m.v.Type())}
444}
445func (m aberrantMessage) Interface() pref.ProtoMessage {
446 return m
447}
448func (m aberrantMessage) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
449}
450func (m aberrantMessage) Has(pref.FieldDescriptor) bool {
451 panic("invalid field descriptor")
452}
453func (m aberrantMessage) Clear(pref.FieldDescriptor) {
454 panic("invalid field descriptor")
455}
456func (m aberrantMessage) Get(pref.FieldDescriptor) pref.Value {
457 panic("invalid field descriptor")
458}
459func (m aberrantMessage) Set(pref.FieldDescriptor, pref.Value) {
460 panic("invalid field descriptor")
461}
462func (m aberrantMessage) Mutable(pref.FieldDescriptor) pref.Value {
463 panic("invalid field descriptor")
464}
465func (m aberrantMessage) NewField(pref.FieldDescriptor) pref.Value {
466 panic("invalid field descriptor")
467}
468func (m aberrantMessage) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor {
469 panic("invalid oneof descriptor")
470}
471func (m aberrantMessage) GetUnknown() pref.RawFields {
472 return nil
473}
474func (m aberrantMessage) SetUnknown(pref.RawFields) {
475 // SetUnknown discards its input on messages which don't support unknown field storage.
476}
Damien Neil82886da2019-11-26 13:27:24 -0800477func (m aberrantMessage) IsValid() bool {
478 // An invalid message is a read-only, empty message. Since we don't know anything
479 // about the alleged contents of this message, we can't say with confidence that
480 // it is invalid in this sense. Therefore, report it as valid.
481 return true
482}
Damien Neil47d58932019-09-30 15:34:27 -0700483func (m aberrantMessage) ProtoMethods() *piface.Methods {
484 return legacyProtoMethods
485}
486func (m aberrantMessage) protoUnwrap() interface{} {
487 return m.v.Interface()
488}