Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [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 | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 8 | "reflect" |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 9 | "sync" |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 10 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 11 | ptag "google.golang.org/protobuf/internal/encoding/tag" |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/internal/filedesc" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 13 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 14 | preg "google.golang.org/protobuf/reflect/protoregistry" |
| 15 | piface "google.golang.org/protobuf/runtime/protoiface" |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 16 | ) |
| 17 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 18 | // legacyExtensionDescKey is a comparable version of protoiface.ExtensionDescV1 |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 19 | // suitable for use as a key in a map. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 20 | type legacyExtensionDescKey struct { |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 21 | typeV2 pref.ExtensionType |
| 22 | extendedType reflect.Type |
| 23 | extensionType reflect.Type |
| 24 | field int32 |
| 25 | name string |
| 26 | tag string |
| 27 | filename string |
| 28 | } |
| 29 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 30 | func legacyExtensionDescKeyOf(d *piface.ExtensionDescV1) legacyExtensionDescKey { |
| 31 | return legacyExtensionDescKey{ |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 32 | d.Type, |
| 33 | reflect.TypeOf(d.ExtendedType), |
| 34 | reflect.TypeOf(d.ExtensionType), |
| 35 | d.Field, d.Name, d.Tag, d.Filename, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | var ( |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 40 | legacyExtensionTypeCache sync.Map // map[legacyExtensionDescKey]protoreflect.ExtensionType |
| 41 | legacyExtensionDescCache sync.Map // map[protoreflect.ExtensionType]*protoiface.ExtensionDescV1 |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 42 | ) |
| 43 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 44 | // legacyExtensionDescFromType converts a v2 protoreflect.ExtensionType to a |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 45 | // protoiface.ExtensionDescV1. The returned ExtensionDesc must not be mutated. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 46 | func legacyExtensionDescFromType(xt pref.ExtensionType) *piface.ExtensionDescV1 { |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 47 | // Fast-path: check whether an extension desc is already nested within. |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 48 | if xt, ok := xt.(interface { |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 49 | ProtoLegacyExtensionDesc() *piface.ExtensionDescV1 |
| 50 | }); ok { |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 51 | if d := xt.ProtoLegacyExtensionDesc(); d != nil { |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 52 | return d |
| 53 | } |
| 54 | } |
| 55 | |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 56 | // Fast-path: check the cache for whether this ExtensionType has already |
| 57 | // been converted to a legacy descriptor. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 58 | if d, ok := legacyExtensionDescCache.Load(xt); ok { |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 59 | return d.(*piface.ExtensionDescV1) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Determine the parent type if possible. |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 63 | xd := xt.Descriptor() |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 64 | var parent piface.MessageV1 |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 65 | messageName := xd.ContainingMessage().FullName() |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 66 | if mt, _ := preg.GlobalTypes.FindMessageByName(messageName); mt != nil { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 67 | // Create a new parent message and unwrap it if possible. |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 68 | mv := mt.New().Interface() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 69 | t := reflect.TypeOf(mv) |
Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 70 | if mv, ok := mv.(Unwrapper); ok { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 71 | t = reflect.TypeOf(mv.ProtoUnwrap()) |
| 72 | } |
| 73 | |
| 74 | // Check whether the message implements the legacy v1 Message interface. |
| 75 | mz := reflect.Zero(t).Interface() |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 76 | if mz, ok := mz.(piface.MessageV1); ok { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 77 | parent = mz |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Determine the v1 extension type, which is unfortunately not the same as |
| 82 | // the v2 ExtensionType.GoType. |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 83 | extType := xt.GoType() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 84 | switch extType.Kind() { |
| 85 | case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String: |
| 86 | extType = reflect.PtrTo(extType) // T -> *T for singular scalar fields |
| 87 | case reflect.Ptr: |
| 88 | if extType.Elem().Kind() == reflect.Slice { |
| 89 | extType = extType.Elem() // *[]T -> []T for repeated fields |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Reconstruct the legacy enum full name, which is an odd mixture of the |
| 94 | // proto package name with the Go type name. |
| 95 | var enumName string |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 96 | if xd.Kind() == pref.EnumKind { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 97 | // Derive Go type name. |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 98 | t := extType |
| 99 | if t.Kind() == reflect.Ptr || t.Kind() == reflect.Slice { |
| 100 | t = t.Elem() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 101 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 102 | enumName = t.Name() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 103 | |
| 104 | // Derive the proto package name. |
| 105 | // For legacy enums, obtain the proto package from the raw descriptor. |
| 106 | var protoPkg string |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 107 | if fd := xd.Enum().ParentFile(); fd != nil { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 108 | protoPkg = string(fd.Package()) |
| 109 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 110 | if ed, ok := reflect.Zero(t).Interface().(enumV1); ok && protoPkg == "" { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 111 | b, _ := ed.EnumDescriptor() |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 112 | protoPkg = string(legacyLoadFileDesc(b).Package()) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | if protoPkg != "" { |
| 116 | enumName = protoPkg + "." + enumName |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Derive the proto file that the extension was declared within. |
| 121 | var filename string |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 122 | if fd := xd.ParentFile(); fd != nil { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 123 | filename = fd.Path() |
| 124 | } |
| 125 | |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 126 | // Construct and return a ExtensionDescV1. |
| 127 | d := &piface.ExtensionDescV1{ |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 128 | Type: xt, |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 129 | ExtendedType: parent, |
| 130 | ExtensionType: reflect.Zero(extType).Interface(), |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 131 | Field: int32(xd.Number()), |
| 132 | Name: string(xd.FullName()), |
| 133 | Tag: ptag.Marshal(xd, enumName), |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 134 | Filename: filename, |
| 135 | } |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 136 | if d, ok := legacyExtensionDescCache.LoadOrStore(xt, d); ok { |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 137 | return d.(*piface.ExtensionDescV1) |
Joe Tsai | b936504 | 2019-03-19 14:14:29 -0700 | [diff] [blame] | 138 | } |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 139 | return d |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 142 | // legacyExtensionTypeFromDesc converts a protoiface.ExtensionDescV1 to a |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 143 | // v2 protoreflect.ExtensionType. The returned descriptor type takes ownership |
| 144 | // of the input extension desc. The input must not be mutated so long as the |
| 145 | // returned type is still in use. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 146 | func legacyExtensionTypeFromDesc(d *piface.ExtensionDescV1) pref.ExtensionType { |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 147 | // Fast-path: check whether an extension type is already nested within. |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 148 | if d.Type != nil { |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 149 | return d.Type |
| 150 | } |
| 151 | |
| 152 | // Fast-path: check the cache for whether this ExtensionType has already |
| 153 | // been converted from a legacy descriptor. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 154 | dk := legacyExtensionDescKeyOf(d) |
| 155 | if t, ok := legacyExtensionTypeCache.Load(dk); ok { |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 156 | return t.(pref.ExtensionType) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 159 | // Resolve enum or message dependencies. |
| 160 | var ed pref.EnumDescriptor |
| 161 | var md pref.MessageDescriptor |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 162 | t := reflect.TypeOf(d.ExtensionType) |
| 163 | isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct |
| 164 | isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 |
| 165 | if isOptional || isRepeated { |
| 166 | t = t.Elem() |
| 167 | } |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 168 | switch v := reflect.Zero(t).Interface().(type) { |
| 169 | case pref.Enum: |
| 170 | ed = v.Descriptor() |
| 171 | case enumV1: |
| 172 | ed = LegacyLoadEnumDesc(t) |
| 173 | case pref.ProtoMessage: |
| 174 | md = v.ProtoReflect().Descriptor() |
| 175 | case messageV1: |
| 176 | md = LegacyLoadMessageDesc(t) |
| 177 | } |
| 178 | |
| 179 | // Derive basic field information from the struct tag. |
| 180 | var evs pref.EnumValueDescriptors |
| 181 | if ed != nil { |
| 182 | evs = ed.Values() |
| 183 | } |
| 184 | fd := ptag.Unmarshal(d.Tag, t, evs).(*filedesc.Field) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 185 | |
| 186 | // Construct a v2 ExtensionType. |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 187 | xd := &filedesc.Extension{L2: new(filedesc.ExtensionL2)} |
| 188 | xd.L0.ParentFile = filedesc.SurrogateProto2 |
| 189 | xd.L0.FullName = pref.FullName(d.Name) |
| 190 | xd.L1.Number = pref.FieldNumber(d.Field) |
| 191 | xd.L2.Cardinality = fd.L1.Cardinality |
| 192 | xd.L1.Kind = fd.L1.Kind |
| 193 | xd.L2.IsPacked = fd.L1.IsPacked |
| 194 | xd.L2.Default = fd.L1.Default |
| 195 | xd.L1.Extendee = Export{}.MessageDescriptorOf(d.ExtendedType) |
| 196 | xd.L2.Enum = ed |
| 197 | xd.L2.Message = md |
Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 198 | tt := reflect.TypeOf(d.ExtensionType) |
| 199 | if isOptional { |
| 200 | tt = tt.Elem() |
| 201 | } else if isRepeated { |
| 202 | tt = reflect.PtrTo(tt) |
| 203 | } |
| 204 | xt := LegacyExtensionTypeOf(xd, tt) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 205 | |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 206 | // Cache the conversion for both directions. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 207 | legacyExtensionDescCache.LoadOrStore(xt, d) |
| 208 | if xt, ok := legacyExtensionTypeCache.LoadOrStore(dk, xt); ok { |
Joe Tsai | b936504 | 2019-03-19 14:14:29 -0700 | [diff] [blame] | 209 | return xt.(pref.ExtensionType) |
| 210 | } |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 211 | return xt |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 214 | // LegacyExtensionTypeOf returns a protoreflect.ExtensionType where the |
Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 215 | // element type of the field is t. |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 216 | // |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 217 | // This is exported for testing purposes. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 218 | func LegacyExtensionTypeOf(xd pref.ExtensionDescriptor, t reflect.Type) pref.ExtensionType { |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 219 | xt := &legacyExtensionType{ |
| 220 | typ: t, |
| 221 | conv: NewConverter(t, xd), |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 222 | } |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 223 | xt.desc = &extDesc{xd, xt} |
| 224 | return xt |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame] | 227 | type legacyExtensionType struct { |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 228 | desc pref.ExtensionTypeDescriptor |
Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 229 | typ reflect.Type |
| 230 | conv Converter |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 233 | func (x *legacyExtensionType) GoType() reflect.Type { return x.typ } |
| 234 | func (x *legacyExtensionType) New() pref.Value { return x.conv.New() } |
Damien Neil | d4f0800 | 2019-08-07 12:21:41 -0700 | [diff] [blame] | 235 | func (x *legacyExtensionType) Zero() pref.Value { return x.conv.Zero() } |
Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 236 | func (x *legacyExtensionType) ValueOf(v interface{}) pref.Value { |
| 237 | return x.conv.PBValueOf(reflect.ValueOf(v)) |
| 238 | } |
| 239 | func (x *legacyExtensionType) InterfaceOf(v pref.Value) interface{} { |
| 240 | return x.conv.GoValueOf(v).Interface() |
| 241 | } |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 242 | func (x *legacyExtensionType) Descriptor() pref.ExtensionTypeDescriptor { return x.desc } |
| 243 | |
| 244 | type extDesc struct { |
| 245 | pref.ExtensionDescriptor |
| 246 | xt *legacyExtensionType |
| 247 | } |
| 248 | |
| 249 | func (t *extDesc) Type() pref.ExtensionType { return t.xt } |
| 250 | func (t *extDesc) Descriptor() pref.ExtensionDescriptor { return t.ExtensionDescriptor } |