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