blob: 43b8be2e32ecc0115bf4a77650f5974005995203 [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 Neil725bfea2020-02-14 16:48:37 -080020 "google.golang.org/protobuf/runtime/protoiface"
Damien Neil37ef6912019-09-25 16:51:15 -070021 piface "google.golang.org/protobuf/runtime/protoiface"
Joe Tsai90fe9962018-10-18 11:06:29 -070022)
23
Joe Tsai21ade492019-05-22 13:42:54 -040024// legacyWrapMessage wraps v as a protoreflect.ProtoMessage,
Joe Tsaif0c01e42018-11-06 13:05:20 -080025// where v must be a *struct kind and not implement the v2 API already.
Joe Tsai21ade492019-05-22 13:42:54 -040026func legacyWrapMessage(v reflect.Value) pref.ProtoMessage {
Damien Neil47d58932019-09-30 15:34:27 -070027 typ := v.Type()
28 if typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Struct {
29 return aberrantMessage{v: v}
30 }
31 mt := legacyLoadMessageInfo(typ, "")
Joe Tsai08e00302018-11-26 22:32:06 -080032 return mt.MessageOf(v.Interface()).Interface()
Joe Tsaif0c01e42018-11-06 13:05:20 -080033}
34
Joe Tsai21ade492019-05-22 13:42:54 -040035var legacyMessageTypeCache sync.Map // map[reflect.Type]*MessageInfo
Joe Tsaice6edd32018-10-19 16:27:46 -070036
Joe Tsai21ade492019-05-22 13:42:54 -040037// legacyLoadMessageInfo dynamically loads a *MessageInfo for t,
Joe Tsaif0c01e42018-11-06 13:05:20 -080038// where t must be a *struct kind and not implement the v2 API already.
Joe Tsaiea5ada12019-09-04 22:41:40 -070039// The provided name is used if it cannot be determined from the message.
40func legacyLoadMessageInfo(t reflect.Type, name pref.FullName) *MessageInfo {
Joe Tsai4fe96632019-05-22 05:12:36 -040041 // Fast-path: check if a MessageInfo is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040042 if mt, ok := legacyMessageTypeCache.Load(t); ok {
43 return mt.(*MessageInfo)
Joe Tsaice6edd32018-10-19 16:27:46 -070044 }
45
Joe Tsai4fe96632019-05-22 05:12:36 -040046 // Slow-path: derive message descriptor and initialize MessageInfo.
Damien Neil16163b42019-08-06 15:43:25 -070047 mi := &MessageInfo{
Joe Tsaiea5ada12019-09-04 22:41:40 -070048 Desc: legacyLoadMessageDesc(t, name),
Damien Neil16163b42019-08-06 15:43:25 -070049 GoReflectType: t,
Joe Tsaib2f66be2019-05-22 00:42:45 -040050 }
Damien Neil37ef6912019-09-25 16:51:15 -070051
52 v := reflect.Zero(t).Interface()
Damien Neil47d58932019-09-30 15:34:27 -070053 if _, ok := v.(legacyMarshaler); ok {
Damien Neil61781dd2020-01-21 13:29:51 -080054 mi.methods.Marshal = legacyMarshal
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 }
Damien Neil725bfea2020-02-14 16:48:37 -080065 if _, ok := v.(legacyMerger); ok {
66 mi.methods.Merge = legacyMerge
67 }
Damien Neil37ef6912019-09-25 16:51:15 -070068
Damien Neil16163b42019-08-06 15:43:25 -070069 if mi, ok := legacyMessageTypeCache.LoadOrStore(t, mi); ok {
70 return mi.(*MessageInfo)
Joe Tsaib9365042019-03-19 14:14:29 -070071 }
Damien Neil16163b42019-08-06 15:43:25 -070072 return mi
Joe Tsaice6edd32018-10-19 16:27:46 -070073}
74
Joe Tsaid8881392019-06-06 13:01:53 -070075var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor
Joe Tsai90fe9962018-10-18 11:06:29 -070076
Joe Tsai21ade492019-05-22 13:42:54 -040077// LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type,
Joe Tsaice6edd32018-10-19 16:27:46 -070078// which must be a *struct kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -070079//
80// This is exported for testing purposes.
Joe Tsai21ade492019-05-22 13:42:54 -040081func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor {
Joe Tsaiea5ada12019-09-04 22:41:40 -070082 return legacyLoadMessageDesc(t, "")
83}
84func legacyLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070085 // Fast-path: check if a MessageDescriptor is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040086 if mi, ok := legacyMessageDescCache.Load(t); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -070087 return mi.(pref.MessageDescriptor)
88 }
89
Joe Tsaid8881392019-06-06 13:01:53 -070090 // Slow-path: initialize MessageDescriptor from the raw descriptor.
Damien Neil47d58932019-09-30 15:34:27 -070091 mv := reflect.Zero(t).Interface()
Joe Tsai90fe9962018-10-18 11:06:29 -070092 if _, ok := mv.(pref.ProtoMessage); ok {
93 panic(fmt.Sprintf("%v already implements proto.Message", t))
94 }
Joe Tsaid8881392019-06-06 13:01:53 -070095 mdV1, ok := mv.(messageV1)
96 if !ok {
Joe Tsaiea5ada12019-09-04 22:41:40 -070097 return aberrantLoadMessageDesc(t, name)
Joe Tsaid8881392019-06-06 13:01:53 -070098 }
Damien Neil16057752019-11-11 16:30:04 -080099
100 // If this is a dynamic message type where there isn't a 1-1 mapping between
101 // Go and protobuf types, calling the Descriptor method on the zero value of
102 // the message type isn't likely to work. If it panics, swallow the panic and
103 // continue as if the Descriptor method wasn't present.
104 b, idxs := func() ([]byte, []int) {
105 defer func() {
106 recover()
107 }()
108 return mdV1.Descriptor()
109 }()
110 if b == nil {
111 return aberrantLoadMessageDesc(t, name)
112 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700113
Damien Neil4151cae2019-12-09 14:31:23 -0800114 // If the Go type has no fields, then this might be a proto3 empty message
115 // from before the size cache was added. If there are any fields, check to
116 // see that at least one of them looks like something we generated.
117 if nfield := t.Elem().NumField(); nfield > 0 {
118 hasProtoField := false
119 for i := 0; i < nfield; i++ {
120 f := t.Elem().Field(i)
Joe Tsaid6500182019-12-10 17:28:03 -0800121 if f.Tag.Get("protobuf") != "" || f.Tag.Get("protobuf_oneof") != "" || strings.HasPrefix(f.Name, "XXX_") {
Damien Neil4151cae2019-12-09 14:31:23 -0800122 hasProtoField = true
123 break
124 }
125 }
126 if !hasProtoField {
127 return aberrantLoadMessageDesc(t, name)
128 }
129 }
130
Joe Tsaid8881392019-06-06 13:01:53 -0700131 md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
132 for _, i := range idxs[1:] {
133 md = md.Messages().Get(i)
Joe Tsai90fe9962018-10-18 11:06:29 -0700134 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700135 if name != "" && md.FullName() != name {
136 panic(fmt.Sprintf("mismatching message name: got %v, want %v", md.FullName(), name))
137 }
Joe Tsaid8881392019-06-06 13:01:53 -0700138 if md, ok := legacyMessageDescCache.LoadOrStore(t, md); ok {
139 return md.(protoreflect.MessageDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -0700140 }
Joe Tsaid8881392019-06-06 13:01:53 -0700141 return md
Joe Tsai90fe9962018-10-18 11:06:29 -0700142}
Joe Tsai851185d2019-07-01 13:45:52 -0700143
Joe Tsai32e8a522019-07-02 10:51:24 -0700144var (
145 aberrantMessageDescLock sync.Mutex
146 aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor
147)
Joe Tsai851185d2019-07-01 13:45:52 -0700148
Damien Neil47d58932019-09-30 15:34:27 -0700149// aberrantLoadMessageDesc returns an MessageDescriptor derived from the Go type,
Joe Tsai851185d2019-07-01 13:45:52 -0700150// which must not implement protoreflect.ProtoMessage or messageV1.
151//
152// This is a best-effort derivation of the message descriptor using the protobuf
153// tags on the struct fields.
Joe Tsaiea5ada12019-09-04 22:41:40 -0700154func aberrantLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai32e8a522019-07-02 10:51:24 -0700155 aberrantMessageDescLock.Lock()
156 defer aberrantMessageDescLock.Unlock()
157 if aberrantMessageDescCache == nil {
158 aberrantMessageDescCache = make(map[reflect.Type]protoreflect.MessageDescriptor)
159 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700160 return aberrantLoadMessageDescReentrant(t, name)
Joe Tsai32e8a522019-07-02 10:51:24 -0700161}
Joe Tsaiea5ada12019-09-04 22:41:40 -0700162func aberrantLoadMessageDescReentrant(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
Joe Tsai851185d2019-07-01 13:45:52 -0700163 // Fast-path: check if an MessageDescriptor is cached for this concrete type.
Joe Tsai32e8a522019-07-02 10:51:24 -0700164 if md, ok := aberrantMessageDescCache[t]; ok {
165 return md
Joe Tsai851185d2019-07-01 13:45:52 -0700166 }
167
Joe Tsai851185d2019-07-01 13:45:52 -0700168 // Slow-path: construct a descriptor from the Go struct type (best-effort).
Joe Tsai32e8a522019-07-02 10:51:24 -0700169 // Cache the MessageDescriptor early on so that we can resolve internal
170 // cyclic references.
171 md := &filedesc.Message{L2: new(filedesc.MessageL2)}
Damien Neil47d58932019-09-30 15:34:27 -0700172 md.L0.FullName = aberrantDeriveMessageName(t, name)
Joe Tsai851185d2019-07-01 13:45:52 -0700173 md.L0.ParentFile = filedesc.SurrogateProto2
Joe Tsai32e8a522019-07-02 10:51:24 -0700174 aberrantMessageDescCache[t] = md
Joe Tsai851185d2019-07-01 13:45:52 -0700175
Damien Neil47d58932019-09-30 15:34:27 -0700176 if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
177 return md
178 }
179
Joe Tsai851185d2019-07-01 13:45:52 -0700180 // Try to determine if the message is using proto3 by checking scalars.
181 for i := 0; i < t.Elem().NumField(); i++ {
182 f := t.Elem().Field(i)
183 if tag := f.Tag.Get("protobuf"); tag != "" {
184 switch f.Type.Kind() {
185 case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
186 md.L0.ParentFile = filedesc.SurrogateProto3
187 }
188 for _, s := range strings.Split(tag, ",") {
189 if s == "proto3" {
190 md.L0.ParentFile = filedesc.SurrogateProto3
191 }
192 }
193 }
194 }
195
196 // Obtain a list of oneof wrapper types.
197 var oneofWrappers []reflect.Type
198 if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok {
199 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3]
200 for _, v := range vs.Interface().([]interface{}) {
201 oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
202 }
203 }
204 if fn, ok := t.MethodByName("XXX_OneofWrappers"); ok {
205 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
206 for _, v := range vs.Interface().([]interface{}) {
207 oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
208 }
209 }
210
211 // Obtain a list of the extension ranges.
212 if fn, ok := t.MethodByName("ExtensionRangeArray"); ok {
213 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
214 for i := 0; i < vs.Len(); i++ {
215 v := vs.Index(i)
216 md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, [2]pref.FieldNumber{
217 pref.FieldNumber(v.FieldByName("Start").Int()),
218 pref.FieldNumber(v.FieldByName("End").Int() + 1),
219 })
220 md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, nil)
221 }
222 }
223
224 // Derive the message fields by inspecting the struct fields.
225 for i := 0; i < t.Elem().NumField(); i++ {
226 f := t.Elem().Field(i)
227 if tag := f.Tag.Get("protobuf"); tag != "" {
228 tagKey := f.Tag.Get("protobuf_key")
229 tagVal := f.Tag.Get("protobuf_val")
230 aberrantAppendField(md, f.Type, tag, tagKey, tagVal)
231 }
232 if tag := f.Tag.Get("protobuf_oneof"); tag != "" {
233 n := len(md.L2.Oneofs.List)
234 md.L2.Oneofs.List = append(md.L2.Oneofs.List, filedesc.Oneof{})
235 od := &md.L2.Oneofs.List[n]
236 od.L0.FullName = md.FullName().Append(pref.Name(tag))
237 od.L0.ParentFile = md.L0.ParentFile
238 od.L0.Parent = md
239 od.L0.Index = n
240
241 for _, t := range oneofWrappers {
242 if t.Implements(f.Type) {
243 f := t.Elem().Field(0)
244 if tag := f.Tag.Get("protobuf"); tag != "" {
245 aberrantAppendField(md, f.Type, tag, "", "")
246 fd := &md.L2.Fields.List[len(md.L2.Fields.List)-1]
247 fd.L1.ContainingOneof = od
248 od.L1.Fields.List = append(od.L1.Fields.List, fd)
249 }
250 }
251 }
252 }
253 }
254
Joe Tsai851185d2019-07-01 13:45:52 -0700255 return md
256}
257
Joe Tsaiea5ada12019-09-04 22:41:40 -0700258func aberrantDeriveMessageName(t reflect.Type, name pref.FullName) pref.FullName {
259 if name.IsValid() {
260 return name
261 }
Joe Tsaie87cf532019-09-10 12:20:00 -0700262 func() {
263 defer func() { recover() }() // swallow possible nil panics
Damien Neil47d58932019-09-30 15:34:27 -0700264 if m, ok := reflect.Zero(t).Interface().(interface{ XXX_MessageName() string }); ok {
Joe Tsaie87cf532019-09-10 12:20:00 -0700265 name = pref.FullName(m.XXX_MessageName())
Joe Tsaiea5ada12019-09-04 22:41:40 -0700266 }
Joe Tsaie87cf532019-09-10 12:20:00 -0700267 }()
268 if name.IsValid() {
269 return name
Joe Tsaiea5ada12019-09-04 22:41:40 -0700270 }
Damien Neil47d58932019-09-30 15:34:27 -0700271 if t.Kind() == reflect.Ptr {
272 t = t.Elem()
273 }
Joe Tsai55f18252020-01-11 00:25:01 -0800274 return AberrantDeriveFullName(t)
Joe Tsaiea5ada12019-09-04 22:41:40 -0700275}
276
Joe Tsai851185d2019-07-01 13:45:52 -0700277func aberrantAppendField(md *filedesc.Message, goType reflect.Type, tag, tagKey, tagVal string) {
278 t := goType
279 isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
280 isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
281 if isOptional || isRepeated {
282 t = t.Elem()
283 }
284 fd := ptag.Unmarshal(tag, t, placeholderEnumValues{}).(*filedesc.Field)
285
286 // Append field descriptor to the message.
287 n := len(md.L2.Fields.List)
288 md.L2.Fields.List = append(md.L2.Fields.List, *fd)
289 fd = &md.L2.Fields.List[n]
290 fd.L0.FullName = md.FullName().Append(fd.Name())
291 fd.L0.ParentFile = md.L0.ParentFile
292 fd.L0.Parent = md
293 fd.L0.Index = n
294
295 if fd.L1.IsWeak || fd.L1.HasPacked {
296 fd.L1.Options = func() pref.ProtoMessage {
297 opts := descopts.Field.ProtoReflect().New()
298 if fd.L1.IsWeak {
Joe Tsai84177c92019-09-17 13:38:48 -0700299 opts.Set(opts.Descriptor().Fields().ByName("weak"), protoreflect.ValueOfBool(true))
Joe Tsai851185d2019-07-01 13:45:52 -0700300 }
301 if fd.L1.HasPacked {
Joe Tsai84177c92019-09-17 13:38:48 -0700302 opts.Set(opts.Descriptor().Fields().ByName("packed"), protoreflect.ValueOfBool(fd.L1.IsPacked))
Joe Tsai851185d2019-07-01 13:45:52 -0700303 }
304 return opts.Interface()
305 }
306 }
307
308 // Populate Enum and Message.
309 if fd.Enum() == nil && fd.Kind() == pref.EnumKind {
310 switch v := reflect.Zero(t).Interface().(type) {
311 case pref.Enum:
312 fd.L1.Enum = v.Descriptor()
313 default:
314 fd.L1.Enum = LegacyLoadEnumDesc(t)
315 }
316 }
317 if fd.Message() == nil && (fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind) {
318 switch v := reflect.Zero(t).Interface().(type) {
319 case pref.ProtoMessage:
320 fd.L1.Message = v.ProtoReflect().Descriptor()
Joe Tsai32e8a522019-07-02 10:51:24 -0700321 case messageV1:
322 fd.L1.Message = LegacyLoadMessageDesc(t)
Joe Tsai851185d2019-07-01 13:45:52 -0700323 default:
324 if t.Kind() == reflect.Map {
325 n := len(md.L1.Messages.List)
326 md.L1.Messages.List = append(md.L1.Messages.List, filedesc.Message{L2: new(filedesc.MessageL2)})
327 md2 := &md.L1.Messages.List[n]
Joe Tsai97a87392019-07-07 01:49:59 -0700328 md2.L0.FullName = md.FullName().Append(pref.Name(strs.MapEntryName(string(fd.Name()))))
Joe Tsai851185d2019-07-01 13:45:52 -0700329 md2.L0.ParentFile = md.L0.ParentFile
330 md2.L0.Parent = md
331 md2.L0.Index = n
332
Damien Neile9187322019-11-07 15:30:44 -0800333 md2.L1.IsMapEntry = true
Joe Tsai851185d2019-07-01 13:45:52 -0700334 md2.L2.Options = func() pref.ProtoMessage {
335 opts := descopts.Message.ProtoReflect().New()
Joe Tsai84177c92019-09-17 13:38:48 -0700336 opts.Set(opts.Descriptor().Fields().ByName("map_entry"), protoreflect.ValueOfBool(true))
Joe Tsai851185d2019-07-01 13:45:52 -0700337 return opts.Interface()
338 }
339
340 aberrantAppendField(md2, t.Key(), tagKey, "", "")
341 aberrantAppendField(md2, t.Elem(), tagVal, "", "")
342
343 fd.L1.Message = md2
344 break
345 }
Joe Tsaiea5ada12019-09-04 22:41:40 -0700346 fd.L1.Message = aberrantLoadMessageDescReentrant(t, "")
Joe Tsai851185d2019-07-01 13:45:52 -0700347 }
348 }
349}
350
351type placeholderEnumValues struct {
352 protoreflect.EnumValueDescriptors
353}
354
355func (placeholderEnumValues) ByNumber(n pref.EnumNumber) pref.EnumValueDescriptor {
356 return filedesc.PlaceholderEnumValue(pref.FullName(fmt.Sprintf("UNKNOWN_%d", n)))
357}
Damien Neil47d58932019-09-30 15:34:27 -0700358
359// legacyMarshaler is the proto.Marshaler interface superseded by protoiface.Methoder.
360type legacyMarshaler interface {
361 Marshal() ([]byte, error)
362}
363
364// legacyUnmarshaler is the proto.Unmarshaler interface superseded by protoiface.Methoder.
365type legacyUnmarshaler interface {
366 Unmarshal([]byte) error
367}
368
Damien Neil725bfea2020-02-14 16:48:37 -0800369// legacyMerger is the proto.Merger interface superseded by protoiface.Methoder.
370type legacyMerger interface {
371 Merge(protoiface.MessageV1)
372}
373
Damien Neil47d58932019-09-30 15:34:27 -0700374var legacyProtoMethods = &piface.Methods{
Damien Neil61781dd2020-01-21 13:29:51 -0800375 Marshal: legacyMarshal,
376 Unmarshal: legacyUnmarshal,
Damien Neil725bfea2020-02-14 16:48:37 -0800377 Merge: legacyMerge,
Damien Neilc7f2bee2019-11-06 15:29:05 -0800378
379 // We have no way to tell whether the type's Marshal method
380 // supports deterministic serialization or not, but this
381 // preserves the v1 implementation's behavior of always
382 // calling Marshal methods when present.
383 Flags: piface.SupportMarshalDeterministic,
Damien Neil47d58932019-09-30 15:34:27 -0700384}
385
Damien Neil466dd772020-02-14 14:49:35 -0800386func legacyMarshal(in piface.MarshalInput) (piface.MarshalOutput, error) {
387 v := in.Message.(unwrapper).protoUnwrap()
Damien Neil47d58932019-09-30 15:34:27 -0700388 marshaler, ok := v.(legacyMarshaler)
389 if !ok {
Damien Neil61781dd2020-01-21 13:29:51 -0800390 return piface.MarshalOutput{}, errors.New("%T does not implement Marshal", v)
Damien Neil47d58932019-09-30 15:34:27 -0700391 }
392 out, err := marshaler.Marshal()
Damien Neil61781dd2020-01-21 13:29:51 -0800393 if in.Buf != nil {
394 out = append(in.Buf, out...)
Damien Neil47d58932019-09-30 15:34:27 -0700395 }
Damien Neil61781dd2020-01-21 13:29:51 -0800396 return piface.MarshalOutput{
397 Buf: out,
398 }, err
Damien Neil47d58932019-09-30 15:34:27 -0700399}
400
Damien Neil466dd772020-02-14 14:49:35 -0800401func legacyUnmarshal(in piface.UnmarshalInput) (piface.UnmarshalOutput, error) {
402 v := in.Message.(unwrapper).protoUnwrap()
Damien Neil47d58932019-09-30 15:34:27 -0700403 unmarshaler, ok := v.(legacyUnmarshaler)
404 if !ok {
Damien Neil61781dd2020-01-21 13:29:51 -0800405 return piface.UnmarshalOutput{}, errors.New("%T does not implement Marshal", v)
Damien Neil47d58932019-09-30 15:34:27 -0700406 }
Damien Neil61781dd2020-01-21 13:29:51 -0800407 return piface.UnmarshalOutput{}, unmarshaler.Unmarshal(in.Buf)
Damien Neil47d58932019-09-30 15:34:27 -0700408}
409
Damien Neil466dd772020-02-14 14:49:35 -0800410func legacyMerge(in piface.MergeInput) piface.MergeOutput {
411 dstv := in.Destination.(unwrapper).protoUnwrap()
Damien Neil725bfea2020-02-14 16:48:37 -0800412 merger, ok := dstv.(legacyMerger)
413 if !ok {
414 return piface.MergeOutput{}
415 }
Damien Neil466dd772020-02-14 14:49:35 -0800416 merger.Merge(Export{}.ProtoMessageV1Of(in.Source))
417 return piface.MergeOutput{Flags: piface.MergeComplete}
Damien Neil725bfea2020-02-14 16:48:37 -0800418}
419
Damien Neil47d58932019-09-30 15:34:27 -0700420// aberrantMessageType implements MessageType for all types other than pointer-to-struct.
421type aberrantMessageType struct {
422 t reflect.Type
423}
424
425func (mt aberrantMessageType) New() pref.Message {
426 return aberrantMessage{reflect.Zero(mt.t)}
427}
428func (mt aberrantMessageType) Zero() pref.Message {
429 return aberrantMessage{reflect.Zero(mt.t)}
430}
431func (mt aberrantMessageType) GoType() reflect.Type {
432 return mt.t
433}
434func (mt aberrantMessageType) Descriptor() pref.MessageDescriptor {
435 return LegacyLoadMessageDesc(mt.t)
436}
437
438// aberrantMessage implements Message for all types other than pointer-to-struct.
439//
440// When the underlying type implements legacyMarshaler or legacyUnmarshaler,
441// the aberrant Message can be marshaled or unmarshaled. Otherwise, there is
442// not much that can be done with values of this type.
443type aberrantMessage struct {
444 v reflect.Value
445}
446
447func (m aberrantMessage) ProtoReflect() pref.Message {
448 return m
449}
450
451func (m aberrantMessage) Descriptor() pref.MessageDescriptor {
452 return LegacyLoadMessageDesc(m.v.Type())
453}
454func (m aberrantMessage) Type() pref.MessageType {
455 return aberrantMessageType{m.v.Type()}
456}
457func (m aberrantMessage) New() pref.Message {
458 return aberrantMessage{reflect.Zero(m.v.Type())}
459}
460func (m aberrantMessage) Interface() pref.ProtoMessage {
461 return m
462}
463func (m aberrantMessage) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
464}
465func (m aberrantMessage) Has(pref.FieldDescriptor) bool {
466 panic("invalid field descriptor")
467}
468func (m aberrantMessage) Clear(pref.FieldDescriptor) {
469 panic("invalid field descriptor")
470}
471func (m aberrantMessage) Get(pref.FieldDescriptor) pref.Value {
472 panic("invalid field descriptor")
473}
474func (m aberrantMessage) Set(pref.FieldDescriptor, pref.Value) {
475 panic("invalid field descriptor")
476}
477func (m aberrantMessage) Mutable(pref.FieldDescriptor) pref.Value {
478 panic("invalid field descriptor")
479}
480func (m aberrantMessage) NewField(pref.FieldDescriptor) pref.Value {
481 panic("invalid field descriptor")
482}
483func (m aberrantMessage) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor {
484 panic("invalid oneof descriptor")
485}
486func (m aberrantMessage) GetUnknown() pref.RawFields {
487 return nil
488}
489func (m aberrantMessage) SetUnknown(pref.RawFields) {
490 // SetUnknown discards its input on messages which don't support unknown field storage.
491}
Damien Neil82886da2019-11-26 13:27:24 -0800492func (m aberrantMessage) IsValid() bool {
493 // An invalid message is a read-only, empty message. Since we don't know anything
494 // about the alleged contents of this message, we can't say with confidence that
495 // it is invalid in this sense. Therefore, report it as valid.
496 return true
497}
Damien Neil47d58932019-09-30 15:34:27 -0700498func (m aberrantMessage) ProtoMethods() *piface.Methods {
499 return legacyProtoMethods
500}
501func (m aberrantMessage) protoUnwrap() interface{} {
502 return m.v.Interface()
503}