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