Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [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 impl |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "math" |
| 10 | "reflect" |
| 11 | "sync" |
| 12 | |
| 13 | descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor" |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 14 | pvalue "github.com/golang/protobuf/v2/internal/value" |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 15 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
| 16 | ptype "github.com/golang/protobuf/v2/reflect/prototype" |
| 17 | ) |
| 18 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame^] | 19 | // legacyWrapEnum wraps v as a protoreflect.ProtoEnum, |
| 20 | // where v must be a *struct kind and not implement the v2 API already. |
| 21 | func legacyWrapEnum(v reflect.Value) pref.ProtoEnum { |
| 22 | et := legacyLoadEnumType(v.Type()) |
| 23 | return et.New(pref.EnumNumber(v.Int())) |
| 24 | } |
| 25 | |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 26 | var enumTypeCache sync.Map // map[reflect.Type]protoreflect.EnumType |
| 27 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame^] | 28 | // legacyLoadEnumType dynamically loads a protoreflect.EnumType for t, |
| 29 | // where t must be an int32 kind and not implement the v2 API already. |
| 30 | func legacyLoadEnumType(t reflect.Type) pref.EnumType { |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 31 | // Fast-path: check if a EnumType is cached for this concrete type. |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame^] | 32 | if et, ok := enumTypeCache.Load(t); ok { |
| 33 | return et.(pref.EnumType) |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // Slow-path: derive enum descriptor and initialize EnumType. |
| 37 | var m sync.Map // map[protoreflect.EnumNumber]proto.Enum |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame^] | 38 | ed := legacyLoadEnumDesc(t) |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 39 | et := ptype.GoEnum(ed, func(et pref.EnumType, n pref.EnumNumber) pref.ProtoEnum { |
| 40 | if e, ok := m.Load(n); ok { |
| 41 | return e.(pref.ProtoEnum) |
| 42 | } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame^] | 43 | e := &legacyEnumWrapper{num: n, pbTyp: et, goTyp: t} |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 44 | m.Store(n, e) |
| 45 | return e |
| 46 | }) |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame^] | 47 | enumTypeCache.Store(t, et) |
| 48 | return et.(pref.EnumType) |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | type legacyEnumWrapper struct { |
| 52 | num pref.EnumNumber |
| 53 | pbTyp pref.EnumType |
| 54 | goTyp reflect.Type |
| 55 | } |
| 56 | |
| 57 | func (e *legacyEnumWrapper) Number() pref.EnumNumber { |
| 58 | return e.num |
| 59 | } |
| 60 | func (e *legacyEnumWrapper) Type() pref.EnumType { |
| 61 | return e.pbTyp |
| 62 | } |
| 63 | func (e *legacyEnumWrapper) ProtoReflect() pref.Enum { |
| 64 | return e |
| 65 | } |
| 66 | func (e *legacyEnumWrapper) Unwrap() interface{} { |
| 67 | v := reflect.New(e.goTyp).Elem() |
| 68 | v.SetInt(int64(e.num)) |
| 69 | return v.Interface() |
| 70 | } |
| 71 | |
| 72 | var ( |
| 73 | _ pref.Enum = (*legacyEnumWrapper)(nil) |
| 74 | _ pref.ProtoEnum = (*legacyEnumWrapper)(nil) |
| 75 | _ pvalue.Unwrapper = (*legacyEnumWrapper)(nil) |
| 76 | ) |
| 77 | |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 78 | var enumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor |
| 79 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame^] | 80 | var enumNumberType = reflect.TypeOf(pref.EnumNumber(0)) |
| 81 | |
| 82 | // legacyLoadEnumDesc returns an EnumDescriptor derived from the Go type, |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 83 | // which must be an int32 kind and not implement the v2 API already. |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame^] | 84 | func legacyLoadEnumDesc(t reflect.Type) pref.EnumDescriptor { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 85 | // Fast-path: check if an EnumDescriptor is cached for this concrete type. |
| 86 | if v, ok := enumDescCache.Load(t); ok { |
| 87 | return v.(pref.EnumDescriptor) |
| 88 | } |
| 89 | |
| 90 | // Slow-path: initialize EnumDescriptor from the proto descriptor. |
Joe Tsai | 6f9095c | 2018-11-10 14:12:21 -0800 | [diff] [blame] | 91 | if t.Kind() != reflect.Int32 || t.PkgPath() == "" { |
| 92 | panic(fmt.Sprintf("got %v, want named int32 kind", t)) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 93 | } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame^] | 94 | if t == enumNumberType { |
| 95 | panic(fmt.Sprintf("cannot be %v", t)) |
| 96 | } |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 97 | |
| 98 | // Derive the enum descriptor from the raw descriptor proto. |
| 99 | e := new(ptype.StandaloneEnum) |
| 100 | ev := reflect.Zero(t).Interface() |
| 101 | if _, ok := ev.(pref.ProtoEnum); ok { |
| 102 | panic(fmt.Sprintf("%v already implements proto.Enum", t)) |
| 103 | } |
| 104 | if ed, ok := ev.(legacyEnum); ok { |
| 105 | b, idxs := ed.EnumDescriptor() |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame^] | 106 | fd := legacyLoadFileDesc(b) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 107 | |
| 108 | // Derive syntax. |
| 109 | switch fd.GetSyntax() { |
| 110 | case "proto2", "": |
| 111 | e.Syntax = pref.Proto2 |
| 112 | case "proto3": |
| 113 | e.Syntax = pref.Proto3 |
| 114 | } |
| 115 | |
| 116 | // Derive the full name and correct enum descriptor. |
| 117 | var ed *descriptorV1.EnumDescriptorProto |
| 118 | e.FullName = pref.FullName(fd.GetPackage()) |
| 119 | if len(idxs) == 1 { |
| 120 | ed = fd.EnumType[idxs[0]] |
| 121 | e.FullName = e.FullName.Append(pref.Name(ed.GetName())) |
| 122 | } else { |
| 123 | md := fd.MessageType[idxs[0]] |
| 124 | e.FullName = e.FullName.Append(pref.Name(md.GetName())) |
| 125 | for _, i := range idxs[1 : len(idxs)-1] { |
| 126 | md = md.NestedType[i] |
| 127 | e.FullName = e.FullName.Append(pref.Name(md.GetName())) |
| 128 | } |
| 129 | ed = md.EnumType[idxs[len(idxs)-1]] |
| 130 | e.FullName = e.FullName.Append(pref.Name(ed.GetName())) |
| 131 | } |
| 132 | |
| 133 | // Derive the enum values. |
| 134 | for _, vd := range ed.GetValue() { |
| 135 | e.Values = append(e.Values, ptype.EnumValue{ |
| 136 | Name: pref.Name(vd.GetName()), |
| 137 | Number: pref.EnumNumber(vd.GetNumber()), |
| 138 | }) |
| 139 | } |
| 140 | } else { |
| 141 | // If the type does not implement legacyEnum, then there is no reliable |
| 142 | // way to derive the original protobuf type information. |
| 143 | // We are unable to use the global enum registry since it is |
| 144 | // unfortunately keyed by the full name, which we do not know. |
| 145 | // Furthermore, some generated enums register with a fork of |
| 146 | // golang/protobuf so the enum may not even be found in the registry. |
| 147 | // |
| 148 | // Instead, create a bogus enum descriptor to ensure that |
| 149 | // most operations continue to work. For example, textpb and jsonpb |
| 150 | // will be unable to parse a message with an enum value by name. |
| 151 | e.Syntax = pref.Proto2 |
| 152 | e.FullName = deriveFullName(t) |
| 153 | e.Values = []ptype.EnumValue{{Name: "INVALID", Number: math.MinInt32}} |
| 154 | } |
| 155 | |
| 156 | ed, err := ptype.NewEnum(e) |
| 157 | if err != nil { |
| 158 | panic(err) |
| 159 | } |
| 160 | enumDescCache.Store(t, ed) |
| 161 | return ed |
| 162 | } |