khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [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 | "reflect" |
| 10 | "strings" |
| 11 | "sync" |
| 12 | |
| 13 | "google.golang.org/protobuf/internal/filedesc" |
| 14 | "google.golang.org/protobuf/internal/strs" |
| 15 | "google.golang.org/protobuf/reflect/protoreflect" |
| 16 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 17 | ) |
| 18 | |
| 19 | // legacyEnumName returns the name of enums used in legacy code. |
| 20 | // It is neither the protobuf full name nor the qualified Go name, |
| 21 | // but rather an odd hybrid of both. |
| 22 | func legacyEnumName(ed pref.EnumDescriptor) string { |
| 23 | var protoPkg string |
| 24 | enumName := string(ed.FullName()) |
| 25 | if fd := ed.ParentFile(); fd != nil { |
| 26 | protoPkg = string(fd.Package()) |
| 27 | enumName = strings.TrimPrefix(enumName, protoPkg+".") |
| 28 | } |
| 29 | if protoPkg == "" { |
| 30 | return strs.GoCamelCase(enumName) |
| 31 | } |
| 32 | return protoPkg + "." + strs.GoCamelCase(enumName) |
| 33 | } |
| 34 | |
| 35 | // legacyWrapEnum wraps v as a protoreflect.Enum, |
| 36 | // where v must be a int32 kind and not implement the v2 API already. |
| 37 | func legacyWrapEnum(v reflect.Value) pref.Enum { |
| 38 | et := legacyLoadEnumType(v.Type()) |
| 39 | return et.New(pref.EnumNumber(v.Int())) |
| 40 | } |
| 41 | |
| 42 | var legacyEnumTypeCache sync.Map // map[reflect.Type]protoreflect.EnumType |
| 43 | |
| 44 | // legacyLoadEnumType dynamically loads a protoreflect.EnumType for t, |
| 45 | // where t must be an int32 kind and not implement the v2 API already. |
| 46 | func legacyLoadEnumType(t reflect.Type) pref.EnumType { |
| 47 | // Fast-path: check if a EnumType is cached for this concrete type. |
| 48 | if et, ok := legacyEnumTypeCache.Load(t); ok { |
| 49 | return et.(pref.EnumType) |
| 50 | } |
| 51 | |
| 52 | // Slow-path: derive enum descriptor and initialize EnumType. |
| 53 | var et pref.EnumType |
| 54 | ed := LegacyLoadEnumDesc(t) |
| 55 | et = &legacyEnumType{ |
| 56 | desc: ed, |
| 57 | goType: t, |
| 58 | } |
| 59 | if et, ok := legacyEnumTypeCache.LoadOrStore(t, et); ok { |
| 60 | return et.(pref.EnumType) |
| 61 | } |
| 62 | return et |
| 63 | } |
| 64 | |
| 65 | type legacyEnumType struct { |
| 66 | desc pref.EnumDescriptor |
| 67 | goType reflect.Type |
| 68 | m sync.Map // map[protoreflect.EnumNumber]proto.Enum |
| 69 | } |
| 70 | |
| 71 | func (t *legacyEnumType) New(n pref.EnumNumber) pref.Enum { |
| 72 | if e, ok := t.m.Load(n); ok { |
| 73 | return e.(pref.Enum) |
| 74 | } |
| 75 | e := &legacyEnumWrapper{num: n, pbTyp: t, goTyp: t.goType} |
| 76 | t.m.Store(n, e) |
| 77 | return e |
| 78 | } |
| 79 | func (t *legacyEnumType) Descriptor() pref.EnumDescriptor { |
| 80 | return t.desc |
| 81 | } |
| 82 | |
| 83 | type legacyEnumWrapper struct { |
| 84 | num pref.EnumNumber |
| 85 | pbTyp pref.EnumType |
| 86 | goTyp reflect.Type |
| 87 | } |
| 88 | |
| 89 | func (e *legacyEnumWrapper) Descriptor() pref.EnumDescriptor { |
| 90 | return e.pbTyp.Descriptor() |
| 91 | } |
| 92 | func (e *legacyEnumWrapper) Type() pref.EnumType { |
| 93 | return e.pbTyp |
| 94 | } |
| 95 | func (e *legacyEnumWrapper) Number() pref.EnumNumber { |
| 96 | return e.num |
| 97 | } |
| 98 | func (e *legacyEnumWrapper) ProtoReflect() pref.Enum { |
| 99 | return e |
| 100 | } |
| 101 | func (e *legacyEnumWrapper) protoUnwrap() interface{} { |
| 102 | v := reflect.New(e.goTyp).Elem() |
| 103 | v.SetInt(int64(e.num)) |
| 104 | return v.Interface() |
| 105 | } |
| 106 | |
| 107 | var ( |
| 108 | _ pref.Enum = (*legacyEnumWrapper)(nil) |
| 109 | _ unwrapper = (*legacyEnumWrapper)(nil) |
| 110 | ) |
| 111 | |
| 112 | var legacyEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor |
| 113 | |
| 114 | // LegacyLoadEnumDesc returns an EnumDescriptor derived from the Go type, |
| 115 | // which must be an int32 kind and not implement the v2 API already. |
| 116 | // |
| 117 | // This is exported for testing purposes. |
| 118 | func LegacyLoadEnumDesc(t reflect.Type) pref.EnumDescriptor { |
| 119 | // Fast-path: check if an EnumDescriptor is cached for this concrete type. |
| 120 | if ed, ok := legacyEnumDescCache.Load(t); ok { |
| 121 | return ed.(pref.EnumDescriptor) |
| 122 | } |
| 123 | |
| 124 | // Slow-path: initialize EnumDescriptor from the raw descriptor. |
| 125 | ev := reflect.Zero(t).Interface() |
| 126 | if _, ok := ev.(pref.Enum); ok { |
| 127 | panic(fmt.Sprintf("%v already implements proto.Enum", t)) |
| 128 | } |
| 129 | edV1, ok := ev.(enumV1) |
| 130 | if !ok { |
| 131 | return aberrantLoadEnumDesc(t) |
| 132 | } |
| 133 | b, idxs := edV1.EnumDescriptor() |
| 134 | |
| 135 | var ed pref.EnumDescriptor |
| 136 | if len(idxs) == 1 { |
| 137 | ed = legacyLoadFileDesc(b).Enums().Get(idxs[0]) |
| 138 | } else { |
| 139 | md := legacyLoadFileDesc(b).Messages().Get(idxs[0]) |
| 140 | for _, i := range idxs[1 : len(idxs)-1] { |
| 141 | md = md.Messages().Get(i) |
| 142 | } |
| 143 | ed = md.Enums().Get(idxs[len(idxs)-1]) |
| 144 | } |
| 145 | if ed, ok := legacyEnumDescCache.LoadOrStore(t, ed); ok { |
| 146 | return ed.(protoreflect.EnumDescriptor) |
| 147 | } |
| 148 | return ed |
| 149 | } |
| 150 | |
| 151 | var aberrantEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor |
| 152 | |
| 153 | // aberrantLoadEnumDesc returns an EnumDescriptor derived from the Go type, |
| 154 | // which must not implement protoreflect.Enum or enumV1. |
| 155 | // |
| 156 | // If the type does not implement enumV1, then there is no reliable |
| 157 | // way to derive the original protobuf type information. |
| 158 | // We are unable to use the global enum registry since it is |
| 159 | // unfortunately keyed by the protobuf full name, which we also do not know. |
| 160 | // Thus, this produces some bogus enum descriptor based on the Go type name. |
| 161 | func aberrantLoadEnumDesc(t reflect.Type) pref.EnumDescriptor { |
| 162 | // Fast-path: check if an EnumDescriptor is cached for this concrete type. |
| 163 | if ed, ok := aberrantEnumDescCache.Load(t); ok { |
| 164 | return ed.(pref.EnumDescriptor) |
| 165 | } |
| 166 | |
| 167 | // Slow-path: construct a bogus, but unique EnumDescriptor. |
| 168 | ed := &filedesc.Enum{L2: new(filedesc.EnumL2)} |
| 169 | ed.L0.FullName = AberrantDeriveFullName(t) // e.g., github_com.user.repo.MyEnum |
| 170 | ed.L0.ParentFile = filedesc.SurrogateProto3 |
| 171 | ed.L2.Values.List = append(ed.L2.Values.List, filedesc.EnumValue{}) |
| 172 | |
| 173 | // TODO: Use the presence of a UnmarshalJSON method to determine proto2? |
| 174 | |
| 175 | vd := &ed.L2.Values.List[0] |
| 176 | vd.L0.FullName = ed.L0.FullName + "_UNKNOWN" // e.g., github_com.user.repo.MyEnum_UNKNOWN |
| 177 | vd.L0.ParentFile = ed.L0.ParentFile |
| 178 | vd.L0.Parent = ed |
| 179 | |
| 180 | // TODO: We could use the String method to obtain some enum value names by |
| 181 | // starting at 0 and print the enum until it produces invalid identifiers. |
| 182 | // An exhaustive query is clearly impractical, but can be best-effort. |
| 183 | |
| 184 | if ed, ok := aberrantEnumDescCache.LoadOrStore(t, ed); ok { |
| 185 | return ed.(pref.EnumDescriptor) |
| 186 | } |
| 187 | return ed |
| 188 | } |
| 189 | |
| 190 | // AberrantDeriveFullName derives a fully qualified protobuf name for the given Go type |
| 191 | // The provided name is not guaranteed to be stable nor universally unique. |
| 192 | // It should be sufficiently unique within a program. |
| 193 | // |
| 194 | // This is exported for testing purposes. |
| 195 | func AberrantDeriveFullName(t reflect.Type) pref.FullName { |
| 196 | sanitize := func(r rune) rune { |
| 197 | switch { |
| 198 | case r == '/': |
| 199 | return '.' |
| 200 | case 'a' <= r && r <= 'z', 'A' <= r && r <= 'Z', '0' <= r && r <= '9': |
| 201 | return r |
| 202 | default: |
| 203 | return '_' |
| 204 | } |
| 205 | } |
| 206 | prefix := strings.Map(sanitize, t.PkgPath()) |
| 207 | suffix := strings.Map(sanitize, t.Name()) |
| 208 | if suffix == "" { |
| 209 | suffix = fmt.Sprintf("UnknownX%X", reflect.ValueOf(t).Pointer()) |
| 210 | } |
| 211 | |
| 212 | ss := append(strings.Split(prefix, "."), suffix) |
| 213 | for i, s := range ss { |
| 214 | if s == "" || ('0' <= s[0] && s[0] <= '9') { |
| 215 | ss[i] = "x" + s |
| 216 | } |
| 217 | } |
| 218 | return pref.FullName(strings.Join(ss, ".")) |
| 219 | } |