blob: bfa6e1bf5a18998ed1dfadd9674897c434c9b978 [file] [log] [blame]
Joe Tsai08e00302018-11-26 22:32:06 -08001// 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 Tsai21ade492019-05-22 13:42:54 -04005package impl
Joe Tsai08e00302018-11-26 22:32:06 -08006
7import (
Joe Tsai08e00302018-11-26 22:32:06 -08008 "reflect"
Joe Tsai62517cc2018-12-04 14:00:01 -08009 "sync"
Joe Tsai08e00302018-11-26 22:32:06 -080010
Joe Tsai945a1702019-07-20 14:57:56 -070011 "google.golang.org/protobuf/internal/encoding/messageset"
Damien Neile89e6242019-05-13 23:55:40 -070012 ptag "google.golang.org/protobuf/internal/encoding/tag"
Joe Tsaid8881392019-06-06 13:01:53 -070013 "google.golang.org/protobuf/internal/filedesc"
Damien Neile89e6242019-05-13 23:55:40 -070014 pref "google.golang.org/protobuf/reflect/protoreflect"
15 preg "google.golang.org/protobuf/reflect/protoregistry"
16 piface "google.golang.org/protobuf/runtime/protoiface"
Joe Tsai08e00302018-11-26 22:32:06 -080017)
18
Damien Neilf1e905b2019-08-08 15:45:59 -070019var legacyExtensionInfoCache sync.Map // map[protoreflect.ExtensionType]*ExtensionInfo
Joe Tsai62517cc2018-12-04 14:00:01 -080020
Damien Neilf1e905b2019-08-08 15:45:59 -070021// legacyExtensionDescFromType converts a protoreflect.ExtensionType to an
22// ExtensionInfo. The returned ExtensionInfo must not be mutated.
23func legacyExtensionDescFromType(xt pref.ExtensionType) *ExtensionInfo {
24 // Fast-path: check whether this is an ExtensionInfo.
25 if xt, ok := xt.(*ExtensionInfo); ok {
26 return xt
Joe Tsaiafb455e2019-03-14 16:08:22 -070027 }
28
Joe Tsai62517cc2018-12-04 14:00:01 -080029 // Fast-path: check the cache for whether this ExtensionType has already
Damien Neilf1e905b2019-08-08 15:45:59 -070030 // been converted to an ExtensionInfo.
31 if d, ok := legacyExtensionInfoCache.Load(xt); ok {
32 return d.(*ExtensionInfo)
Joe Tsai08e00302018-11-26 22:32:06 -080033 }
34
Damien Neilf1e905b2019-08-08 15:45:59 -070035 tt := xt.GoType()
Damien Neil79bfdbe2019-08-28 11:08:22 -070036 if xt.TypeDescriptor().Cardinality() == pref.Repeated {
Damien Neilf1e905b2019-08-08 15:45:59 -070037 tt = tt.Elem().Elem()
38 }
39 xi := &ExtensionInfo{}
Damien Neil79bfdbe2019-08-28 11:08:22 -070040 InitExtensionInfo(xi, xt.TypeDescriptor().Descriptor(), tt)
Damien Neilf1e905b2019-08-08 15:45:59 -070041 xi.lazyInit() // populate legacy fields
42
43 if xi, ok := legacyExtensionInfoCache.LoadOrStore(xt, xi); ok {
44 return xi.(*ExtensionInfo)
45 }
46 return xi
47}
48
49func (xi *ExtensionInfo) initToLegacy() {
50 xd := xi.desc
Joe Tsai4fddeba2019-03-20 18:29:32 -070051 var parent piface.MessageV1
Damien Neil92f76182019-08-02 16:58:08 -070052 messageName := xd.ContainingMessage().FullName()
Joe Tsaiac31a352019-05-13 14:32:56 -070053 if mt, _ := preg.GlobalTypes.FindMessageByName(messageName); mt != nil {
Joe Tsai08e00302018-11-26 22:32:06 -080054 // Create a new parent message and unwrap it if possible.
Joe Tsai3bc7d6f2019-01-09 02:57:13 -080055 mv := mt.New().Interface()
Joe Tsai08e00302018-11-26 22:32:06 -080056 t := reflect.TypeOf(mv)
Joe Tsaifd528ff2019-09-03 16:30:39 -070057 if mv, ok := mv.(unwrapper); ok {
58 t = reflect.TypeOf(mv.protoUnwrap())
Joe Tsai08e00302018-11-26 22:32:06 -080059 }
60
61 // Check whether the message implements the legacy v1 Message interface.
62 mz := reflect.Zero(t).Interface()
Joe Tsai4fddeba2019-03-20 18:29:32 -070063 if mz, ok := mz.(piface.MessageV1); ok {
Joe Tsai08e00302018-11-26 22:32:06 -080064 parent = mz
65 }
66 }
67
68 // Determine the v1 extension type, which is unfortunately not the same as
69 // the v2 ExtensionType.GoType.
Damien Neilf1e905b2019-08-08 15:45:59 -070070 extType := xi.goType
Joe Tsai08e00302018-11-26 22:32:06 -080071 switch extType.Kind() {
72 case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
73 extType = reflect.PtrTo(extType) // T -> *T for singular scalar fields
Joe Tsai08e00302018-11-26 22:32:06 -080074 }
75
Joe Tsai2e7817f2019-08-23 12:18:57 -070076 // Reconstruct the legacy enum full name.
Joe Tsai08e00302018-11-26 22:32:06 -080077 var enumName string
Damien Neil92f76182019-08-02 16:58:08 -070078 if xd.Kind() == pref.EnumKind {
Joe Tsai2e7817f2019-08-23 12:18:57 -070079 enumName = legacyEnumName(xd.Enum())
Joe Tsai08e00302018-11-26 22:32:06 -080080 }
81
82 // Derive the proto file that the extension was declared within.
83 var filename string
Damien Neil92f76182019-08-02 16:58:08 -070084 if fd := xd.ParentFile(); fd != nil {
Joe Tsai08e00302018-11-26 22:32:06 -080085 filename = fd.Path()
86 }
87
Joe Tsai945a1702019-07-20 14:57:56 -070088 // For MessageSet extensions, the name used is the parent message.
89 name := xd.FullName()
90 if messageset.IsMessageSetExtension(xd) {
91 name = name.Parent()
92 }
93
Damien Neilf1e905b2019-08-08 15:45:59 -070094 xi.ExtendedType = parent
95 xi.ExtensionType = reflect.Zero(extType).Interface()
96 xi.Field = int32(xd.Number())
Joe Tsai945a1702019-07-20 14:57:56 -070097 xi.Name = string(name)
Damien Neilf1e905b2019-08-08 15:45:59 -070098 xi.Tag = ptag.Marshal(xd, enumName)
99 xi.Filename = filename
Joe Tsai08e00302018-11-26 22:32:06 -0800100}
101
Damien Neilf1e905b2019-08-08 15:45:59 -0700102// initFromLegacy initializes an ExtensionInfo from
103// the contents of the deprecated exported fields of the type.
104func (xi *ExtensionInfo) initFromLegacy() {
Joe Tsaid8881392019-06-06 13:01:53 -0700105 // Resolve enum or message dependencies.
106 var ed pref.EnumDescriptor
107 var md pref.MessageDescriptor
Damien Neilf1e905b2019-08-08 15:45:59 -0700108 t := reflect.TypeOf(xi.ExtensionType)
Joe Tsai08e00302018-11-26 22:32:06 -0800109 isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
110 isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
111 if isOptional || isRepeated {
112 t = t.Elem()
113 }
Joe Tsaid8881392019-06-06 13:01:53 -0700114 switch v := reflect.Zero(t).Interface().(type) {
115 case pref.Enum:
116 ed = v.Descriptor()
117 case enumV1:
118 ed = LegacyLoadEnumDesc(t)
119 case pref.ProtoMessage:
120 md = v.ProtoReflect().Descriptor()
121 case messageV1:
122 md = LegacyLoadMessageDesc(t)
123 }
124
125 // Derive basic field information from the struct tag.
126 var evs pref.EnumValueDescriptors
127 if ed != nil {
128 evs = ed.Values()
129 }
Damien Neilf1e905b2019-08-08 15:45:59 -0700130 fd := ptag.Unmarshal(xi.Tag, t, evs).(*filedesc.Field)
Joe Tsai08e00302018-11-26 22:32:06 -0800131
132 // Construct a v2 ExtensionType.
Joe Tsaid8881392019-06-06 13:01:53 -0700133 xd := &filedesc.Extension{L2: new(filedesc.ExtensionL2)}
134 xd.L0.ParentFile = filedesc.SurrogateProto2
Damien Neilf1e905b2019-08-08 15:45:59 -0700135 xd.L0.FullName = pref.FullName(xi.Name)
136 xd.L1.Number = pref.FieldNumber(xi.Field)
Joe Tsaid8881392019-06-06 13:01:53 -0700137 xd.L2.Cardinality = fd.L1.Cardinality
138 xd.L1.Kind = fd.L1.Kind
139 xd.L2.IsPacked = fd.L1.IsPacked
140 xd.L2.Default = fd.L1.Default
Damien Neilf1e905b2019-08-08 15:45:59 -0700141 xd.L1.Extendee = Export{}.MessageDescriptorOf(xi.ExtendedType)
Joe Tsaid8881392019-06-06 13:01:53 -0700142 xd.L2.Enum = ed
143 xd.L2.Message = md
Joe Tsai945a1702019-07-20 14:57:56 -0700144
145 // Derive real extension field name for MessageSets.
146 if messageset.IsMessageSet(xd.L1.Extendee) && md.FullName() == xd.L0.FullName {
147 xd.L0.FullName = xd.L0.FullName.Append(messageset.ExtensionName)
148 }
149
Damien Neilf1e905b2019-08-08 15:45:59 -0700150 tt := reflect.TypeOf(xi.ExtensionType)
Damien Neil954bd922019-07-17 16:52:10 -0700151 if isOptional {
152 tt = tt.Elem()
Damien Neil954bd922019-07-17 16:52:10 -0700153 }
Damien Neilf1e905b2019-08-08 15:45:59 -0700154 xi.desc = xd
155 xi.goType = tt
Joe Tsai08e00302018-11-26 22:32:06 -0800156}