blob: 29daccbf0fe4913df87927387238479509c92988 [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 {
Damien Neil61781dd2020-01-21 13:29:51 -080053 mi.methods.Marshal = legacyMarshal
Damien Neilc7f2bee2019-11-06 15:29:05 -080054
55 // We have no way to tell whether the type's Marshal method
56 // supports deterministic serialization or not, but this
57 // preserves the v1 implementation's behavior of always
58 // calling Marshal methods when present.
59 mi.methods.Flags |= piface.SupportMarshalDeterministic
Damien Neil37ef6912019-09-25 16:51:15 -070060 }
Damien Neil47d58932019-09-30 15:34:27 -070061 if _, ok := v.(legacyUnmarshaler); ok {
62 mi.methods.Unmarshal = legacyUnmarshal
Damien Neil37ef6912019-09-25 16:51:15 -070063 }
64
Damien Neil16163b42019-08-06 15:43:25 -070065 if mi, ok := legacyMessageTypeCache.LoadOrStore(t, mi); ok {
66 return mi.(*MessageInfo)
Joe Tsaib9365042019-03-19 14:14:29 -070067 }
Damien Neil16163b42019-08-06 15:43:25 -070068 return mi
Joe Tsaice6edd32018-10-19 16:27:46 -070069}
70
Joe Tsaid8881392019-06-06 13:01:53 -070071var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor
Joe Tsai90fe9962018-10-18 11:06:29 -070072
Joe Tsai21ade492019-05-22 13:42:54 -040073// LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type,
Joe Tsaice6edd32018-10-19 16:27:46 -070074// which must be a *struct kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -070075//
76// This is exported for testing purposes.
Joe Tsai21ade492019-05-22 13:42:54 -040077func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor {
Joe Tsaiea5ada12019-09-04 22:41:40 -070078 return legacyLoadMessageDesc(t, "")
79}
80func legacyLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070081 // Fast-path: check if a MessageDescriptor is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040082 if mi, ok := legacyMessageDescCache.Load(t); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -070083 return mi.(pref.MessageDescriptor)
84 }
85
Joe Tsaid8881392019-06-06 13:01:53 -070086 // Slow-path: initialize MessageDescriptor from the raw descriptor.
Damien Neil47d58932019-09-30 15:34:27 -070087 mv := reflect.Zero(t).Interface()
Joe Tsai90fe9962018-10-18 11:06:29 -070088 if _, ok := mv.(pref.ProtoMessage); ok {
89 panic(fmt.Sprintf("%v already implements proto.Message", t))
90 }
Joe Tsaid8881392019-06-06 13:01:53 -070091 mdV1, ok := mv.(messageV1)
92 if !ok {
Joe Tsaiea5ada12019-09-04 22:41:40 -070093 return aberrantLoadMessageDesc(t, name)
Joe Tsaid8881392019-06-06 13:01:53 -070094 }
Damien Neil16057752019-11-11 16:30:04 -080095
96 // If this is a dynamic message type where there isn't a 1-1 mapping between
97 // Go and protobuf types, calling the Descriptor method on the zero value of
98 // the message type isn't likely to work. If it panics, swallow the panic and
99 // continue as if the Descriptor method wasn't present.
100 b, idxs := func() ([]byte, []int) {
101 defer func() {
102 recover()
103 }()
104 return mdV1.Descriptor()
105 }()
106 if b == nil {
107 return aberrantLoadMessageDesc(t, name)
108 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700109
Damien Neil4151cae2019-12-09 14:31:23 -0800110 // If the Go type has no fields, then this might be a proto3 empty message
111 // from before the size cache was added. If there are any fields, check to
112 // see that at least one of them looks like something we generated.
113 if nfield := t.Elem().NumField(); nfield > 0 {
114 hasProtoField := false
115 for i := 0; i < nfield; i++ {
116 f := t.Elem().Field(i)
Joe Tsaid6500182019-12-10 17:28:03 -0800117 if f.Tag.Get("protobuf") != "" || f.Tag.Get("protobuf_oneof") != "" || strings.HasPrefix(f.Name, "XXX_") {
Damien Neil4151cae2019-12-09 14:31:23 -0800118 hasProtoField = true
119 break
120 }
121 }
122 if !hasProtoField {
123 return aberrantLoadMessageDesc(t, name)
124 }
125 }
126
Joe Tsaid8881392019-06-06 13:01:53 -0700127 md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
128 for _, i := range idxs[1:] {
129 md = md.Messages().Get(i)
Joe Tsai90fe9962018-10-18 11:06:29 -0700130 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700131 if name != "" && md.FullName() != name {
132 panic(fmt.Sprintf("mismatching message name: got %v, want %v", md.FullName(), name))
133 }
Joe Tsaid8881392019-06-06 13:01:53 -0700134 if md, ok := legacyMessageDescCache.LoadOrStore(t, md); ok {
135 return md.(protoreflect.MessageDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -0700136 }
Joe Tsaid8881392019-06-06 13:01:53 -0700137 return md
Joe Tsai90fe9962018-10-18 11:06:29 -0700138}
Joe Tsai851185d2019-07-01 13:45:52 -0700139
Joe Tsai32e8a522019-07-02 10:51:24 -0700140var (
141 aberrantMessageDescLock sync.Mutex
142 aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor
143)
Joe Tsai851185d2019-07-01 13:45:52 -0700144
Damien Neil47d58932019-09-30 15:34:27 -0700145// aberrantLoadMessageDesc returns an MessageDescriptor derived from the Go type,
Joe Tsai851185d2019-07-01 13:45:52 -0700146// which must not implement protoreflect.ProtoMessage or messageV1.
147//
148// This is a best-effort derivation of the message descriptor using the protobuf
149// tags on the struct fields.
Joe Tsaiea5ada12019-09-04 22:41:40 -0700150func aberrantLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai32e8a522019-07-02 10:51:24 -0700151 aberrantMessageDescLock.Lock()
152 defer aberrantMessageDescLock.Unlock()
153 if aberrantMessageDescCache == nil {
154 aberrantMessageDescCache = make(map[reflect.Type]protoreflect.MessageDescriptor)
155 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700156 return aberrantLoadMessageDescReentrant(t, name)
Joe Tsai32e8a522019-07-02 10:51:24 -0700157}
Joe Tsaiea5ada12019-09-04 22:41:40 -0700158func aberrantLoadMessageDescReentrant(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai851185d2019-07-01 13:45:52 -0700159 // Fast-path: check if an MessageDescriptor is cached for this concrete type.
Joe Tsai32e8a522019-07-02 10:51:24 -0700160 if md, ok := aberrantMessageDescCache[t]; ok {
161 return md
Joe Tsai851185d2019-07-01 13:45:52 -0700162 }
163
Joe Tsai851185d2019-07-01 13:45:52 -0700164 // Slow-path: construct a descriptor from the Go struct type (best-effort).
Joe Tsai32e8a522019-07-02 10:51:24 -0700165 // Cache the MessageDescriptor early on so that we can resolve internal
166 // cyclic references.
167 md := &filedesc.Message{L2: new(filedesc.MessageL2)}
Damien Neil47d58932019-09-30 15:34:27 -0700168 md.L0.FullName = aberrantDeriveMessageName(t, name)
Joe Tsai851185d2019-07-01 13:45:52 -0700169 md.L0.ParentFile = filedesc.SurrogateProto2
Joe Tsai32e8a522019-07-02 10:51:24 -0700170 aberrantMessageDescCache[t] = md
Joe Tsai851185d2019-07-01 13:45:52 -0700171
Damien Neil47d58932019-09-30 15:34:27 -0700172 if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
173 return md
174 }
175
Joe Tsai851185d2019-07-01 13:45:52 -0700176 // Try to determine if the message is using proto3 by checking scalars.
177 for i := 0; i < t.Elem().NumField(); i++ {
178 f := t.Elem().Field(i)
179 if tag := f.Tag.Get("protobuf"); tag != "" {
180 switch f.Type.Kind() {
181 case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
182 md.L0.ParentFile = filedesc.SurrogateProto3
183 }
184 for _, s := range strings.Split(tag, ",") {
185 if s == "proto3" {
186 md.L0.ParentFile = filedesc.SurrogateProto3
187 }
188 }
189 }
190 }
191
192 // Obtain a list of oneof wrapper types.
193 var oneofWrappers []reflect.Type
194 if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok {
195 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3]
196 for _, v := range vs.Interface().([]interface{}) {
197 oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
198 }
199 }
200 if fn, ok := t.MethodByName("XXX_OneofWrappers"); ok {
201 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
202 for _, v := range vs.Interface().([]interface{}) {
203 oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
204 }
205 }
206
207 // Obtain a list of the extension ranges.
208 if fn, ok := t.MethodByName("ExtensionRangeArray"); ok {
209 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
210 for i := 0; i < vs.Len(); i++ {
211 v := vs.Index(i)
212 md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, [2]pref.FieldNumber{
213 pref.FieldNumber(v.FieldByName("Start").Int()),
214 pref.FieldNumber(v.FieldByName("End").Int() + 1),
215 })
216 md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, nil)
217 }
218 }
219
220 // Derive the message fields by inspecting the struct fields.
221 for i := 0; i < t.Elem().NumField(); i++ {
222 f := t.Elem().Field(i)
223 if tag := f.Tag.Get("protobuf"); tag != "" {
224 tagKey := f.Tag.Get("protobuf_key")
225 tagVal := f.Tag.Get("protobuf_val")
226 aberrantAppendField(md, f.Type, tag, tagKey, tagVal)
227 }
228 if tag := f.Tag.Get("protobuf_oneof"); tag != "" {
229 n := len(md.L2.Oneofs.List)
230 md.L2.Oneofs.List = append(md.L2.Oneofs.List, filedesc.Oneof{})
231 od := &md.L2.Oneofs.List[n]
232 od.L0.FullName = md.FullName().Append(pref.Name(tag))
233 od.L0.ParentFile = md.L0.ParentFile
234 od.L0.Parent = md
235 od.L0.Index = n
236
237 for _, t := range oneofWrappers {
238 if t.Implements(f.Type) {
239 f := t.Elem().Field(0)
240 if tag := f.Tag.Get("protobuf"); tag != "" {
241 aberrantAppendField(md, f.Type, tag, "", "")
242 fd := &md.L2.Fields.List[len(md.L2.Fields.List)-1]
243 fd.L1.ContainingOneof = od
244 od.L1.Fields.List = append(od.L1.Fields.List, fd)
245 }
246 }
247 }
248 }
249 }
250
Joe Tsai851185d2019-07-01 13:45:52 -0700251 return md
252}
253
Joe Tsaiea5ada12019-09-04 22:41:40 -0700254func aberrantDeriveMessageName(t reflect.Type, name pref.FullName) pref.FullName {
255 if name.IsValid() {
256 return name
257 }
Joe Tsaie87cf532019-09-10 12:20:00 -0700258 func() {
259 defer func() { recover() }() // swallow possible nil panics
Damien Neil47d58932019-09-30 15:34:27 -0700260 if m, ok := reflect.Zero(t).Interface().(interface{ XXX_MessageName() string }); ok {
Joe Tsaie87cf532019-09-10 12:20:00 -0700261 name = pref.FullName(m.XXX_MessageName())
Joe Tsaiea5ada12019-09-04 22:41:40 -0700262 }
Joe Tsaie87cf532019-09-10 12:20:00 -0700263 }()
264 if name.IsValid() {
265 return name
Joe Tsaiea5ada12019-09-04 22:41:40 -0700266 }
Damien Neil47d58932019-09-30 15:34:27 -0700267 if t.Kind() == reflect.Ptr {
268 t = t.Elem()
269 }
Joe Tsai55f18252020-01-11 00:25:01 -0800270 return AberrantDeriveFullName(t)
Joe Tsaiea5ada12019-09-04 22:41:40 -0700271}
272
Joe Tsai851185d2019-07-01 13:45:52 -0700273func aberrantAppendField(md *filedesc.Message, goType reflect.Type, tag, tagKey, tagVal string) {
274 t := goType
275 isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
276 isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
277 if isOptional || isRepeated {
278 t = t.Elem()
279 }
280 fd := ptag.Unmarshal(tag, t, placeholderEnumValues{}).(*filedesc.Field)
281
282 // Append field descriptor to the message.
283 n := len(md.L2.Fields.List)
284 md.L2.Fields.List = append(md.L2.Fields.List, *fd)
285 fd = &md.L2.Fields.List[n]
286 fd.L0.FullName = md.FullName().Append(fd.Name())
287 fd.L0.ParentFile = md.L0.ParentFile
288 fd.L0.Parent = md
289 fd.L0.Index = n
290
291 if fd.L1.IsWeak || fd.L1.HasPacked {
292 fd.L1.Options = func() pref.ProtoMessage {
293 opts := descopts.Field.ProtoReflect().New()
294 if fd.L1.IsWeak {
Joe Tsai84177c92019-09-17 13:38:48 -0700295 opts.Set(opts.Descriptor().Fields().ByName("weak"), protoreflect.ValueOfBool(true))
Joe Tsai851185d2019-07-01 13:45:52 -0700296 }
297 if fd.L1.HasPacked {
Joe Tsai84177c92019-09-17 13:38:48 -0700298 opts.Set(opts.Descriptor().Fields().ByName("packed"), protoreflect.ValueOfBool(fd.L1.IsPacked))
Joe Tsai851185d2019-07-01 13:45:52 -0700299 }
300 return opts.Interface()
301 }
302 }
303
304 // Populate Enum and Message.
305 if fd.Enum() == nil && fd.Kind() == pref.EnumKind {
306 switch v := reflect.Zero(t).Interface().(type) {
307 case pref.Enum:
308 fd.L1.Enum = v.Descriptor()
309 default:
310 fd.L1.Enum = LegacyLoadEnumDesc(t)
311 }
312 }
313 if fd.Message() == nil && (fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind) {
314 switch v := reflect.Zero(t).Interface().(type) {
315 case pref.ProtoMessage:
316 fd.L1.Message = v.ProtoReflect().Descriptor()
Joe Tsai32e8a522019-07-02 10:51:24 -0700317 case messageV1:
318 fd.L1.Message = LegacyLoadMessageDesc(t)
Joe Tsai851185d2019-07-01 13:45:52 -0700319 default:
320 if t.Kind() == reflect.Map {
321 n := len(md.L1.Messages.List)
322 md.L1.Messages.List = append(md.L1.Messages.List, filedesc.Message{L2: new(filedesc.MessageL2)})
323 md2 := &md.L1.Messages.List[n]
Joe Tsai97a87392019-07-07 01:49:59 -0700324 md2.L0.FullName = md.FullName().Append(pref.Name(strs.MapEntryName(string(fd.Name()))))
Joe Tsai851185d2019-07-01 13:45:52 -0700325 md2.L0.ParentFile = md.L0.ParentFile
326 md2.L0.Parent = md
327 md2.L0.Index = n
328
Damien Neile9187322019-11-07 15:30:44 -0800329 md2.L1.IsMapEntry = true
Joe Tsai851185d2019-07-01 13:45:52 -0700330 md2.L2.Options = func() pref.ProtoMessage {
331 opts := descopts.Message.ProtoReflect().New()
Joe Tsai84177c92019-09-17 13:38:48 -0700332 opts.Set(opts.Descriptor().Fields().ByName("map_entry"), protoreflect.ValueOfBool(true))
Joe Tsai851185d2019-07-01 13:45:52 -0700333 return opts.Interface()
334 }
335
336 aberrantAppendField(md2, t.Key(), tagKey, "", "")
337 aberrantAppendField(md2, t.Elem(), tagVal, "", "")
338
339 fd.L1.Message = md2
340 break
341 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700342 fd.L1.Message = aberrantLoadMessageDescReentrant(t, "")
Joe Tsai851185d2019-07-01 13:45:52 -0700343 }
344 }
345}
346
347type placeholderEnumValues struct {
348 protoreflect.EnumValueDescriptors
349}
350
351func (placeholderEnumValues) ByNumber(n pref.EnumNumber) pref.EnumValueDescriptor {
352 return filedesc.PlaceholderEnumValue(pref.FullName(fmt.Sprintf("UNKNOWN_%d", n)))
353}
Damien Neil47d58932019-09-30 15:34:27 -0700354
355// legacyMarshaler is the proto.Marshaler interface superseded by protoiface.Methoder.
356type legacyMarshaler interface {
357 Marshal() ([]byte, error)
358}
359
360// legacyUnmarshaler is the proto.Unmarshaler interface superseded by protoiface.Methoder.
361type legacyUnmarshaler interface {
362 Unmarshal([]byte) error
363}
364
365var legacyProtoMethods = &piface.Methods{
Damien Neil61781dd2020-01-21 13:29:51 -0800366 Marshal: legacyMarshal,
367 Unmarshal: legacyUnmarshal,
Damien Neilc7f2bee2019-11-06 15:29:05 -0800368
369 // We have no way to tell whether the type's Marshal method
370 // supports deterministic serialization or not, but this
371 // preserves the v1 implementation's behavior of always
372 // calling Marshal methods when present.
373 Flags: piface.SupportMarshalDeterministic,
Damien Neil47d58932019-09-30 15:34:27 -0700374}
375
Damien Neild30e5612020-01-22 10:28:16 -0800376func legacyMarshal(m protoreflect.Message, in piface.MarshalInput, opts piface.MarshalOptions) (piface.MarshalOutput, error) {
Damien Neil47d58932019-09-30 15:34:27 -0700377 v := m.(unwrapper).protoUnwrap()
378 marshaler, ok := v.(legacyMarshaler)
379 if !ok {
Damien Neil61781dd2020-01-21 13:29:51 -0800380 return piface.MarshalOutput{}, errors.New("%T does not implement Marshal", v)
Damien Neil47d58932019-09-30 15:34:27 -0700381 }
382 out, err := marshaler.Marshal()
Damien Neil61781dd2020-01-21 13:29:51 -0800383 if in.Buf != nil {
384 out = append(in.Buf, out...)
Damien Neil47d58932019-09-30 15:34:27 -0700385 }
Damien Neil61781dd2020-01-21 13:29:51 -0800386 return piface.MarshalOutput{
387 Buf: out,
388 }, err
Damien Neil47d58932019-09-30 15:34:27 -0700389}
390
Damien Neild30e5612020-01-22 10:28:16 -0800391func legacyUnmarshal(m protoreflect.Message, in piface.UnmarshalInput, opts piface.UnmarshalOptions) (piface.UnmarshalOutput, error) {
Damien Neil47d58932019-09-30 15:34:27 -0700392 v := m.(unwrapper).protoUnwrap()
393 unmarshaler, ok := v.(legacyUnmarshaler)
394 if !ok {
Damien Neil61781dd2020-01-21 13:29:51 -0800395 return piface.UnmarshalOutput{}, errors.New("%T does not implement Marshal", v)
Damien Neil47d58932019-09-30 15:34:27 -0700396 }
Damien Neil61781dd2020-01-21 13:29:51 -0800397 return piface.UnmarshalOutput{}, unmarshaler.Unmarshal(in.Buf)
Damien Neil47d58932019-09-30 15:34:27 -0700398}
399
400// aberrantMessageType implements MessageType for all types other than pointer-to-struct.
401type aberrantMessageType struct {
402 t reflect.Type
403}
404
405func (mt aberrantMessageType) New() pref.Message {
406 return aberrantMessage{reflect.Zero(mt.t)}
407}
408func (mt aberrantMessageType) Zero() pref.Message {
409 return aberrantMessage{reflect.Zero(mt.t)}
410}
411func (mt aberrantMessageType) GoType() reflect.Type {
412 return mt.t
413}
414func (mt aberrantMessageType) Descriptor() pref.MessageDescriptor {
415 return LegacyLoadMessageDesc(mt.t)
416}
417
418// aberrantMessage implements Message for all types other than pointer-to-struct.
419//
420// When the underlying type implements legacyMarshaler or legacyUnmarshaler,
421// the aberrant Message can be marshaled or unmarshaled. Otherwise, there is
422// not much that can be done with values of this type.
423type aberrantMessage struct {
424 v reflect.Value
425}
426
427func (m aberrantMessage) ProtoReflect() pref.Message {
428 return m
429}
430
431func (m aberrantMessage) Descriptor() pref.MessageDescriptor {
432 return LegacyLoadMessageDesc(m.v.Type())
433}
434func (m aberrantMessage) Type() pref.MessageType {
435 return aberrantMessageType{m.v.Type()}
436}
437func (m aberrantMessage) New() pref.Message {
438 return aberrantMessage{reflect.Zero(m.v.Type())}
439}
440func (m aberrantMessage) Interface() pref.ProtoMessage {
441 return m
442}
443func (m aberrantMessage) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
444}
445func (m aberrantMessage) Has(pref.FieldDescriptor) bool {
446 panic("invalid field descriptor")
447}
448func (m aberrantMessage) Clear(pref.FieldDescriptor) {
449 panic("invalid field descriptor")
450}
451func (m aberrantMessage) Get(pref.FieldDescriptor) pref.Value {
452 panic("invalid field descriptor")
453}
454func (m aberrantMessage) Set(pref.FieldDescriptor, pref.Value) {
455 panic("invalid field descriptor")
456}
457func (m aberrantMessage) Mutable(pref.FieldDescriptor) pref.Value {
458 panic("invalid field descriptor")
459}
460func (m aberrantMessage) NewField(pref.FieldDescriptor) pref.Value {
461 panic("invalid field descriptor")
462}
463func (m aberrantMessage) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor {
464 panic("invalid oneof descriptor")
465}
466func (m aberrantMessage) GetUnknown() pref.RawFields {
467 return nil
468}
469func (m aberrantMessage) SetUnknown(pref.RawFields) {
470 // SetUnknown discards its input on messages which don't support unknown field storage.
471}
Damien Neil82886da2019-11-26 13:27:24 -0800472func (m aberrantMessage) IsValid() bool {
473 // An invalid message is a read-only, empty message. Since we don't know anything
474 // about the alleged contents of this message, we can't say with confidence that
475 // it is invalid in this sense. Therefore, report it as valid.
476 return true
477}
Damien Neil47d58932019-09-30 15:34:27 -0700478func (m aberrantMessage) ProtoMethods() *piface.Methods {
479 return legacyProtoMethods
480}
481func (m aberrantMessage) protoUnwrap() interface{} {
482 return m.v.Interface()
483}