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 | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 12 | "unicode" |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 13 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 14 | "google.golang.org/protobuf/internal/descopts" |
| 15 | ptag "google.golang.org/protobuf/internal/encoding/tag" |
| 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" |
Joe Tsai | b2f66be | 2019-05-22 00:42:45 -0400 | [diff] [blame] | 20 | "google.golang.org/protobuf/reflect/prototype" |
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 { |
| 26 | mt := legacyLoadMessageInfo(v.Type()) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 27 | return mt.MessageOf(v.Interface()).Interface() |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 28 | } |
| 29 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 30 | var legacyMessageTypeCache sync.Map // map[reflect.Type]*MessageInfo |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 31 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 32 | // legacyLoadMessageInfo dynamically loads a *MessageInfo for t, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 33 | // where t must be a *struct kind and not implement the v2 API already. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 34 | func legacyLoadMessageInfo(t reflect.Type) *MessageInfo { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 35 | // Fast-path: check if a MessageInfo is cached for this concrete type. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 36 | if mt, ok := legacyMessageTypeCache.Load(t); ok { |
| 37 | return mt.(*MessageInfo) |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 40 | // Slow-path: derive message descriptor and initialize MessageInfo. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 41 | md := LegacyLoadMessageDesc(t) |
| 42 | mt := new(MessageInfo) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 43 | mt.GoType = t |
Joe Tsai | b2f66be | 2019-05-22 00:42:45 -0400 | [diff] [blame] | 44 | mt.PBType = &prototype.Message{ |
| 45 | MessageDescriptor: md, |
| 46 | NewMessage: func() pref.Message { |
| 47 | return mt.MessageOf(reflect.New(t.Elem()).Interface()) |
| 48 | }, |
| 49 | } |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 50 | if mt, ok := legacyMessageTypeCache.LoadOrStore(t, mt); ok { |
| 51 | return mt.(*MessageInfo) |
Joe Tsai | b936504 | 2019-03-19 14:14:29 -0700 | [diff] [blame] | 52 | } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 53 | return mt |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 56 | var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 57 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 58 | // LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type, |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 59 | // 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] | 60 | // |
| 61 | // This is exported for testing purposes. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 62 | func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 63 | // Fast-path: check if a MessageDescriptor is cached for this concrete type. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 64 | if mi, ok := legacyMessageDescCache.Load(t); ok { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 65 | return mi.(pref.MessageDescriptor) |
| 66 | } |
| 67 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 68 | // Slow-path: initialize MessageDescriptor from the raw descriptor. |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 69 | mv := reflect.New(t.Elem()).Interface() |
| 70 | if _, ok := mv.(pref.ProtoMessage); ok { |
| 71 | panic(fmt.Sprintf("%v already implements proto.Message", t)) |
| 72 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 73 | mdV1, ok := mv.(messageV1) |
| 74 | if !ok { |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 75 | return aberrantLoadMessageDesc(t) |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 76 | } |
| 77 | b, idxs := mdV1.Descriptor() |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 78 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 79 | md := legacyLoadFileDesc(b).Messages().Get(idxs[0]) |
| 80 | for _, i := range idxs[1:] { |
| 81 | md = md.Messages().Get(i) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 82 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 83 | if md, ok := legacyMessageDescCache.LoadOrStore(t, md); ok { |
| 84 | return md.(protoreflect.MessageDescriptor) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 85 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 86 | return md |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 87 | } |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 88 | |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 89 | var ( |
| 90 | aberrantMessageDescLock sync.Mutex |
| 91 | aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor |
| 92 | ) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 93 | |
Joe Tsai | a804450 | 2019-07-06 17:38:44 -0700 | [diff] [blame] | 94 | // aberrantLoadMessageDesc returns an EnumDescriptor derived from the Go type, |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 95 | // which must not implement protoreflect.ProtoMessage or messageV1. |
| 96 | // |
| 97 | // This is a best-effort derivation of the message descriptor using the protobuf |
| 98 | // tags on the struct fields. |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 99 | func aberrantLoadMessageDesc(t reflect.Type) pref.MessageDescriptor { |
| 100 | aberrantMessageDescLock.Lock() |
| 101 | defer aberrantMessageDescLock.Unlock() |
| 102 | if aberrantMessageDescCache == nil { |
| 103 | aberrantMessageDescCache = make(map[reflect.Type]protoreflect.MessageDescriptor) |
| 104 | } |
| 105 | return aberrantLoadMessageDescReentrant(t) |
| 106 | } |
| 107 | func aberrantLoadMessageDescReentrant(t reflect.Type) pref.MessageDescriptor { |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 108 | // Fast-path: check if an MessageDescriptor is cached for this concrete type. |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 109 | if md, ok := aberrantMessageDescCache[t]; ok { |
| 110 | return md |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 113 | // Slow-path: construct a descriptor from the Go struct type (best-effort). |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 114 | // Cache the MessageDescriptor early on so that we can resolve internal |
| 115 | // cyclic references. |
| 116 | md := &filedesc.Message{L2: new(filedesc.MessageL2)} |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 117 | md.L0.FullName = aberrantDeriveFullName(t.Elem()) |
| 118 | md.L0.ParentFile = filedesc.SurrogateProto2 |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 119 | aberrantMessageDescCache[t] = md |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 120 | |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 121 | // Try to determine if the message is using proto3 by checking scalars. |
| 122 | for i := 0; i < t.Elem().NumField(); i++ { |
| 123 | f := t.Elem().Field(i) |
| 124 | if tag := f.Tag.Get("protobuf"); tag != "" { |
| 125 | switch f.Type.Kind() { |
| 126 | case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String: |
| 127 | md.L0.ParentFile = filedesc.SurrogateProto3 |
| 128 | } |
| 129 | for _, s := range strings.Split(tag, ",") { |
| 130 | if s == "proto3" { |
| 131 | md.L0.ParentFile = filedesc.SurrogateProto3 |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Obtain a list of oneof wrapper types. |
| 138 | var oneofWrappers []reflect.Type |
| 139 | if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok { |
| 140 | vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3] |
| 141 | for _, v := range vs.Interface().([]interface{}) { |
| 142 | oneofWrappers = append(oneofWrappers, reflect.TypeOf(v)) |
| 143 | } |
| 144 | } |
| 145 | if fn, ok := t.MethodByName("XXX_OneofWrappers"); ok { |
| 146 | vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0] |
| 147 | for _, v := range vs.Interface().([]interface{}) { |
| 148 | oneofWrappers = append(oneofWrappers, reflect.TypeOf(v)) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Obtain a list of the extension ranges. |
| 153 | if fn, ok := t.MethodByName("ExtensionRangeArray"); ok { |
| 154 | vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0] |
| 155 | for i := 0; i < vs.Len(); i++ { |
| 156 | v := vs.Index(i) |
| 157 | md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, [2]pref.FieldNumber{ |
| 158 | pref.FieldNumber(v.FieldByName("Start").Int()), |
| 159 | pref.FieldNumber(v.FieldByName("End").Int() + 1), |
| 160 | }) |
| 161 | md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, nil) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // Derive the message fields by inspecting the struct fields. |
| 166 | for i := 0; i < t.Elem().NumField(); i++ { |
| 167 | f := t.Elem().Field(i) |
| 168 | if tag := f.Tag.Get("protobuf"); tag != "" { |
| 169 | tagKey := f.Tag.Get("protobuf_key") |
| 170 | tagVal := f.Tag.Get("protobuf_val") |
| 171 | aberrantAppendField(md, f.Type, tag, tagKey, tagVal) |
| 172 | } |
| 173 | if tag := f.Tag.Get("protobuf_oneof"); tag != "" { |
| 174 | n := len(md.L2.Oneofs.List) |
| 175 | md.L2.Oneofs.List = append(md.L2.Oneofs.List, filedesc.Oneof{}) |
| 176 | od := &md.L2.Oneofs.List[n] |
| 177 | od.L0.FullName = md.FullName().Append(pref.Name(tag)) |
| 178 | od.L0.ParentFile = md.L0.ParentFile |
| 179 | od.L0.Parent = md |
| 180 | od.L0.Index = n |
| 181 | |
| 182 | for _, t := range oneofWrappers { |
| 183 | if t.Implements(f.Type) { |
| 184 | f := t.Elem().Field(0) |
| 185 | if tag := f.Tag.Get("protobuf"); tag != "" { |
| 186 | aberrantAppendField(md, f.Type, tag, "", "") |
| 187 | fd := &md.L2.Fields.List[len(md.L2.Fields.List)-1] |
| 188 | fd.L1.ContainingOneof = od |
| 189 | od.L1.Fields.List = append(od.L1.Fields.List, fd) |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // TODO: Use custom Marshal/Unmarshal methods for the fast-path? |
| 197 | |
| 198 | return md |
| 199 | } |
| 200 | |
| 201 | func aberrantAppendField(md *filedesc.Message, goType reflect.Type, tag, tagKey, tagVal string) { |
| 202 | t := goType |
| 203 | isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct |
| 204 | isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 |
| 205 | if isOptional || isRepeated { |
| 206 | t = t.Elem() |
| 207 | } |
| 208 | fd := ptag.Unmarshal(tag, t, placeholderEnumValues{}).(*filedesc.Field) |
| 209 | |
| 210 | // Append field descriptor to the message. |
| 211 | n := len(md.L2.Fields.List) |
| 212 | md.L2.Fields.List = append(md.L2.Fields.List, *fd) |
| 213 | fd = &md.L2.Fields.List[n] |
| 214 | fd.L0.FullName = md.FullName().Append(fd.Name()) |
| 215 | fd.L0.ParentFile = md.L0.ParentFile |
| 216 | fd.L0.Parent = md |
| 217 | fd.L0.Index = n |
| 218 | |
| 219 | if fd.L1.IsWeak || fd.L1.HasPacked { |
| 220 | fd.L1.Options = func() pref.ProtoMessage { |
| 221 | opts := descopts.Field.ProtoReflect().New() |
| 222 | if fd.L1.IsWeak { |
| 223 | opts.Set(opts.Descriptor().Fields().ByName("weak"), protoreflect.ValueOf(true)) |
| 224 | } |
| 225 | if fd.L1.HasPacked { |
| 226 | opts.Set(opts.Descriptor().Fields().ByName("packed"), protoreflect.ValueOf(fd.L1.IsPacked)) |
| 227 | } |
| 228 | return opts.Interface() |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // Populate Enum and Message. |
| 233 | if fd.Enum() == nil && fd.Kind() == pref.EnumKind { |
| 234 | switch v := reflect.Zero(t).Interface().(type) { |
| 235 | case pref.Enum: |
| 236 | fd.L1.Enum = v.Descriptor() |
| 237 | default: |
| 238 | fd.L1.Enum = LegacyLoadEnumDesc(t) |
| 239 | } |
| 240 | } |
| 241 | if fd.Message() == nil && (fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind) { |
| 242 | switch v := reflect.Zero(t).Interface().(type) { |
| 243 | case pref.ProtoMessage: |
| 244 | fd.L1.Message = v.ProtoReflect().Descriptor() |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 245 | case messageV1: |
| 246 | fd.L1.Message = LegacyLoadMessageDesc(t) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 247 | default: |
| 248 | if t.Kind() == reflect.Map { |
| 249 | n := len(md.L1.Messages.List) |
| 250 | md.L1.Messages.List = append(md.L1.Messages.List, filedesc.Message{L2: new(filedesc.MessageL2)}) |
| 251 | md2 := &md.L1.Messages.List[n] |
Joe Tsai | 97a8739 | 2019-07-07 01:49:59 -0700 | [diff] [blame^] | 252 | 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] | 253 | md2.L0.ParentFile = md.L0.ParentFile |
| 254 | md2.L0.Parent = md |
| 255 | md2.L0.Index = n |
| 256 | |
| 257 | md2.L2.IsMapEntry = true |
| 258 | md2.L2.Options = func() pref.ProtoMessage { |
| 259 | opts := descopts.Message.ProtoReflect().New() |
| 260 | opts.Set(opts.Descriptor().Fields().ByName("map_entry"), protoreflect.ValueOf(true)) |
| 261 | return opts.Interface() |
| 262 | } |
| 263 | |
| 264 | aberrantAppendField(md2, t.Key(), tagKey, "", "") |
| 265 | aberrantAppendField(md2, t.Elem(), tagVal, "", "") |
| 266 | |
| 267 | fd.L1.Message = md2 |
| 268 | break |
| 269 | } |
Joe Tsai | 32e8a52 | 2019-07-02 10:51:24 -0700 | [diff] [blame] | 270 | fd.L1.Message = aberrantLoadMessageDescReentrant(t) |
Joe Tsai | 851185d | 2019-07-01 13:45:52 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | type placeholderEnumValues struct { |
| 276 | protoreflect.EnumValueDescriptors |
| 277 | } |
| 278 | |
| 279 | func (placeholderEnumValues) ByNumber(n pref.EnumNumber) pref.EnumValueDescriptor { |
| 280 | return filedesc.PlaceholderEnumValue(pref.FullName(fmt.Sprintf("UNKNOWN_%d", n))) |
| 281 | } |
| 282 | |
| 283 | // aberrantMapEntryName derives the name for a map entry message. |
| 284 | // See protoc v3.8.0: src/google/protobuf/descriptor.cc:254-276,6057 |
| 285 | func aberrantMapEntryName(s pref.Name) pref.Name { |
| 286 | var b []byte |
| 287 | upperNext := true |
| 288 | for _, c := range s { |
| 289 | switch { |
| 290 | case c == '_': |
| 291 | upperNext = true |
| 292 | case upperNext: |
| 293 | b = append(b, byte(unicode.ToUpper(c))) |
| 294 | upperNext = false |
| 295 | default: |
| 296 | b = append(b, byte(c)) |
| 297 | } |
| 298 | } |
| 299 | b = append(b, "Entry"...) |
| 300 | return pref.Name(b) |
| 301 | } |