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 | |
| 5 | package legacy |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "reflect" |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 10 | "sync" |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 11 | |
| 12 | papi "github.com/golang/protobuf/protoapi" |
| 13 | ptag "github.com/golang/protobuf/v2/internal/encoding/tag" |
| 14 | pimpl "github.com/golang/protobuf/v2/internal/impl" |
Joe Tsai | b3960a7 | 2018-12-05 10:35:35 -0800 | [diff] [blame] | 15 | pfmt "github.com/golang/protobuf/v2/internal/typefmt" |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 16 | pvalue "github.com/golang/protobuf/v2/internal/value" |
| 17 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
| 18 | ptype "github.com/golang/protobuf/v2/reflect/prototype" |
| 19 | ) |
| 20 | |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 21 | // extensionDescKey is a comparable version of protoapi.ExtensionDesc |
| 22 | // suitable for use as a key in a map. |
| 23 | type extensionDescKey struct { |
| 24 | typeV2 pref.ExtensionType |
| 25 | extendedType reflect.Type |
| 26 | extensionType reflect.Type |
| 27 | field int32 |
| 28 | name string |
| 29 | tag string |
| 30 | filename string |
| 31 | } |
| 32 | |
| 33 | func extensionDescKeyOf(d *papi.ExtensionDesc) extensionDescKey { |
| 34 | return extensionDescKey{ |
| 35 | d.Type, |
| 36 | reflect.TypeOf(d.ExtendedType), |
| 37 | reflect.TypeOf(d.ExtensionType), |
| 38 | d.Field, d.Name, d.Tag, d.Filename, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | var ( |
| 43 | extensionTypeCache sync.Map // map[extensionDescKey]protoreflect.ExtensionType |
| 44 | extensionDescCache sync.Map // map[protoreflect.ExtensionType]*protoapi.ExtensionDesc |
| 45 | ) |
| 46 | |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 47 | // extensionDescFromType converts a v2 protoreflect.ExtensionType to a |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 48 | // v1 protoapi.ExtensionDesc. The returned ExtensionDesc must not be mutated. |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 49 | func extensionDescFromType(t pref.ExtensionType) *papi.ExtensionDesc { |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 50 | // Fast-path: check the cache for whether this ExtensionType has already |
| 51 | // been converted to a legacy descriptor. |
| 52 | if d, ok := extensionDescCache.Load(t); ok { |
| 53 | return d.(*papi.ExtensionDesc) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Determine the parent type if possible. |
| 57 | var parent papi.Message |
| 58 | if mt, ok := t.ExtendedType().(pref.MessageType); ok { |
| 59 | // Create a new parent message and unwrap it if possible. |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 60 | mv := mt.New().Interface() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 61 | t := reflect.TypeOf(mv) |
| 62 | if mv, ok := mv.(pvalue.Unwrapper); ok { |
| 63 | t = reflect.TypeOf(mv.ProtoUnwrap()) |
| 64 | } |
| 65 | |
| 66 | // Check whether the message implements the legacy v1 Message interface. |
| 67 | mz := reflect.Zero(t).Interface() |
| 68 | if mz, ok := mz.(papi.Message); ok { |
| 69 | parent = mz |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Determine the v1 extension type, which is unfortunately not the same as |
| 74 | // the v2 ExtensionType.GoType. |
| 75 | extType := t.GoType() |
| 76 | switch extType.Kind() { |
| 77 | case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String: |
| 78 | extType = reflect.PtrTo(extType) // T -> *T for singular scalar fields |
| 79 | case reflect.Ptr: |
| 80 | if extType.Elem().Kind() == reflect.Slice { |
| 81 | extType = extType.Elem() // *[]T -> []T for repeated fields |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Reconstruct the legacy enum full name, which is an odd mixture of the |
| 86 | // proto package name with the Go type name. |
| 87 | var enumName string |
| 88 | if t.Kind() == pref.EnumKind { |
| 89 | // Derive Go type name. |
| 90 | // For legacy enums, unwrap the wrapper to get the underlying Go type. |
| 91 | et := t.EnumType().(pref.EnumType) |
| 92 | var ev interface{} = et.New(0) |
| 93 | if u, ok := ev.(pvalue.Unwrapper); ok { |
| 94 | ev = u.ProtoUnwrap() |
| 95 | } |
| 96 | enumName = reflect.TypeOf(ev).Name() |
| 97 | |
| 98 | // Derive the proto package name. |
| 99 | // For legacy enums, obtain the proto package from the raw descriptor. |
| 100 | var protoPkg string |
| 101 | if fd := parentFileDescriptor(et); fd != nil { |
| 102 | protoPkg = string(fd.Package()) |
| 103 | } |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 104 | if ed, ok := ev.(enumV1); ok && protoPkg == "" { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 105 | b, _ := ed.EnumDescriptor() |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 106 | protoPkg = loadFileDesc(b).GetPackage() |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if protoPkg != "" { |
| 110 | enumName = protoPkg + "." + enumName |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Derive the proto file that the extension was declared within. |
| 115 | var filename string |
| 116 | if fd := parentFileDescriptor(t); fd != nil { |
| 117 | filename = fd.Path() |
| 118 | } |
| 119 | |
| 120 | // Construct and return a v1 ExtensionDesc. |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 121 | d := &papi.ExtensionDesc{ |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 122 | Type: t, |
| 123 | ExtendedType: parent, |
| 124 | ExtensionType: reflect.Zero(extType).Interface(), |
| 125 | Field: int32(t.Number()), |
| 126 | Name: string(t.FullName()), |
| 127 | Tag: ptag.Marshal(t, enumName), |
| 128 | Filename: filename, |
| 129 | } |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 130 | extensionDescCache.Store(t, d) |
| 131 | return d |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 134 | // extensionTypeFromDesc converts a v1 protoapi.ExtensionDesc to a |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 135 | // v2 protoreflect.ExtensionType. The returned descriptor type takes ownership |
| 136 | // of the input extension desc. The input must not be mutated so long as the |
| 137 | // returned type is still in use. |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 138 | func extensionTypeFromDesc(d *papi.ExtensionDesc) pref.ExtensionType { |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 139 | // Fast-path: check whether an extension type is already nested within. |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 140 | if d.Type != nil { |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 141 | // Cache descriptor for future extensionDescFromType operation. |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 142 | // This assumes that there is only one legacy protoapi.ExtensionDesc |
| 143 | // that wraps any given specific protoreflect.ExtensionType. |
| 144 | extensionDescCache.LoadOrStore(d.Type, d) |
| 145 | return d.Type |
| 146 | } |
| 147 | |
| 148 | // Fast-path: check the cache for whether this ExtensionType has already |
| 149 | // been converted from a legacy descriptor. |
| 150 | dk := extensionDescKeyOf(d) |
| 151 | if t, ok := extensionTypeCache.Load(dk); ok { |
| 152 | return t.(pref.ExtensionType) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Derive basic field information from the struct tag. |
| 156 | t := reflect.TypeOf(d.ExtensionType) |
| 157 | isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct |
| 158 | isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 |
| 159 | if isOptional || isRepeated { |
| 160 | t = t.Elem() |
| 161 | } |
| 162 | f := ptag.Unmarshal(d.Tag, t) |
| 163 | |
| 164 | // Construct a v2 ExtensionType. |
| 165 | conv := pvalue.NewLegacyConverter(t, f.Kind, Export{}) |
| 166 | xd, err := ptype.NewExtension(&ptype.StandaloneExtension{ |
| 167 | FullName: pref.FullName(d.Name), |
| 168 | Number: pref.FieldNumber(d.Field), |
| 169 | Cardinality: f.Cardinality, |
| 170 | Kind: f.Kind, |
| 171 | Default: f.Default, |
| 172 | Options: f.Options, |
| 173 | EnumType: conv.EnumType, |
| 174 | MessageType: conv.MessageType, |
Herbie Ong | 2b0bba8 | 2018-12-20 12:56:42 -0800 | [diff] [blame] | 175 | ExtendedType: pimpl.Export{}.MessageTypeOf(d.ExtendedType), |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 176 | }) |
| 177 | if err != nil { |
| 178 | panic(err) |
| 179 | } |
Herbie Ong | 2b0bba8 | 2018-12-20 12:56:42 -0800 | [diff] [blame] | 180 | var zv interface{} |
| 181 | switch xd.Kind() { |
| 182 | case pref.EnumKind, pref.MessageKind, pref.GroupKind: |
| 183 | zv = reflect.Zero(t).Interface() |
| 184 | } |
| 185 | xt := pimpl.Export{}.ExtensionTypeOf(xd, zv) |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 186 | |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 187 | // Cache the conversion for both directions. |
| 188 | extensionDescCache.Store(xt, d) |
| 189 | extensionTypeCache.Store(dk, xt) |
| 190 | return xt |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 193 | // extensionTypeOf returns a protoreflect.ExtensionType where the GoType |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 194 | // is the underlying v1 Go type instead of the wrapper types used to present |
| 195 | // v1 Go types as if they satisfied the v2 API. |
| 196 | // |
| 197 | // This function is only valid if xd.Kind is an enum or message. |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 198 | func extensionTypeOf(xd pref.ExtensionDescriptor, t reflect.Type) pref.ExtensionType { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 199 | // Step 1: Create an ExtensionType where GoType is the wrapper type. |
| 200 | conv := pvalue.NewLegacyConverter(t, xd.Kind(), Export{}) |
| 201 | xt := ptype.GoExtension(xd, conv.EnumType, conv.MessageType) |
| 202 | |
| 203 | // Step 2: Wrap ExtensionType such that GoType presents the legacy Go type. |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 204 | xt2 := &extensionType{ExtensionType: xt} |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 205 | if xd.Cardinality() != pref.Repeated { |
| 206 | xt2.typ = t |
| 207 | xt2.new = func() interface{} { |
| 208 | return xt.New().(pvalue.Unwrapper).ProtoUnwrap() |
| 209 | } |
| 210 | xt2.valueOf = func(v interface{}) pref.Value { |
| 211 | if reflect.TypeOf(v) != xt2.typ { |
| 212 | panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ)) |
| 213 | } |
| 214 | if xd.Kind() == pref.EnumKind { |
| 215 | return xt.ValueOf(Export{}.EnumOf(v)) |
| 216 | } else { |
| 217 | return xt.ValueOf(Export{}.MessageOf(v)) |
| 218 | } |
| 219 | } |
| 220 | xt2.interfaceOf = func(v pref.Value) interface{} { |
| 221 | return xt.InterfaceOf(v).(pvalue.Unwrapper).ProtoUnwrap() |
| 222 | } |
| 223 | } else { |
| 224 | xt2.typ = reflect.PtrTo(reflect.SliceOf(t)) |
| 225 | xt2.new = func() interface{} { |
| 226 | return reflect.New(xt2.typ.Elem()).Interface() |
| 227 | } |
| 228 | xt2.valueOf = func(v interface{}) pref.Value { |
| 229 | if reflect.TypeOf(v) != xt2.typ { |
| 230 | panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ)) |
| 231 | } |
| 232 | return pref.ValueOf(pvalue.ListOf(v, conv)) |
| 233 | } |
| 234 | xt2.interfaceOf = func(pv pref.Value) interface{} { |
| 235 | v := pv.List().(pvalue.Unwrapper).ProtoUnwrap() |
| 236 | if reflect.TypeOf(v) != xt2.typ { |
| 237 | panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ)) |
| 238 | } |
| 239 | return v |
| 240 | } |
| 241 | } |
| 242 | return xt2 |
| 243 | } |
| 244 | |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 245 | type extensionType struct { |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 246 | pref.ExtensionType |
| 247 | typ reflect.Type |
| 248 | new func() interface{} |
| 249 | valueOf func(interface{}) pref.Value |
| 250 | interfaceOf func(pref.Value) interface{} |
| 251 | } |
| 252 | |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 253 | func (x *extensionType) GoType() reflect.Type { return x.typ } |
| 254 | func (x *extensionType) New() interface{} { return x.new() } |
| 255 | func (x *extensionType) ValueOf(v interface{}) pref.Value { return x.valueOf(v) } |
| 256 | func (x *extensionType) InterfaceOf(v pref.Value) interface{} { return x.interfaceOf(v) } |
Joe Tsai | b3960a7 | 2018-12-05 10:35:35 -0800 | [diff] [blame] | 257 | func (x *extensionType) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, x) } |