blob: 142a90a9ba3e614ad3d1f58bcf9b66d6f6c4209b [file] [log] [blame]
Joe Tsai90fe9962018-10-18 11:06:29 -07001// 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 Tsai21ade492019-05-22 13:42:54 -04005package impl
Joe Tsai90fe9962018-10-18 11:06:29 -07006
7import (
8 "fmt"
Joe Tsai90fe9962018-10-18 11:06:29 -07009 "reflect"
Joe Tsai851185d2019-07-01 13:45:52 -070010 "strings"
Joe Tsai90fe9962018-10-18 11:06:29 -070011 "sync"
12
Joe Tsai851185d2019-07-01 13:45:52 -070013 "google.golang.org/protobuf/internal/filedesc"
Joe Tsaid8881392019-06-06 13:01:53 -070014 "google.golang.org/protobuf/reflect/protoreflect"
Damien Neile89e6242019-05-13 23:55:40 -070015 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaib2f66be2019-05-22 00:42:45 -040016 "google.golang.org/protobuf/reflect/prototype"
Joe Tsai90fe9962018-10-18 11:06:29 -070017)
18
Joe Tsai21ade492019-05-22 13:42:54 -040019// legacyWrapEnum wraps v as a protoreflect.Enum,
Joe Tsai08e00302018-11-26 22:32:06 -080020// where v must be a int32 kind and not implement the v2 API already.
Joe Tsai21ade492019-05-22 13:42:54 -040021func legacyWrapEnum(v reflect.Value) pref.Enum {
22 et := legacyLoadEnumType(v.Type())
Joe Tsaif0c01e42018-11-06 13:05:20 -080023 return et.New(pref.EnumNumber(v.Int()))
24}
25
Joe Tsai21ade492019-05-22 13:42:54 -040026var legacyEnumTypeCache sync.Map // map[reflect.Type]protoreflect.EnumType
Joe Tsai6f9095c2018-11-10 14:12:21 -080027
Joe Tsai21ade492019-05-22 13:42:54 -040028// legacyLoadEnumType dynamically loads a protoreflect.EnumType for t,
Joe Tsaif0c01e42018-11-06 13:05:20 -080029// where t must be an int32 kind and not implement the v2 API already.
Joe Tsai21ade492019-05-22 13:42:54 -040030func legacyLoadEnumType(t reflect.Type) pref.EnumType {
Joe Tsai6f9095c2018-11-10 14:12:21 -080031 // Fast-path: check if a EnumType is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040032 if et, ok := legacyEnumTypeCache.Load(t); ok {
Joe Tsaif0c01e42018-11-06 13:05:20 -080033 return et.(pref.EnumType)
Joe Tsai6f9095c2018-11-10 14:12:21 -080034 }
35
36 // Slow-path: derive enum descriptor and initialize EnumType.
Joe Tsaib2f66be2019-05-22 00:42:45 -040037 var et pref.EnumType
Joe Tsai6f9095c2018-11-10 14:12:21 -080038 var m sync.Map // map[protoreflect.EnumNumber]proto.Enum
Joe Tsai21ade492019-05-22 13:42:54 -040039 ed := LegacyLoadEnumDesc(t)
Joe Tsaib2f66be2019-05-22 00:42:45 -040040 et = &prototype.Enum{
41 EnumDescriptor: ed,
42 NewEnum: func(n pref.EnumNumber) pref.Enum {
43 if e, ok := m.Load(n); ok {
44 return e.(pref.Enum)
45 }
Joe Tsai21ade492019-05-22 13:42:54 -040046 e := &legacyEnumWrapper{num: n, pbTyp: et, goTyp: t}
Joe Tsaib2f66be2019-05-22 00:42:45 -040047 m.Store(n, e)
48 return e
49 },
50 }
Joe Tsai21ade492019-05-22 13:42:54 -040051 if et, ok := legacyEnumTypeCache.LoadOrStore(t, et); ok {
Joe Tsaib9365042019-03-19 14:14:29 -070052 return et.(pref.EnumType)
53 }
54 return et
Joe Tsai6f9095c2018-11-10 14:12:21 -080055}
56
Joe Tsai21ade492019-05-22 13:42:54 -040057type legacyEnumWrapper struct {
Joe Tsai6f9095c2018-11-10 14:12:21 -080058 num pref.EnumNumber
59 pbTyp pref.EnumType
60 goTyp reflect.Type
61}
62
Joe Tsai21ade492019-05-22 13:42:54 -040063func (e *legacyEnumWrapper) Descriptor() pref.EnumDescriptor {
Joe Tsai0fc49f82019-05-01 12:29:25 -070064 return e.pbTyp.Descriptor()
65}
Joe Tsaid4211502019-07-02 14:58:02 -070066func (e *legacyEnumWrapper) Type() pref.EnumType {
67 return e.pbTyp
68}
Joe Tsai21ade492019-05-22 13:42:54 -040069func (e *legacyEnumWrapper) Number() pref.EnumNumber {
Joe Tsai0fc49f82019-05-01 12:29:25 -070070 return e.num
71}
Joe Tsai21ade492019-05-22 13:42:54 -040072func (e *legacyEnumWrapper) ProtoReflect() pref.Enum {
Joe Tsai6f9095c2018-11-10 14:12:21 -080073 return e
74}
Joe Tsai21ade492019-05-22 13:42:54 -040075func (e *legacyEnumWrapper) ProtoUnwrap() interface{} {
Joe Tsai6f9095c2018-11-10 14:12:21 -080076 v := reflect.New(e.goTyp).Elem()
77 v.SetInt(int64(e.num))
78 return v.Interface()
79}
80
81var (
Damien Neil954bd922019-07-17 16:52:10 -070082 _ pref.Enum = (*legacyEnumWrapper)(nil)
83 _ Unwrapper = (*legacyEnumWrapper)(nil)
Joe Tsai6f9095c2018-11-10 14:12:21 -080084)
85
Joe Tsai21ade492019-05-22 13:42:54 -040086var legacyEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
Joe Tsai90fe9962018-10-18 11:06:29 -070087
Joe Tsai21ade492019-05-22 13:42:54 -040088var legacyEnumNumberType = reflect.TypeOf(pref.EnumNumber(0))
Joe Tsaif0c01e42018-11-06 13:05:20 -080089
Joe Tsai21ade492019-05-22 13:42:54 -040090// LegacyLoadEnumDesc returns an EnumDescriptor derived from the Go type,
Joe Tsai90fe9962018-10-18 11:06:29 -070091// which must be an int32 kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -070092//
93// This is exported for testing purposes.
Joe Tsai21ade492019-05-22 13:42:54 -040094func LegacyLoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070095 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040096 if ed, ok := legacyEnumDescCache.Load(t); ok {
Joe Tsaib9365042019-03-19 14:14:29 -070097 return ed.(pref.EnumDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -070098 }
99
Joe Tsaid8881392019-06-06 13:01:53 -0700100 // Slow-path: initialize EnumDescriptor from the raw descriptor.
Joe Tsai90fe9962018-10-18 11:06:29 -0700101 ev := reflect.Zero(t).Interface()
Damien Neila8593ba2019-01-08 16:18:07 -0800102 if _, ok := ev.(pref.Enum); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700103 panic(fmt.Sprintf("%v already implements proto.Enum", t))
104 }
Joe Tsaid8881392019-06-06 13:01:53 -0700105 edV1, ok := ev.(enumV1)
106 if !ok {
Joe Tsai851185d2019-07-01 13:45:52 -0700107 return aberrantLoadEnumDesc(t)
Joe Tsai90fe9962018-10-18 11:06:29 -0700108 }
Joe Tsaid8881392019-06-06 13:01:53 -0700109 b, idxs := edV1.EnumDescriptor()
Joe Tsai90fe9962018-10-18 11:06:29 -0700110
Joe Tsaid8881392019-06-06 13:01:53 -0700111 var ed pref.EnumDescriptor
112 if len(idxs) == 1 {
113 ed = legacyLoadFileDesc(b).Enums().Get(idxs[0])
114 } else {
115 md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
116 for _, i := range idxs[1 : len(idxs)-1] {
117 md = md.Messages().Get(i)
118 }
119 ed = md.Enums().Get(idxs[len(idxs)-1])
Joe Tsai90fe9962018-10-18 11:06:29 -0700120 }
Joe Tsai21ade492019-05-22 13:42:54 -0400121 if ed, ok := legacyEnumDescCache.LoadOrStore(t, ed); ok {
Joe Tsaid8881392019-06-06 13:01:53 -0700122 return ed.(protoreflect.EnumDescriptor)
Joe Tsaib9365042019-03-19 14:14:29 -0700123 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700124 return ed
125}
Joe Tsai851185d2019-07-01 13:45:52 -0700126
127var aberrantEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
128
129// aberrantLoadEnumDesc returns an EnumDescriptor derived from the Go type,
130// which must not implement protoreflect.Enum or enumV1.
131//
132// If the type does not implement enumV1, then there is no reliable
133// way to derive the original protobuf type information.
134// We are unable to use the global enum registry since it is
135// unfortunately keyed by the protobuf full name, which we also do not know.
136// Thus, this produces some bogus enum descriptor based on the Go type name.
137func aberrantLoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
138 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
139 if ed, ok := aberrantEnumDescCache.Load(t); ok {
140 return ed.(pref.EnumDescriptor)
141 }
142
143 // Slow-path: construct a bogus, but unique EnumDescriptor.
144 ed := &filedesc.Enum{L2: new(filedesc.EnumL2)}
145 ed.L0.FullName = aberrantDeriveFullName(t) // e.g., github_com.user.repo.MyEnum
146 ed.L0.ParentFile = filedesc.SurrogateProto3
147 ed.L2.Values.List = append(ed.L2.Values.List, filedesc.EnumValue{})
148
149 // TODO: Use the presence of a UnmarshalJSON method to determine proto2?
150
151 vd := &ed.L2.Values.List[0]
152 vd.L0.FullName = ed.L0.FullName + "_UNKNOWN" // e.g., github_com.user.repo.MyEnum_UNKNOWN
153 vd.L0.ParentFile = ed.L0.ParentFile
154 vd.L0.Parent = ed
155
156 // TODO: We could use the String method to obtain some enum value names by
157 // starting at 0 and print the enum until it produces invalid identifiers.
158 // An exhaustive query is clearly impractical, but can be best-effort.
159
160 if ed, ok := aberrantEnumDescCache.LoadOrStore(t, ed); ok {
161 return ed.(pref.EnumDescriptor)
162 }
163 return ed
164}
165
166// aberrantDeriveFullName derives a fully qualified protobuf name for the given Go type
167// The provided name is not guaranteed to be stable nor universally unique.
168// It should be sufficiently unique within a program.
169func aberrantDeriveFullName(t reflect.Type) pref.FullName {
170 sanitize := func(r rune) rune {
171 switch {
172 case r == '/':
173 return '.'
174 case 'a' <= r && r <= 'z', 'A' <= r && r <= 'Z', '0' <= r && r <= '9':
175 return r
176 default:
177 return '_'
178 }
179 }
180 prefix := strings.Map(sanitize, t.PkgPath())
181 suffix := strings.Map(sanitize, t.Name())
182 if suffix == "" {
183 suffix = fmt.Sprintf("UnknownX%X", reflect.ValueOf(t).Pointer())
184 }
185
186 ss := append(strings.Split(prefix, "."), suffix)
187 for i, s := range ss {
188 if s == "" || ('0' <= s[0] && s[0] <= '9') {
189 ss[i] = "x" + s
190 }
191 }
192 return pref.FullName(strings.Join(ss, "."))
193}