blob: b484067309439bb67a73d179527cf6473e2521ec [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()
36 if xt.Descriptor().Cardinality() == pref.Repeated {
37 tt = tt.Elem().Elem()
38 }
39 xi := &ExtensionInfo{}
40 InitExtensionInfo(xi, xt.Descriptor().Descriptor(), tt)
41 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)
Damien Neil954bd922019-07-17 16:52:10 -070057 if mv, ok := mv.(Unwrapper); ok {
Joe Tsai08e00302018-11-26 22:32:06 -080058 t = reflect.TypeOf(mv.ProtoUnwrap())
59 }
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
74 case reflect.Ptr:
75 if extType.Elem().Kind() == reflect.Slice {
76 extType = extType.Elem() // *[]T -> []T for repeated fields
77 }
78 }
79
80 // Reconstruct the legacy enum full name, which is an odd mixture of the
81 // proto package name with the Go type name.
82 var enumName string
Damien Neil92f76182019-08-02 16:58:08 -070083 if xd.Kind() == pref.EnumKind {
Joe Tsai08e00302018-11-26 22:32:06 -080084 // Derive Go type name.
Joe Tsai0fc49f82019-05-01 12:29:25 -070085 t := extType
86 if t.Kind() == reflect.Ptr || t.Kind() == reflect.Slice {
87 t = t.Elem()
Joe Tsai08e00302018-11-26 22:32:06 -080088 }
Joe Tsai0fc49f82019-05-01 12:29:25 -070089 enumName = t.Name()
Joe Tsai08e00302018-11-26 22:32:06 -080090
91 // Derive the proto package name.
92 // For legacy enums, obtain the proto package from the raw descriptor.
93 var protoPkg string
Damien Neil92f76182019-08-02 16:58:08 -070094 if fd := xd.Enum().ParentFile(); fd != nil {
Joe Tsai08e00302018-11-26 22:32:06 -080095 protoPkg = string(fd.Package())
96 }
Joe Tsai0fc49f82019-05-01 12:29:25 -070097 if ed, ok := reflect.Zero(t).Interface().(enumV1); ok && protoPkg == "" {
Joe Tsai08e00302018-11-26 22:32:06 -080098 b, _ := ed.EnumDescriptor()
Joe Tsaid8881392019-06-06 13:01:53 -070099 protoPkg = string(legacyLoadFileDesc(b).Package())
Joe Tsai08e00302018-11-26 22:32:06 -0800100 }
101
102 if protoPkg != "" {
103 enumName = protoPkg + "." + enumName
104 }
105 }
106
107 // Derive the proto file that the extension was declared within.
108 var filename string
Damien Neil92f76182019-08-02 16:58:08 -0700109 if fd := xd.ParentFile(); fd != nil {
Joe Tsai08e00302018-11-26 22:32:06 -0800110 filename = fd.Path()
111 }
112
Joe Tsai945a1702019-07-20 14:57:56 -0700113 // For MessageSet extensions, the name used is the parent message.
114 name := xd.FullName()
115 if messageset.IsMessageSetExtension(xd) {
116 name = name.Parent()
117 }
118
Damien Neilf1e905b2019-08-08 15:45:59 -0700119 xi.ExtendedType = parent
120 xi.ExtensionType = reflect.Zero(extType).Interface()
121 xi.Field = int32(xd.Number())
Joe Tsai945a1702019-07-20 14:57:56 -0700122 xi.Name = string(name)
Damien Neilf1e905b2019-08-08 15:45:59 -0700123 xi.Tag = ptag.Marshal(xd, enumName)
124 xi.Filename = filename
Joe Tsai08e00302018-11-26 22:32:06 -0800125}
126
Damien Neilf1e905b2019-08-08 15:45:59 -0700127// initFromLegacy initializes an ExtensionInfo from
128// the contents of the deprecated exported fields of the type.
129func (xi *ExtensionInfo) initFromLegacy() {
Joe Tsaid8881392019-06-06 13:01:53 -0700130 // Resolve enum or message dependencies.
131 var ed pref.EnumDescriptor
132 var md pref.MessageDescriptor
Damien Neilf1e905b2019-08-08 15:45:59 -0700133 t := reflect.TypeOf(xi.ExtensionType)
Joe Tsai08e00302018-11-26 22:32:06 -0800134 isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
135 isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
136 if isOptional || isRepeated {
137 t = t.Elem()
138 }
Joe Tsaid8881392019-06-06 13:01:53 -0700139 switch v := reflect.Zero(t).Interface().(type) {
140 case pref.Enum:
141 ed = v.Descriptor()
142 case enumV1:
143 ed = LegacyLoadEnumDesc(t)
144 case pref.ProtoMessage:
145 md = v.ProtoReflect().Descriptor()
146 case messageV1:
147 md = LegacyLoadMessageDesc(t)
148 }
149
150 // Derive basic field information from the struct tag.
151 var evs pref.EnumValueDescriptors
152 if ed != nil {
153 evs = ed.Values()
154 }
Damien Neilf1e905b2019-08-08 15:45:59 -0700155 fd := ptag.Unmarshal(xi.Tag, t, evs).(*filedesc.Field)
Joe Tsai08e00302018-11-26 22:32:06 -0800156
157 // Construct a v2 ExtensionType.
Joe Tsaid8881392019-06-06 13:01:53 -0700158 xd := &filedesc.Extension{L2: new(filedesc.ExtensionL2)}
159 xd.L0.ParentFile = filedesc.SurrogateProto2
Damien Neilf1e905b2019-08-08 15:45:59 -0700160 xd.L0.FullName = pref.FullName(xi.Name)
161 xd.L1.Number = pref.FieldNumber(xi.Field)
Joe Tsaid8881392019-06-06 13:01:53 -0700162 xd.L2.Cardinality = fd.L1.Cardinality
163 xd.L1.Kind = fd.L1.Kind
164 xd.L2.IsPacked = fd.L1.IsPacked
165 xd.L2.Default = fd.L1.Default
Damien Neilf1e905b2019-08-08 15:45:59 -0700166 xd.L1.Extendee = Export{}.MessageDescriptorOf(xi.ExtendedType)
Joe Tsaid8881392019-06-06 13:01:53 -0700167 xd.L2.Enum = ed
168 xd.L2.Message = md
Joe Tsai945a1702019-07-20 14:57:56 -0700169
170 // Derive real extension field name for MessageSets.
171 if messageset.IsMessageSet(xd.L1.Extendee) && md.FullName() == xd.L0.FullName {
172 xd.L0.FullName = xd.L0.FullName.Append(messageset.ExtensionName)
173 }
174
Damien Neilf1e905b2019-08-08 15:45:59 -0700175 tt := reflect.TypeOf(xi.ExtensionType)
Damien Neil954bd922019-07-17 16:52:10 -0700176 if isOptional {
177 tt = tt.Elem()
178 } else if isRepeated {
179 tt = reflect.PtrTo(tt)
180 }
Damien Neilf1e905b2019-08-08 15:45:59 -0700181 xi.desc = xd
182 xi.goType = tt
Joe Tsai08e00302018-11-26 22:32:06 -0800183}