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 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 111 | md := legacyLoadFileDesc(b).Messages().Get(idxs[0]) |
| 112 | for _, i := range idxs[1:] { |
| 113 | md = md.Messages().Get(i) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 114 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 115 | if name != "" && md.FullName() != name { |
| 116 | panic(fmt.Sprintf("mismatching message name: got %v, want %v", md.FullName(), name)) |
| 117 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 118 | if md, ok := legacyMessageDescCache.LoadOrStore(t, md); ok { |
| 119 | return md.(protoreflect.MessageDescriptor) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 120 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 121 | return md |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 122 | } |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 123 | |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 124 | var ( |
| 125 | aberrantMessageDescLock sync.Mutex |
| 126 | aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor |
| 127 | ) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 128 | |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 129 | // aberrantLoadMessageDesc returns an MessageDescriptor derived from the Go type, |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 130 | // which must not implement protoreflect.ProtoMessage or messageV1. |
| 131 | // |
| 132 | // This is a best-effort derivation of the message descriptor using the protobuf |
| 133 | // tags on the struct fields. |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 134 | func aberrantLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor { |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 135 | aberrantMessageDescLock.Lock() |
| 136 | defer aberrantMessageDescLock.Unlock() |
| 137 | if aberrantMessageDescCache == nil { |
| 138 | aberrantMessageDescCache = make(map[reflect.Type]protoreflect.MessageDescriptor) |
| 139 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 140 | return aberrantLoadMessageDescReentrant(t, name) |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 141 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 142 | func aberrantLoadMessageDescReentrant(t reflect.Type, name pref.FullName) pref.MessageDescriptor { |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 143 | // Fast-path: check if an MessageDescriptor is cached for this concrete type. |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 144 | if md, ok := aberrantMessageDescCache[t]; ok { |
| 145 | return md |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 148 | // Slow-path: construct a descriptor from the Go struct type (best-effort). |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 149 | // Cache the MessageDescriptor early on so that we can resolve internal |
| 150 | // cyclic references. |
| 151 | md := &filedesc.Message{L2: new(filedesc.MessageL2)} |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 152 | md.L0.FullName = aberrantDeriveMessageName(t, name) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 153 | md.L0.ParentFile = filedesc.SurrogateProto2 |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 154 | aberrantMessageDescCache[t] = md |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 155 | |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 156 | if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct { |
| 157 | return md |
| 158 | } |
| 159 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 160 | // Try to determine if the message is using proto3 by checking scalars. |
| 161 | for i := 0; i < t.Elem().NumField(); i++ { |
| 162 | f := t.Elem().Field(i) |
| 163 | if tag := f.Tag.Get("protobuf"); tag != "" { |
| 164 | switch f.Type.Kind() { |
| 165 | case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String: |
| 166 | md.L0.ParentFile = filedesc.SurrogateProto3 |
| 167 | } |
| 168 | for _, s := range strings.Split(tag, ",") { |
| 169 | if s == "proto3" { |
| 170 | md.L0.ParentFile = filedesc.SurrogateProto3 |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Obtain a list of oneof wrapper types. |
| 177 | var oneofWrappers []reflect.Type |
| 178 | if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok { |
| 179 | vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3] |
| 180 | for _, v := range vs.Interface().([]interface{}) { |
| 181 | oneofWrappers = append(oneofWrappers, reflect.TypeOf(v)) |
| 182 | } |
| 183 | } |
| 184 | if fn, ok := t.MethodByName("XXX_OneofWrappers"); ok { |
| 185 | vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0] |
| 186 | for _, v := range vs.Interface().([]interface{}) { |
| 187 | oneofWrappers = append(oneofWrappers, reflect.TypeOf(v)) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Obtain a list of the extension ranges. |
| 192 | if fn, ok := t.MethodByName("ExtensionRangeArray"); ok { |
| 193 | vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0] |
| 194 | for i := 0; i < vs.Len(); i++ { |
| 195 | v := vs.Index(i) |
| 196 | md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, [2]pref.FieldNumber{ |
| 197 | pref.FieldNumber(v.FieldByName("Start").Int()), |
| 198 | pref.FieldNumber(v.FieldByName("End").Int() + 1), |
| 199 | }) |
| 200 | md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, nil) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Derive the message fields by inspecting the struct fields. |
| 205 | for i := 0; i < t.Elem().NumField(); i++ { |
| 206 | f := t.Elem().Field(i) |
| 207 | if tag := f.Tag.Get("protobuf"); tag != "" { |
| 208 | tagKey := f.Tag.Get("protobuf_key") |
| 209 | tagVal := f.Tag.Get("protobuf_val") |
| 210 | aberrantAppendField(md, f.Type, tag, tagKey, tagVal) |
| 211 | } |
| 212 | if tag := f.Tag.Get("protobuf_oneof"); tag != "" { |
| 213 | n := len(md.L2.Oneofs.List) |
| 214 | md.L2.Oneofs.List = append(md.L2.Oneofs.List, filedesc.Oneof{}) |
| 215 | od := &md.L2.Oneofs.List[n] |
| 216 | od.L0.FullName = md.FullName().Append(pref.Name(tag)) |
| 217 | od.L0.ParentFile = md.L0.ParentFile |
| 218 | od.L0.Parent = md |
| 219 | od.L0.Index = n |
| 220 | |
| 221 | for _, t := range oneofWrappers { |
| 222 | if t.Implements(f.Type) { |
| 223 | f := t.Elem().Field(0) |
| 224 | if tag := f.Tag.Get("protobuf"); tag != "" { |
| 225 | aberrantAppendField(md, f.Type, tag, "", "") |
| 226 | fd := &md.L2.Fields.List[len(md.L2.Fields.List)-1] |
| 227 | fd.L1.ContainingOneof = od |
| 228 | od.L1.Fields.List = append(od.L1.Fields.List, fd) |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 235 | return md |
| 236 | } |
| 237 | |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 238 | func aberrantDeriveMessageName(t reflect.Type, name pref.FullName) pref.FullName { |
| 239 | if name.IsValid() { |
| 240 | return name |
| 241 | } |
Joe Tsai | e87cf53 | 2019-09-10 12:20:00 -0700 | [diff] [blame] | 242 | func() { |
| 243 | defer func() { recover() }() // swallow possible nil panics |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 244 | if m, ok := reflect.Zero(t).Interface().(interface{ XXX_MessageName() string }); ok { |
Joe Tsai | e87cf53 | 2019-09-10 12:20:00 -0700 | [diff] [blame] | 245 | name = pref.FullName(m.XXX_MessageName()) |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 246 | } |
Joe Tsai | e87cf53 | 2019-09-10 12:20:00 -0700 | [diff] [blame] | 247 | }() |
| 248 | if name.IsValid() { |
| 249 | return name |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 250 | } |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 251 | if t.Kind() == reflect.Ptr { |
| 252 | t = t.Elem() |
| 253 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 254 | return aberrantDeriveFullName(t) |
| 255 | } |
| 256 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 257 | func aberrantAppendField(md *filedesc.Message, goType reflect.Type, tag, tagKey, tagVal string) { |
| 258 | t := goType |
| 259 | isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct |
| 260 | isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 |
| 261 | if isOptional || isRepeated { |
| 262 | t = t.Elem() |
| 263 | } |
| 264 | fd := ptag.Unmarshal(tag, t, placeholderEnumValues{}).(*filedesc.Field) |
| 265 | |
| 266 | // Append field descriptor to the message. |
| 267 | n := len(md.L2.Fields.List) |
| 268 | md.L2.Fields.List = append(md.L2.Fields.List, *fd) |
| 269 | fd = &md.L2.Fields.List[n] |
| 270 | fd.L0.FullName = md.FullName().Append(fd.Name()) |
| 271 | fd.L0.ParentFile = md.L0.ParentFile |
| 272 | fd.L0.Parent = md |
| 273 | fd.L0.Index = n |
| 274 | |
| 275 | if fd.L1.IsWeak || fd.L1.HasPacked { |
| 276 | fd.L1.Options = func() pref.ProtoMessage { |
| 277 | opts := descopts.Field.ProtoReflect().New() |
| 278 | if fd.L1.IsWeak { |
Joe Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 279 | opts.Set(opts.Descriptor().Fields().ByName("weak"), protoreflect.ValueOfBool(true)) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 280 | } |
| 281 | if fd.L1.HasPacked { |
Joe Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 282 | opts.Set(opts.Descriptor().Fields().ByName("packed"), protoreflect.ValueOfBool(fd.L1.IsPacked)) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 283 | } |
| 284 | return opts.Interface() |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // Populate Enum and Message. |
| 289 | if fd.Enum() == nil && fd.Kind() == pref.EnumKind { |
| 290 | switch v := reflect.Zero(t).Interface().(type) { |
| 291 | case pref.Enum: |
| 292 | fd.L1.Enum = v.Descriptor() |
| 293 | default: |
| 294 | fd.L1.Enum = LegacyLoadEnumDesc(t) |
| 295 | } |
| 296 | } |
| 297 | if fd.Message() == nil && (fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind) { |
| 298 | switch v := reflect.Zero(t).Interface().(type) { |
| 299 | case pref.ProtoMessage: |
| 300 | fd.L1.Message = v.ProtoReflect().Descriptor() |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 301 | case messageV1: |
| 302 | fd.L1.Message = LegacyLoadMessageDesc(t) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 303 | default: |
| 304 | if t.Kind() == reflect.Map { |
| 305 | n := len(md.L1.Messages.List) |
| 306 | md.L1.Messages.List = append(md.L1.Messages.List, filedesc.Message{L2: new(filedesc.MessageL2)}) |
| 307 | md2 := &md.L1.Messages.List[n] |
Joe Tsai | 97a8739 | 2019-07-07 01:49:59 -0700 | [diff] [blame] | 308 | 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] | 309 | md2.L0.ParentFile = md.L0.ParentFile |
| 310 | md2.L0.Parent = md |
| 311 | md2.L0.Index = n |
| 312 | |
Damien Neil | e918732 | 2019-11-07 15:30:44 -0800 | [diff] [blame] | 313 | md2.L1.IsMapEntry = true |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 314 | md2.L2.Options = func() pref.ProtoMessage { |
| 315 | opts := descopts.Message.ProtoReflect().New() |
Joe Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 316 | opts.Set(opts.Descriptor().Fields().ByName("map_entry"), protoreflect.ValueOfBool(true)) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 317 | return opts.Interface() |
| 318 | } |
| 319 | |
| 320 | aberrantAppendField(md2, t.Key(), tagKey, "", "") |
| 321 | aberrantAppendField(md2, t.Elem(), tagVal, "", "") |
| 322 | |
| 323 | fd.L1.Message = md2 |
| 324 | break |
| 325 | } |
Joe Tsai | ea5ada1 | 2019-09-04 22:41:40 -0700 | [diff] [blame] | 326 | fd.L1.Message = aberrantLoadMessageDescReentrant(t, "") |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | type placeholderEnumValues struct { |
| 332 | protoreflect.EnumValueDescriptors |
| 333 | } |
| 334 | |
| 335 | func (placeholderEnumValues) ByNumber(n pref.EnumNumber) pref.EnumValueDescriptor { |
| 336 | return filedesc.PlaceholderEnumValue(pref.FullName(fmt.Sprintf("UNKNOWN_%d", n))) |
| 337 | } |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 338 | |
| 339 | // legacyMarshaler is the proto.Marshaler interface superseded by protoiface.Methoder. |
| 340 | type legacyMarshaler interface { |
| 341 | Marshal() ([]byte, error) |
| 342 | } |
| 343 | |
| 344 | // legacyUnmarshaler is the proto.Unmarshaler interface superseded by protoiface.Methoder. |
| 345 | type legacyUnmarshaler interface { |
| 346 | Unmarshal([]byte) error |
| 347 | } |
| 348 | |
| 349 | var legacyProtoMethods = &piface.Methods{ |
| 350 | Size: legacySize, |
| 351 | MarshalAppend: legacyMarshalAppend, |
| 352 | Unmarshal: legacyUnmarshal, |
Damien Neil | c7f2bee | 2019-11-06 15:29:05 -0800 | [diff] [blame] | 353 | |
| 354 | // We have no way to tell whether the type's Marshal method |
| 355 | // supports deterministic serialization or not, but this |
| 356 | // preserves the v1 implementation's behavior of always |
| 357 | // calling Marshal methods when present. |
| 358 | Flags: piface.SupportMarshalDeterministic, |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | func legacySize(m protoreflect.Message, opts piface.MarshalOptions) int { |
| 362 | b, _ := legacyMarshalAppend(nil, m, opts) |
| 363 | return len(b) |
| 364 | } |
| 365 | |
| 366 | func legacyMarshalAppend(b []byte, m protoreflect.Message, opts piface.MarshalOptions) ([]byte, error) { |
| 367 | v := m.(unwrapper).protoUnwrap() |
| 368 | marshaler, ok := v.(legacyMarshaler) |
| 369 | if !ok { |
| 370 | return nil, errors.New("%T does not implement Marshal", v) |
| 371 | } |
| 372 | out, err := marshaler.Marshal() |
| 373 | if b != nil { |
| 374 | out = append(b, out...) |
| 375 | } |
| 376 | return out, err |
| 377 | } |
| 378 | |
| 379 | func legacyUnmarshal(b []byte, m protoreflect.Message, opts piface.UnmarshalOptions) error { |
| 380 | v := m.(unwrapper).protoUnwrap() |
| 381 | unmarshaler, ok := v.(legacyUnmarshaler) |
| 382 | if !ok { |
| 383 | return errors.New("%T does not implement Marshal", v) |
| 384 | } |
| 385 | return unmarshaler.Unmarshal(b) |
| 386 | } |
| 387 | |
| 388 | // aberrantMessageType implements MessageType for all types other than pointer-to-struct. |
| 389 | type aberrantMessageType struct { |
| 390 | t reflect.Type |
| 391 | } |
| 392 | |
| 393 | func (mt aberrantMessageType) New() pref.Message { |
| 394 | return aberrantMessage{reflect.Zero(mt.t)} |
| 395 | } |
| 396 | func (mt aberrantMessageType) Zero() pref.Message { |
| 397 | return aberrantMessage{reflect.Zero(mt.t)} |
| 398 | } |
| 399 | func (mt aberrantMessageType) GoType() reflect.Type { |
| 400 | return mt.t |
| 401 | } |
| 402 | func (mt aberrantMessageType) Descriptor() pref.MessageDescriptor { |
| 403 | return LegacyLoadMessageDesc(mt.t) |
| 404 | } |
| 405 | |
| 406 | // aberrantMessage implements Message for all types other than pointer-to-struct. |
| 407 | // |
| 408 | // When the underlying type implements legacyMarshaler or legacyUnmarshaler, |
| 409 | // the aberrant Message can be marshaled or unmarshaled. Otherwise, there is |
| 410 | // not much that can be done with values of this type. |
| 411 | type aberrantMessage struct { |
| 412 | v reflect.Value |
| 413 | } |
| 414 | |
| 415 | func (m aberrantMessage) ProtoReflect() pref.Message { |
| 416 | return m |
| 417 | } |
| 418 | |
| 419 | func (m aberrantMessage) Descriptor() pref.MessageDescriptor { |
| 420 | return LegacyLoadMessageDesc(m.v.Type()) |
| 421 | } |
| 422 | func (m aberrantMessage) Type() pref.MessageType { |
| 423 | return aberrantMessageType{m.v.Type()} |
| 424 | } |
| 425 | func (m aberrantMessage) New() pref.Message { |
| 426 | return aberrantMessage{reflect.Zero(m.v.Type())} |
| 427 | } |
| 428 | func (m aberrantMessage) Interface() pref.ProtoMessage { |
| 429 | return m |
| 430 | } |
| 431 | func (m aberrantMessage) Range(f func(pref.FieldDescriptor, pref.Value) bool) { |
| 432 | } |
| 433 | func (m aberrantMessage) Has(pref.FieldDescriptor) bool { |
| 434 | panic("invalid field descriptor") |
| 435 | } |
| 436 | func (m aberrantMessage) Clear(pref.FieldDescriptor) { |
| 437 | panic("invalid field descriptor") |
| 438 | } |
| 439 | func (m aberrantMessage) Get(pref.FieldDescriptor) pref.Value { |
| 440 | panic("invalid field descriptor") |
| 441 | } |
| 442 | func (m aberrantMessage) Set(pref.FieldDescriptor, pref.Value) { |
| 443 | panic("invalid field descriptor") |
| 444 | } |
| 445 | func (m aberrantMessage) Mutable(pref.FieldDescriptor) pref.Value { |
| 446 | panic("invalid field descriptor") |
| 447 | } |
| 448 | func (m aberrantMessage) NewField(pref.FieldDescriptor) pref.Value { |
| 449 | panic("invalid field descriptor") |
| 450 | } |
| 451 | func (m aberrantMessage) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor { |
| 452 | panic("invalid oneof descriptor") |
| 453 | } |
| 454 | func (m aberrantMessage) GetUnknown() pref.RawFields { |
| 455 | return nil |
| 456 | } |
| 457 | func (m aberrantMessage) SetUnknown(pref.RawFields) { |
| 458 | // SetUnknown discards its input on messages which don't support unknown field storage. |
| 459 | } |
Damien Neil | 82886da | 2019-11-26 13:27:24 -0800 | [diff] [blame^] | 460 | func (m aberrantMessage) IsValid() bool { |
| 461 | // An invalid message is a read-only, empty message. Since we don't know anything |
| 462 | // about the alleged contents of this message, we can't say with confidence that |
| 463 | // it is invalid in this sense. Therefore, report it as valid. |
| 464 | return true |
| 465 | } |
Damien Neil | 47d5893 | 2019-09-30 15:34:27 -0700 | [diff] [blame] | 466 | func (m aberrantMessage) ProtoMethods() *piface.Methods { |
| 467 | return legacyProtoMethods |
| 468 | } |
| 469 | func (m aberrantMessage) protoUnwrap() interface{} { |
| 470 | return m.v.Interface() |
| 471 | } |