blob: 279baa9fa9fd0f5b280ac6de38f9c9c397ffff4e [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 Tsai90fe9962018-10-18 11:06:29 -070016)
17
Joe Tsai21ade492019-05-22 13:42:54 -040018// legacyWrapEnum wraps v as a protoreflect.Enum,
Joe Tsai08e00302018-11-26 22:32:06 -080019// where v must be a int32 kind and not implement the v2 API already.
Joe Tsai21ade492019-05-22 13:42:54 -040020func legacyWrapEnum(v reflect.Value) pref.Enum {
21 et := legacyLoadEnumType(v.Type())
Joe Tsaif0c01e42018-11-06 13:05:20 -080022 return et.New(pref.EnumNumber(v.Int()))
23}
24
Joe Tsai21ade492019-05-22 13:42:54 -040025var legacyEnumTypeCache sync.Map // map[reflect.Type]protoreflect.EnumType
Joe Tsai6f9095c2018-11-10 14:12:21 -080026
Joe Tsai21ade492019-05-22 13:42:54 -040027// legacyLoadEnumType dynamically loads a protoreflect.EnumType for t,
Joe Tsaif0c01e42018-11-06 13:05:20 -080028// where t must be an int32 kind and not implement the v2 API already.
Joe Tsai21ade492019-05-22 13:42:54 -040029func legacyLoadEnumType(t reflect.Type) pref.EnumType {
Joe Tsai6f9095c2018-11-10 14:12:21 -080030 // Fast-path: check if a EnumType is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040031 if et, ok := legacyEnumTypeCache.Load(t); ok {
Joe Tsaif0c01e42018-11-06 13:05:20 -080032 return et.(pref.EnumType)
Joe Tsai6f9095c2018-11-10 14:12:21 -080033 }
34
35 // Slow-path: derive enum descriptor and initialize EnumType.
Joe Tsaib2f66be2019-05-22 00:42:45 -040036 var et pref.EnumType
Joe Tsai21ade492019-05-22 13:42:54 -040037 ed := LegacyLoadEnumDesc(t)
Damien Neil16163b42019-08-06 15:43:25 -070038 et = &legacyEnumType{
39 desc: ed,
40 goType: t,
Joe Tsaib2f66be2019-05-22 00:42:45 -040041 }
Joe Tsai21ade492019-05-22 13:42:54 -040042 if et, ok := legacyEnumTypeCache.LoadOrStore(t, et); ok {
Joe Tsaib9365042019-03-19 14:14:29 -070043 return et.(pref.EnumType)
44 }
45 return et
Joe Tsai6f9095c2018-11-10 14:12:21 -080046}
47
Damien Neil16163b42019-08-06 15:43:25 -070048type legacyEnumType struct {
49 desc pref.EnumDescriptor
50 goType reflect.Type
51 m sync.Map // map[protoreflect.EnumNumber]proto.Enum
52}
53
54func (t *legacyEnumType) New(n pref.EnumNumber) pref.Enum {
55 if e, ok := t.m.Load(n); ok {
56 return e.(pref.Enum)
57 }
58 e := &legacyEnumWrapper{num: n, pbTyp: t, goTyp: t.goType}
59 t.m.Store(n, e)
60 return e
61}
62func (t *legacyEnumType) GoType() reflect.Type {
63 return t.goType
64}
65func (t *legacyEnumType) Descriptor() pref.EnumDescriptor {
66 return t.desc
67}
68
Joe Tsai21ade492019-05-22 13:42:54 -040069type legacyEnumWrapper struct {
Joe Tsai6f9095c2018-11-10 14:12:21 -080070 num pref.EnumNumber
71 pbTyp pref.EnumType
72 goTyp reflect.Type
73}
74
Joe Tsai21ade492019-05-22 13:42:54 -040075func (e *legacyEnumWrapper) Descriptor() pref.EnumDescriptor {
Joe Tsai0fc49f82019-05-01 12:29:25 -070076 return e.pbTyp.Descriptor()
77}
Joe Tsaid4211502019-07-02 14:58:02 -070078func (e *legacyEnumWrapper) Type() pref.EnumType {
79 return e.pbTyp
80}
Joe Tsai21ade492019-05-22 13:42:54 -040081func (e *legacyEnumWrapper) Number() pref.EnumNumber {
Joe Tsai0fc49f82019-05-01 12:29:25 -070082 return e.num
83}
Joe Tsai21ade492019-05-22 13:42:54 -040084func (e *legacyEnumWrapper) ProtoReflect() pref.Enum {
Joe Tsai6f9095c2018-11-10 14:12:21 -080085 return e
86}
Joe Tsai21ade492019-05-22 13:42:54 -040087func (e *legacyEnumWrapper) ProtoUnwrap() interface{} {
Joe Tsai6f9095c2018-11-10 14:12:21 -080088 v := reflect.New(e.goTyp).Elem()
89 v.SetInt(int64(e.num))
90 return v.Interface()
91}
92
93var (
Damien Neil954bd922019-07-17 16:52:10 -070094 _ pref.Enum = (*legacyEnumWrapper)(nil)
95 _ Unwrapper = (*legacyEnumWrapper)(nil)
Joe Tsai6f9095c2018-11-10 14:12:21 -080096)
97
Joe Tsai21ade492019-05-22 13:42:54 -040098var legacyEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
Joe Tsai90fe9962018-10-18 11:06:29 -070099
Joe Tsai21ade492019-05-22 13:42:54 -0400100// LegacyLoadEnumDesc returns an EnumDescriptor derived from the Go type,
Joe Tsai90fe9962018-10-18 11:06:29 -0700101// which must be an int32 kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -0700102//
103// This is exported for testing purposes.
Joe Tsai21ade492019-05-22 13:42:54 -0400104func LegacyLoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -0700105 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -0400106 if ed, ok := legacyEnumDescCache.Load(t); ok {
Joe Tsaib9365042019-03-19 14:14:29 -0700107 return ed.(pref.EnumDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -0700108 }
109
Joe Tsaid8881392019-06-06 13:01:53 -0700110 // Slow-path: initialize EnumDescriptor from the raw descriptor.
Joe Tsai90fe9962018-10-18 11:06:29 -0700111 ev := reflect.Zero(t).Interface()
Damien Neila8593ba2019-01-08 16:18:07 -0800112 if _, ok := ev.(pref.Enum); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700113 panic(fmt.Sprintf("%v already implements proto.Enum", t))
114 }
Joe Tsaid8881392019-06-06 13:01:53 -0700115 edV1, ok := ev.(enumV1)
116 if !ok {
Joe Tsai851185d2019-07-01 13:45:52 -0700117 return aberrantLoadEnumDesc(t)
Joe Tsai90fe9962018-10-18 11:06:29 -0700118 }
Joe Tsaid8881392019-06-06 13:01:53 -0700119 b, idxs := edV1.EnumDescriptor()
Joe Tsai90fe9962018-10-18 11:06:29 -0700120
Joe Tsaid8881392019-06-06 13:01:53 -0700121 var ed pref.EnumDescriptor
122 if len(idxs) == 1 {
123 ed = legacyLoadFileDesc(b).Enums().Get(idxs[0])
124 } else {
125 md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
126 for _, i := range idxs[1 : len(idxs)-1] {
127 md = md.Messages().Get(i)
128 }
129 ed = md.Enums().Get(idxs[len(idxs)-1])
Joe Tsai90fe9962018-10-18 11:06:29 -0700130 }
Joe Tsai21ade492019-05-22 13:42:54 -0400131 if ed, ok := legacyEnumDescCache.LoadOrStore(t, ed); ok {
Joe Tsaid8881392019-06-06 13:01:53 -0700132 return ed.(protoreflect.EnumDescriptor)
Joe Tsaib9365042019-03-19 14:14:29 -0700133 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700134 return ed
135}
Joe Tsai851185d2019-07-01 13:45:52 -0700136
137var aberrantEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
138
139// aberrantLoadEnumDesc returns an EnumDescriptor derived from the Go type,
140// which must not implement protoreflect.Enum or enumV1.
141//
142// If the type does not implement enumV1, then there is no reliable
143// way to derive the original protobuf type information.
144// We are unable to use the global enum registry since it is
145// unfortunately keyed by the protobuf full name, which we also do not know.
146// Thus, this produces some bogus enum descriptor based on the Go type name.
147func aberrantLoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
148 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
149 if ed, ok := aberrantEnumDescCache.Load(t); ok {
150 return ed.(pref.EnumDescriptor)
151 }
152
153 // Slow-path: construct a bogus, but unique EnumDescriptor.
154 ed := &filedesc.Enum{L2: new(filedesc.EnumL2)}
155 ed.L0.FullName = aberrantDeriveFullName(t) // e.g., github_com.user.repo.MyEnum
156 ed.L0.ParentFile = filedesc.SurrogateProto3
157 ed.L2.Values.List = append(ed.L2.Values.List, filedesc.EnumValue{})
158
159 // TODO: Use the presence of a UnmarshalJSON method to determine proto2?
160
161 vd := &ed.L2.Values.List[0]
162 vd.L0.FullName = ed.L0.FullName + "_UNKNOWN" // e.g., github_com.user.repo.MyEnum_UNKNOWN
163 vd.L0.ParentFile = ed.L0.ParentFile
164 vd.L0.Parent = ed
165
166 // TODO: We could use the String method to obtain some enum value names by
167 // starting at 0 and print the enum until it produces invalid identifiers.
168 // An exhaustive query is clearly impractical, but can be best-effort.
169
170 if ed, ok := aberrantEnumDescCache.LoadOrStore(t, ed); ok {
171 return ed.(pref.EnumDescriptor)
172 }
173 return ed
174}
175
176// aberrantDeriveFullName derives a fully qualified protobuf name for the given Go type
177// The provided name is not guaranteed to be stable nor universally unique.
178// It should be sufficiently unique within a program.
179func aberrantDeriveFullName(t reflect.Type) pref.FullName {
180 sanitize := func(r rune) rune {
181 switch {
182 case r == '/':
183 return '.'
184 case 'a' <= r && r <= 'z', 'A' <= r && r <= 'Z', '0' <= r && r <= '9':
185 return r
186 default:
187 return '_'
188 }
189 }
190 prefix := strings.Map(sanitize, t.PkgPath())
191 suffix := strings.Map(sanitize, t.Name())
192 if suffix == "" {
193 suffix = fmt.Sprintf("UnknownX%X", reflect.ValueOf(t).Pointer())
194 }
195
196 ss := append(strings.Split(prefix, "."), suffix)
197 for i, s := range ss {
198 if s == "" || ('0' <= s[0] && s[0] <= '9') {
199 ss[i] = "x" + s
200 }
201 }
202 return pref.FullName(strings.Join(ss, "."))
203}