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 { |
| 53 | mi.methods.MarshalAppend = legacyMarshalAppend |
| 54 | mi.methods.Size = legacySize |
Damien Neil | c7f2bee | 2019-11-06 15:29:05 -0800 | [diff] [blame] | 55 | |
| 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 Neil | 37ef691 | 2019-09-25 16:51:15 -0700 | [diff] [blame] | 61 | } |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 62 | if _, ok := v.(legacyUnmarshaler); ok { |
| 63 | mi.methods.Unmarshal = legacyUnmarshal |
Damien Neil | 37ef691 | 2019-09-25 16:51:15 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame] | 66 | if mi, ok := legacyMessageTypeCache.LoadOrStore(t, mi); ok { |
| 67 | return mi.(*MessageInfo) |
Joe Tsai | b936504 | 2019-03-19 14:14:29 -0700 | [diff] [blame] | 68 | } |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame] | 69 | return mi |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 72 | var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 73 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 74 | // LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type, |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 75 | // 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] | 76 | // |
| 77 | // This is exported for testing purposes. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 78 | func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor { |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 79 | return legacyLoadMessageDesc(t, "") |
| 80 | } |
| 81 | func legacyLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 82 | // Fast-path: check if a MessageDescriptor is cached for this concrete type. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 83 | if mi, ok := legacyMessageDescCache.Load(t); ok { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 84 | return mi.(pref.MessageDescriptor) |
| 85 | } |
| 86 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 87 | // Slow-path: initialize MessageDescriptor from the raw descriptor. |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 88 | mv := reflect.Zero(t).Interface() |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 89 | if _, ok := mv.(pref.ProtoMessage); ok { |
| 90 | panic(fmt.Sprintf("%v already implements proto.Message", t)) |
| 91 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 92 | mdV1, ok := mv.(messageV1) |
| 93 | if !ok { |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 94 | return aberrantLoadMessageDesc(t, name) |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 95 | } |
Damien Neil | 1605775 | 2019-11-11 16:30:04 -0800 | [diff] [blame] | 96 | |
| 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 Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 110 | |
Damien Neil | 4151cae | 2019-12-09 14:31:23 -0800 | [diff] [blame] | 111 | // 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) |
Joe Tsai | d650018 | 2019-12-10 17:28:03 -0800 | [diff] [blame^] | 118 | 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] | 119 | hasProtoField = true |
| 120 | break |
| 121 | } |
| 122 | } |
| 123 | if !hasProtoField { |
| 124 | return aberrantLoadMessageDesc(t, name) |
| 125 | } |
| 126 | } |
| 127 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 128 | md := legacyLoadFileDesc(b).Messages().Get(idxs[0]) |
| 129 | for _, i := range idxs[1:] { |
| 130 | md = md.Messages().Get(i) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 131 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 132 | if name != "" && md.FullName() != name { |
| 133 | panic(fmt.Sprintf("mismatching message name: got %v, want %v", md.FullName(), name)) |
| 134 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 135 | if md, ok := legacyMessageDescCache.LoadOrStore(t, md); ok { |
| 136 | return md.(protoreflect.MessageDescriptor) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 137 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 138 | return md |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 139 | } |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 140 | |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 141 | var ( |
| 142 | aberrantMessageDescLock sync.Mutex |
| 143 | aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor |
| 144 | ) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 145 | |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 146 | // aberrantLoadMessageDesc returns an MessageDescriptor derived from the Go type, |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 147 | // 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 Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 151 | func aberrantLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor { |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 152 | aberrantMessageDescLock.Lock() |
| 153 | defer aberrantMessageDescLock.Unlock() |
| 154 | if aberrantMessageDescCache == nil { |
| 155 | aberrantMessageDescCache = make(map[reflect.Type]protoreflect.MessageDescriptor) |
| 156 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 157 | return aberrantLoadMessageDescReentrant(t, name) |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 158 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 159 | func aberrantLoadMessageDescReentrant(t reflect.Type, name pref.FullName) pref.MessageDescriptor { |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 160 | // Fast-path: check if an MessageDescriptor is cached for this concrete type. |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 161 | if md, ok := aberrantMessageDescCache[t]; ok { |
| 162 | return md |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 165 | // Slow-path: construct a descriptor from the Go struct type (best-effort). |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 166 | // Cache the MessageDescriptor early on so that we can resolve internal |
| 167 | // cyclic references. |
| 168 | md := &filedesc.Message{L2: new(filedesc.MessageL2)} |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 169 | md.L0.FullName = aberrantDeriveMessageName(t, name) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 170 | md.L0.ParentFile = filedesc.SurrogateProto2 |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 171 | aberrantMessageDescCache[t] = md |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 172 | |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 173 | if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct { |
| 174 | return md |
| 175 | } |
| 176 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 177 | // 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 Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 252 | return md |
| 253 | } |
| 254 | |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 255 | func aberrantDeriveMessageName(t reflect.Type, name pref.FullName) pref.FullName { |
| 256 | if name.IsValid() { |
| 257 | return name |
| 258 | } |
Joe Tsai | e87cf53 | 2019-09-10 12:20:00 -0700 | [diff] [blame] | 259 | func() { |
| 260 | defer func() { recover() }() // swallow possible nil panics |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 261 | if m, ok := reflect.Zero(t).Interface().(interface{ XXX_MessageName() string }); ok { |
Joe Tsai | e87cf53 | 2019-09-10 12:20:00 -0700 | [diff] [blame] | 262 | name = pref.FullName(m.XXX_MessageName()) |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 263 | } |
Joe Tsai | e87cf53 | 2019-09-10 12:20:00 -0700 | [diff] [blame] | 264 | }() |
| 265 | if name.IsValid() { |
| 266 | return name |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 267 | } |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 268 | if t.Kind() == reflect.Ptr { |
| 269 | t = t.Elem() |
| 270 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 271 | return aberrantDeriveFullName(t) |
| 272 | } |
| 273 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 274 | func 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 Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 296 | opts.Set(opts.Descriptor().Fields().ByName("weak"), protoreflect.ValueOfBool(true)) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 297 | } |
| 298 | if fd.L1.HasPacked { |
Joe Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 299 | opts.Set(opts.Descriptor().Fields().ByName("packed"), protoreflect.ValueOfBool(fd.L1.IsPacked)) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 300 | } |
| 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 Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 318 | case messageV1: |
| 319 | fd.L1.Message = LegacyLoadMessageDesc(t) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 320 | 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 Tsai | 97a8739 | 2019-07-07 01:49:59 -0700 | [diff] [blame] | 325 | 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] | 326 | md2.L0.ParentFile = md.L0.ParentFile |
| 327 | md2.L0.Parent = md |
| 328 | md2.L0.Index = n |
| 329 | |
Damien Neil | e918732 | 2019-11-07 15:30:44 -0800 | [diff] [blame] | 330 | md2.L1.IsMapEntry = true |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 331 | md2.L2.Options = func() pref.ProtoMessage { |
| 332 | opts := descopts.Message.ProtoReflect().New() |
Joe Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 333 | opts.Set(opts.Descriptor().Fields().ByName("map_entry"), protoreflect.ValueOfBool(true)) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 334 | 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 Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 343 | fd.L1.Message = aberrantLoadMessageDescReentrant(t, "") |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | type placeholderEnumValues struct { |
| 349 | protoreflect.EnumValueDescriptors |
| 350 | } |
| 351 | |
| 352 | func (placeholderEnumValues) ByNumber(n pref.EnumNumber) pref.EnumValueDescriptor { |
| 353 | return filedesc.PlaceholderEnumValue(pref.FullName(fmt.Sprintf("UNKNOWN_%d", n))) |
| 354 | } |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 355 | |
| 356 | // legacyMarshaler is the proto.Marshaler interface superseded by protoiface.Methoder. |
| 357 | type legacyMarshaler interface { |
| 358 | Marshal() ([]byte, error) |
| 359 | } |
| 360 | |
| 361 | // legacyUnmarshaler is the proto.Unmarshaler interface superseded by protoiface.Methoder. |
| 362 | type legacyUnmarshaler interface { |
| 363 | Unmarshal([]byte) error |
| 364 | } |
| 365 | |
| 366 | var legacyProtoMethods = &piface.Methods{ |
| 367 | Size: legacySize, |
| 368 | MarshalAppend: legacyMarshalAppend, |
| 369 | Unmarshal: legacyUnmarshal, |
Damien Neil | c7f2bee | 2019-11-06 15:29:05 -0800 | [diff] [blame] | 370 | |
| 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 Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | func legacySize(m protoreflect.Message, opts piface.MarshalOptions) int { |
| 379 | b, _ := legacyMarshalAppend(nil, m, opts) |
| 380 | return len(b) |
| 381 | } |
| 382 | |
| 383 | func 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 | |
| 396 | func 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. |
| 406 | type aberrantMessageType struct { |
| 407 | t reflect.Type |
| 408 | } |
| 409 | |
| 410 | func (mt aberrantMessageType) New() pref.Message { |
| 411 | return aberrantMessage{reflect.Zero(mt.t)} |
| 412 | } |
| 413 | func (mt aberrantMessageType) Zero() pref.Message { |
| 414 | return aberrantMessage{reflect.Zero(mt.t)} |
| 415 | } |
| 416 | func (mt aberrantMessageType) GoType() reflect.Type { |
| 417 | return mt.t |
| 418 | } |
| 419 | func (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. |
| 428 | type aberrantMessage struct { |
| 429 | v reflect.Value |
| 430 | } |
| 431 | |
| 432 | func (m aberrantMessage) ProtoReflect() pref.Message { |
| 433 | return m |
| 434 | } |
| 435 | |
| 436 | func (m aberrantMessage) Descriptor() pref.MessageDescriptor { |
| 437 | return LegacyLoadMessageDesc(m.v.Type()) |
| 438 | } |
| 439 | func (m aberrantMessage) Type() pref.MessageType { |
| 440 | return aberrantMessageType{m.v.Type()} |
| 441 | } |
| 442 | func (m aberrantMessage) New() pref.Message { |
| 443 | return aberrantMessage{reflect.Zero(m.v.Type())} |
| 444 | } |
| 445 | func (m aberrantMessage) Interface() pref.ProtoMessage { |
| 446 | return m |
| 447 | } |
| 448 | func (m aberrantMessage) Range(f func(pref.FieldDescriptor, pref.Value) bool) { |
| 449 | } |
| 450 | func (m aberrantMessage) Has(pref.FieldDescriptor) bool { |
| 451 | panic("invalid field descriptor") |
| 452 | } |
| 453 | func (m aberrantMessage) Clear(pref.FieldDescriptor) { |
| 454 | panic("invalid field descriptor") |
| 455 | } |
| 456 | func (m aberrantMessage) Get(pref.FieldDescriptor) pref.Value { |
| 457 | panic("invalid field descriptor") |
| 458 | } |
| 459 | func (m aberrantMessage) Set(pref.FieldDescriptor, pref.Value) { |
| 460 | panic("invalid field descriptor") |
| 461 | } |
| 462 | func (m aberrantMessage) Mutable(pref.FieldDescriptor) pref.Value { |
| 463 | panic("invalid field descriptor") |
| 464 | } |
| 465 | func (m aberrantMessage) NewField(pref.FieldDescriptor) pref.Value { |
| 466 | panic("invalid field descriptor") |
| 467 | } |
| 468 | func (m aberrantMessage) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor { |
| 469 | panic("invalid oneof descriptor") |
| 470 | } |
| 471 | func (m aberrantMessage) GetUnknown() pref.RawFields { |
| 472 | return nil |
| 473 | } |
| 474 | func (m aberrantMessage) SetUnknown(pref.RawFields) { |
| 475 | // SetUnknown discards its input on messages which don't support unknown field storage. |
| 476 | } |
Damien Neil | 82886da | 2019-11-26 13:27:24 -0800 | [diff] [blame] | 477 | func (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 Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 483 | func (m aberrantMessage) ProtoMethods() *piface.Methods { |
| 484 | return legacyProtoMethods |
| 485 | } |
| 486 | func (m aberrantMessage) protoUnwrap() interface{} { |
| 487 | return m.v.Interface() |
| 488 | } |