Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 1 | // 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 Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 5 | package impl |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "fmt" |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 9 | "reflect" |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 10 | "strings" |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 11 | "sync" |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 12 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 13 | "google.golang.org/protobuf/internal/descopts" |
| 14 | ptag "google.golang.org/protobuf/internal/encoding/tag" |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 15 | "google.golang.org/protobuf/internal/errors" |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 16 | "google.golang.org/protobuf/internal/filedesc" |
Joe Tsai | 97a8739 | 2019-07-07 01:49:59 -0700 | [diff] [blame] | 17 | "google.golang.org/protobuf/internal/strs" |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 18 | "google.golang.org/protobuf/reflect/protoreflect" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 19 | pref "google.golang.org/protobuf/reflect/protoreflect" |
Damien Neil | 37ef691 | 2019-09-25 16:51:15 -0700 | [diff] [blame] | 20 | piface "google.golang.org/protobuf/runtime/protoiface" |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 23 | // legacyWrapMessage wraps v as a protoreflect.ProtoMessage, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 24 | // where v must be a *struct kind and not implement the v2 API already. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 25 | func legacyWrapMessage(v reflect.Value) pref.ProtoMessage { |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 26 | 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 Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 31 | return mt.MessageOf(v.Interface()).Interface() |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 34 | var legacyMessageTypeCache sync.Map // map[reflect.Type]*MessageInfo |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 35 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 36 | // legacyLoadMessageInfo dynamically loads a *MessageInfo for t, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 37 | // where t must be a *struct kind and not implement the v2 API already. |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 38 | // The provided name is used if it cannot be determined from the message. |
| 39 | func legacyLoadMessageInfo(t reflect.Type, name pref.FullName) *MessageInfo { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 40 | // Fast-path: check if a MessageInfo is cached for this concrete type. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 41 | if mt, ok := legacyMessageTypeCache.Load(t); ok { |
| 42 | return mt.(*MessageInfo) |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 45 | // Slow-path: derive message descriptor and initialize MessageInfo. |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame] | 46 | mi := &MessageInfo{ |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 47 | Desc: legacyLoadMessageDesc(t, name), |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame] | 48 | GoReflectType: t, |
Joe Tsai | b2f66be | 2019-05-22 00:42:45 -0400 | [diff] [blame] | 49 | } |
Damien Neil | 37ef691 | 2019-09-25 16:51:15 -0700 | [diff] [blame] | 50 | |
| 51 | v := reflect.Zero(t).Interface() |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 52 | if _, ok := v.(legacyMarshaler); ok { |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame] | 53 | mi.methods.Marshal = legacyMarshal |
Damien Neil | c7f2bee | 2019-11-06 15:29:05 -0800 | [diff] [blame] | 54 | |
| 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 Neil | 37ef691 | 2019-09-25 16:51:15 -0700 | [diff] [blame] | 60 | } |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 61 | if _, ok := v.(legacyUnmarshaler); ok { |
| 62 | mi.methods.Unmarshal = legacyUnmarshal |
Damien Neil | 37ef691 | 2019-09-25 16:51:15 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame] | 65 | if mi, ok := legacyMessageTypeCache.LoadOrStore(t, mi); ok { |
| 66 | return mi.(*MessageInfo) |
Joe Tsai | b936504 | 2019-03-19 14:14:29 -0700 | [diff] [blame] | 67 | } |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame] | 68 | return mi |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 71 | var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 72 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 73 | // LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type, |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 74 | // which must be a *struct kind and not implement the v2 API already. |
Joe Tsai | 35ec98f | 2019-03-25 14:41:32 -0700 | [diff] [blame] | 75 | // |
| 76 | // This is exported for testing purposes. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 77 | func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor { |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 78 | return legacyLoadMessageDesc(t, "") |
| 79 | } |
| 80 | func legacyLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 81 | // Fast-path: check if a MessageDescriptor is cached for this concrete type. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 82 | if mi, ok := legacyMessageDescCache.Load(t); ok { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 83 | return mi.(pref.MessageDescriptor) |
| 84 | } |
| 85 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 86 | // Slow-path: initialize MessageDescriptor from the raw descriptor. |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 87 | mv := reflect.Zero(t).Interface() |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 88 | if _, ok := mv.(pref.ProtoMessage); ok { |
| 89 | panic(fmt.Sprintf("%v already implements proto.Message", t)) |
| 90 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 91 | mdV1, ok := mv.(messageV1) |
| 92 | if !ok { |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 93 | return aberrantLoadMessageDesc(t, name) |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 94 | } |
Damien Neil | 1605775 | 2019-11-11 16:30:04 -0800 | [diff] [blame] | 95 | |
| 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 Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 109 | |
Damien Neil | 4151cae | 2019-12-09 14:31:23 -0800 | [diff] [blame] | 110 | // 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 Tsai | d650018 | 2019-12-10 17:28:03 -0800 | [diff] [blame] | 117 | if f.Tag.Get("protobuf") != "" || f.Tag.Get("protobuf_oneof") != "" || strings.HasPrefix(f.Name, "XXX_") { |
Damien Neil | 4151cae | 2019-12-09 14:31:23 -0800 | [diff] [blame] | 118 | hasProtoField = true |
| 119 | break |
| 120 | } |
| 121 | } |
| 122 | if !hasProtoField { |
| 123 | return aberrantLoadMessageDesc(t, name) |
| 124 | } |
| 125 | } |
| 126 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 127 | md := legacyLoadFileDesc(b).Messages().Get(idxs[0]) |
| 128 | for _, i := range idxs[1:] { |
| 129 | md = md.Messages().Get(i) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 130 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 131 | if name != "" && md.FullName() != name { |
| 132 | panic(fmt.Sprintf("mismatching message name: got %v, want %v", md.FullName(), name)) |
| 133 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 134 | if md, ok := legacyMessageDescCache.LoadOrStore(t, md); ok { |
| 135 | return md.(protoreflect.MessageDescriptor) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 136 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 137 | return md |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 138 | } |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 139 | |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 140 | var ( |
| 141 | aberrantMessageDescLock sync.Mutex |
| 142 | aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor |
| 143 | ) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 144 | |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 145 | // aberrantLoadMessageDesc returns an MessageDescriptor derived from the Go type, |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 146 | // 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 Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 150 | func aberrantLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor { |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 151 | aberrantMessageDescLock.Lock() |
| 152 | defer aberrantMessageDescLock.Unlock() |
| 153 | if aberrantMessageDescCache == nil { |
| 154 | aberrantMessageDescCache = make(map[reflect.Type]protoreflect.MessageDescriptor) |
| 155 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 156 | return aberrantLoadMessageDescReentrant(t, name) |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 157 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 158 | func aberrantLoadMessageDescReentrant(t reflect.Type, name pref.FullName) pref.MessageDescriptor { |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 159 | // Fast-path: check if an MessageDescriptor is cached for this concrete type. |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 160 | if md, ok := aberrantMessageDescCache[t]; ok { |
| 161 | return md |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 164 | // Slow-path: construct a descriptor from the Go struct type (best-effort). |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 165 | // Cache the MessageDescriptor early on so that we can resolve internal |
| 166 | // cyclic references. |
| 167 | md := &filedesc.Message{L2: new(filedesc.MessageL2)} |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 168 | md.L0.FullName = aberrantDeriveMessageName(t, name) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 169 | md.L0.ParentFile = filedesc.SurrogateProto2 |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 170 | aberrantMessageDescCache[t] = md |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 171 | |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 172 | if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct { |
| 173 | return md |
| 174 | } |
| 175 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 176 | // 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 Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 251 | return md |
| 252 | } |
| 253 | |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 254 | func aberrantDeriveMessageName(t reflect.Type, name pref.FullName) pref.FullName { |
| 255 | if name.IsValid() { |
| 256 | return name |
| 257 | } |
Joe Tsai | e87cf53 | 2019-09-10 12:20:00 -0700 | [diff] [blame] | 258 | func() { |
| 259 | defer func() { recover() }() // swallow possible nil panics |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 260 | if m, ok := reflect.Zero(t).Interface().(interface{ XXX_MessageName() string }); ok { |
Joe Tsai | e87cf53 | 2019-09-10 12:20:00 -0700 | [diff] [blame] | 261 | name = pref.FullName(m.XXX_MessageName()) |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 262 | } |
Joe Tsai | e87cf53 | 2019-09-10 12:20:00 -0700 | [diff] [blame] | 263 | }() |
| 264 | if name.IsValid() { |
| 265 | return name |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 266 | } |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 267 | if t.Kind() == reflect.Ptr { |
| 268 | t = t.Elem() |
| 269 | } |
Joe Tsai | 55f1825 | 2020-01-11 00:25:01 -0800 | [diff] [blame] | 270 | return AberrantDeriveFullName(t) |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 273 | func 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 Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 295 | opts.Set(opts.Descriptor().Fields().ByName("weak"), protoreflect.ValueOfBool(true)) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 296 | } |
| 297 | if fd.L1.HasPacked { |
Joe Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 298 | opts.Set(opts.Descriptor().Fields().ByName("packed"), protoreflect.ValueOfBool(fd.L1.IsPacked)) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 299 | } |
| 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 Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 317 | case messageV1: |
| 318 | fd.L1.Message = LegacyLoadMessageDesc(t) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 319 | 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 Tsai | 97a8739 | 2019-07-07 01:49:59 -0700 | [diff] [blame] | 324 | md2.L0.FullName = md.FullName().Append(pref.Name(strs.MapEntryName(string(fd.Name())))) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 325 | md2.L0.ParentFile = md.L0.ParentFile |
| 326 | md2.L0.Parent = md |
| 327 | md2.L0.Index = n |
| 328 | |
Damien Neil | e918732 | 2019-11-07 15:30:44 -0800 | [diff] [blame] | 329 | md2.L1.IsMapEntry = true |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 330 | md2.L2.Options = func() pref.ProtoMessage { |
| 331 | opts := descopts.Message.ProtoReflect().New() |
Joe Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 332 | opts.Set(opts.Descriptor().Fields().ByName("map_entry"), protoreflect.ValueOfBool(true)) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 333 | 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 Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 342 | fd.L1.Message = aberrantLoadMessageDescReentrant(t, "") |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | type placeholderEnumValues struct { |
| 348 | protoreflect.EnumValueDescriptors |
| 349 | } |
| 350 | |
| 351 | func (placeholderEnumValues) ByNumber(n pref.EnumNumber) pref.EnumValueDescriptor { |
| 352 | return filedesc.PlaceholderEnumValue(pref.FullName(fmt.Sprintf("UNKNOWN_%d", n))) |
| 353 | } |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 354 | |
| 355 | // legacyMarshaler is the proto.Marshaler interface superseded by protoiface.Methoder. |
| 356 | type legacyMarshaler interface { |
| 357 | Marshal() ([]byte, error) |
| 358 | } |
| 359 | |
| 360 | // legacyUnmarshaler is the proto.Unmarshaler interface superseded by protoiface.Methoder. |
| 361 | type legacyUnmarshaler interface { |
| 362 | Unmarshal([]byte) error |
| 363 | } |
| 364 | |
| 365 | var legacyProtoMethods = &piface.Methods{ |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame] | 366 | Marshal: legacyMarshal, |
| 367 | Unmarshal: legacyUnmarshal, |
Damien Neil | c7f2bee | 2019-11-06 15:29:05 -0800 | [diff] [blame] | 368 | |
| 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 Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Damien Neil | d30e561 | 2020-01-22 10:28:16 -0800 | [diff] [blame^] | 376 | func legacyMarshal(m protoreflect.Message, in piface.MarshalInput, opts piface.MarshalOptions) (piface.MarshalOutput, error) { |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 377 | v := m.(unwrapper).protoUnwrap() |
| 378 | marshaler, ok := v.(legacyMarshaler) |
| 379 | if !ok { |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame] | 380 | return piface.MarshalOutput{}, errors.New("%T does not implement Marshal", v) |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 381 | } |
| 382 | out, err := marshaler.Marshal() |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame] | 383 | if in.Buf != nil { |
| 384 | out = append(in.Buf, out...) |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 385 | } |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame] | 386 | return piface.MarshalOutput{ |
| 387 | Buf: out, |
| 388 | }, err |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Damien Neil | d30e561 | 2020-01-22 10:28:16 -0800 | [diff] [blame^] | 391 | func legacyUnmarshal(m protoreflect.Message, in piface.UnmarshalInput, opts piface.UnmarshalOptions) (piface.UnmarshalOutput, error) { |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 392 | v := m.(unwrapper).protoUnwrap() |
| 393 | unmarshaler, ok := v.(legacyUnmarshaler) |
| 394 | if !ok { |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame] | 395 | return piface.UnmarshalOutput{}, errors.New("%T does not implement Marshal", v) |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 396 | } |
Damien Neil | 61781dd | 2020-01-21 13:29:51 -0800 | [diff] [blame] | 397 | return piface.UnmarshalOutput{}, unmarshaler.Unmarshal(in.Buf) |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // aberrantMessageType implements MessageType for all types other than pointer-to-struct. |
| 401 | type aberrantMessageType struct { |
| 402 | t reflect.Type |
| 403 | } |
| 404 | |
| 405 | func (mt aberrantMessageType) New() pref.Message { |
| 406 | return aberrantMessage{reflect.Zero(mt.t)} |
| 407 | } |
| 408 | func (mt aberrantMessageType) Zero() pref.Message { |
| 409 | return aberrantMessage{reflect.Zero(mt.t)} |
| 410 | } |
| 411 | func (mt aberrantMessageType) GoType() reflect.Type { |
| 412 | return mt.t |
| 413 | } |
| 414 | func (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. |
| 423 | type aberrantMessage struct { |
| 424 | v reflect.Value |
| 425 | } |
| 426 | |
| 427 | func (m aberrantMessage) ProtoReflect() pref.Message { |
| 428 | return m |
| 429 | } |
| 430 | |
| 431 | func (m aberrantMessage) Descriptor() pref.MessageDescriptor { |
| 432 | return LegacyLoadMessageDesc(m.v.Type()) |
| 433 | } |
| 434 | func (m aberrantMessage) Type() pref.MessageType { |
| 435 | return aberrantMessageType{m.v.Type()} |
| 436 | } |
| 437 | func (m aberrantMessage) New() pref.Message { |
| 438 | return aberrantMessage{reflect.Zero(m.v.Type())} |
| 439 | } |
| 440 | func (m aberrantMessage) Interface() pref.ProtoMessage { |
| 441 | return m |
| 442 | } |
| 443 | func (m aberrantMessage) Range(f func(pref.FieldDescriptor, pref.Value) bool) { |
| 444 | } |
| 445 | func (m aberrantMessage) Has(pref.FieldDescriptor) bool { |
| 446 | panic("invalid field descriptor") |
| 447 | } |
| 448 | func (m aberrantMessage) Clear(pref.FieldDescriptor) { |
| 449 | panic("invalid field descriptor") |
| 450 | } |
| 451 | func (m aberrantMessage) Get(pref.FieldDescriptor) pref.Value { |
| 452 | panic("invalid field descriptor") |
| 453 | } |
| 454 | func (m aberrantMessage) Set(pref.FieldDescriptor, pref.Value) { |
| 455 | panic("invalid field descriptor") |
| 456 | } |
| 457 | func (m aberrantMessage) Mutable(pref.FieldDescriptor) pref.Value { |
| 458 | panic("invalid field descriptor") |
| 459 | } |
| 460 | func (m aberrantMessage) NewField(pref.FieldDescriptor) pref.Value { |
| 461 | panic("invalid field descriptor") |
| 462 | } |
| 463 | func (m aberrantMessage) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor { |
| 464 | panic("invalid oneof descriptor") |
| 465 | } |
| 466 | func (m aberrantMessage) GetUnknown() pref.RawFields { |
| 467 | return nil |
| 468 | } |
| 469 | func (m aberrantMessage) SetUnknown(pref.RawFields) { |
| 470 | // SetUnknown discards its input on messages which don't support unknown field storage. |
| 471 | } |
Damien Neil | 82886da | 2019-11-26 13:27:24 -0800 | [diff] [blame] | 472 | func (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 Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 478 | func (m aberrantMessage) ProtoMethods() *piface.Methods { |
| 479 | return legacyProtoMethods |
| 480 | } |
| 481 | func (m aberrantMessage) protoUnwrap() interface{} { |
| 482 | return m.v.Interface() |
| 483 | } |