blob: e9603e460a3d8984a740d3d3d8a871f08c2be18a [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"
Damien Neile89e6242019-05-13 23:55:40 -070014 pvalue "google.golang.org/protobuf/internal/value"
Joe Tsaid8881392019-06-06 13:01:53 -070015 "google.golang.org/protobuf/reflect/protoreflect"
Damien Neile89e6242019-05-13 23:55:40 -070016 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaib2f66be2019-05-22 00:42:45 -040017 "google.golang.org/protobuf/reflect/prototype"
Joe Tsai90fe9962018-10-18 11:06:29 -070018)
19
Joe Tsai21ade492019-05-22 13:42:54 -040020// legacyWrapEnum wraps v as a protoreflect.Enum,
Joe Tsai08e00302018-11-26 22:32:06 -080021// where v must be a int32 kind and not implement the v2 API already.
Joe Tsai21ade492019-05-22 13:42:54 -040022func legacyWrapEnum(v reflect.Value) pref.Enum {
23 et := legacyLoadEnumType(v.Type())
Joe Tsaif0c01e42018-11-06 13:05:20 -080024 return et.New(pref.EnumNumber(v.Int()))
25}
26
Joe Tsai21ade492019-05-22 13:42:54 -040027var legacyEnumTypeCache sync.Map // map[reflect.Type]protoreflect.EnumType
Joe Tsai6f9095c2018-11-10 14:12:21 -080028
Joe Tsai21ade492019-05-22 13:42:54 -040029// legacyLoadEnumType dynamically loads a protoreflect.EnumType for t,
Joe Tsaif0c01e42018-11-06 13:05:20 -080030// where t must be an int32 kind and not implement the v2 API already.
Joe Tsai21ade492019-05-22 13:42:54 -040031func legacyLoadEnumType(t reflect.Type) pref.EnumType {
Joe Tsai6f9095c2018-11-10 14:12:21 -080032 // Fast-path: check if a EnumType is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040033 if et, ok := legacyEnumTypeCache.Load(t); ok {
Joe Tsaif0c01e42018-11-06 13:05:20 -080034 return et.(pref.EnumType)
Joe Tsai6f9095c2018-11-10 14:12:21 -080035 }
36
37 // Slow-path: derive enum descriptor and initialize EnumType.
Joe Tsaib2f66be2019-05-22 00:42:45 -040038 var et pref.EnumType
Joe Tsai6f9095c2018-11-10 14:12:21 -080039 var m sync.Map // map[protoreflect.EnumNumber]proto.Enum
Joe Tsai21ade492019-05-22 13:42:54 -040040 ed := LegacyLoadEnumDesc(t)
Joe Tsaib2f66be2019-05-22 00:42:45 -040041 et = &prototype.Enum{
42 EnumDescriptor: ed,
43 NewEnum: func(n pref.EnumNumber) pref.Enum {
44 if e, ok := m.Load(n); ok {
45 return e.(pref.Enum)
46 }
Joe Tsai21ade492019-05-22 13:42:54 -040047 e := &legacyEnumWrapper{num: n, pbTyp: et, goTyp: t}
Joe Tsaib2f66be2019-05-22 00:42:45 -040048 m.Store(n, e)
49 return e
50 },
51 }
Joe Tsai21ade492019-05-22 13:42:54 -040052 if et, ok := legacyEnumTypeCache.LoadOrStore(t, et); ok {
Joe Tsaib9365042019-03-19 14:14:29 -070053 return et.(pref.EnumType)
54 }
55 return et
Joe Tsai6f9095c2018-11-10 14:12:21 -080056}
57
Joe Tsai21ade492019-05-22 13:42:54 -040058type legacyEnumWrapper struct {
Joe Tsai6f9095c2018-11-10 14:12:21 -080059 num pref.EnumNumber
60 pbTyp pref.EnumType
61 goTyp reflect.Type
62}
63
Joe Tsai21ade492019-05-22 13:42:54 -040064func (e *legacyEnumWrapper) Descriptor() pref.EnumDescriptor {
Joe Tsai0fc49f82019-05-01 12:29:25 -070065 return e.pbTyp.Descriptor()
66}
Joe Tsai21ade492019-05-22 13:42:54 -040067func (e *legacyEnumWrapper) Number() pref.EnumNumber {
Joe Tsai0fc49f82019-05-01 12:29:25 -070068 return e.num
69}
Joe Tsai21ade492019-05-22 13:42:54 -040070func (e *legacyEnumWrapper) ProtoReflect() pref.Enum {
Joe Tsai6f9095c2018-11-10 14:12:21 -080071 return e
72}
Joe Tsai21ade492019-05-22 13:42:54 -040073func (e *legacyEnumWrapper) ProtoUnwrap() interface{} {
Joe Tsai6f9095c2018-11-10 14:12:21 -080074 v := reflect.New(e.goTyp).Elem()
75 v.SetInt(int64(e.num))
76 return v.Interface()
77}
78
79var (
Joe Tsai21ade492019-05-22 13:42:54 -040080 _ pref.Enum = (*legacyEnumWrapper)(nil)
81 _ pvalue.Unwrapper = (*legacyEnumWrapper)(nil)
Joe Tsai6f9095c2018-11-10 14:12:21 -080082)
83
Joe Tsai21ade492019-05-22 13:42:54 -040084var legacyEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
Joe Tsai90fe9962018-10-18 11:06:29 -070085
Joe Tsai21ade492019-05-22 13:42:54 -040086var legacyEnumNumberType = reflect.TypeOf(pref.EnumNumber(0))
Joe Tsaif0c01e42018-11-06 13:05:20 -080087
Joe Tsai21ade492019-05-22 13:42:54 -040088// LegacyLoadEnumDesc returns an EnumDescriptor derived from the Go type,
Joe Tsai90fe9962018-10-18 11:06:29 -070089// which must be an int32 kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -070090//
91// This is exported for testing purposes.
Joe Tsai21ade492019-05-22 13:42:54 -040092func LegacyLoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070093 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040094 if ed, ok := legacyEnumDescCache.Load(t); ok {
Joe Tsaib9365042019-03-19 14:14:29 -070095 return ed.(pref.EnumDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -070096 }
97
Joe Tsaid8881392019-06-06 13:01:53 -070098 // Slow-path: initialize EnumDescriptor from the raw descriptor.
Joe Tsai90fe9962018-10-18 11:06:29 -070099 ev := reflect.Zero(t).Interface()
Damien Neila8593ba2019-01-08 16:18:07 -0800100 if _, ok := ev.(pref.Enum); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700101 panic(fmt.Sprintf("%v already implements proto.Enum", t))
102 }
Joe Tsaid8881392019-06-06 13:01:53 -0700103 edV1, ok := ev.(enumV1)
104 if !ok {
Joe Tsai851185d2019-07-01 13:45:52 -0700105 return aberrantLoadEnumDesc(t)
Joe Tsai90fe9962018-10-18 11:06:29 -0700106 }
Joe Tsaid8881392019-06-06 13:01:53 -0700107 b, idxs := edV1.EnumDescriptor()
Joe Tsai90fe9962018-10-18 11:06:29 -0700108
Joe Tsaid8881392019-06-06 13:01:53 -0700109 var ed pref.EnumDescriptor
110 if len(idxs) == 1 {
111 ed = legacyLoadFileDesc(b).Enums().Get(idxs[0])
112 } else {
113 md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
114 for _, i := range idxs[1 : len(idxs)-1] {
115 md = md.Messages().Get(i)
116 }
117 ed = md.Enums().Get(idxs[len(idxs)-1])
Joe Tsai90fe9962018-10-18 11:06:29 -0700118 }
Joe Tsai21ade492019-05-22 13:42:54 -0400119 if ed, ok := legacyEnumDescCache.LoadOrStore(t, ed); ok {
Joe Tsaid8881392019-06-06 13:01:53 -0700120 return ed.(protoreflect.EnumDescriptor)
Joe Tsaib9365042019-03-19 14:14:29 -0700121 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700122 return ed
123}
Joe Tsai851185d2019-07-01 13:45:52 -0700124
125var aberrantEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
126
127// aberrantLoadEnumDesc returns an EnumDescriptor derived from the Go type,
128// which must not implement protoreflect.Enum or enumV1.
129//
130// If the type does not implement enumV1, then there is no reliable
131// way to derive the original protobuf type information.
132// We are unable to use the global enum registry since it is
133// unfortunately keyed by the protobuf full name, which we also do not know.
134// Thus, this produces some bogus enum descriptor based on the Go type name.
135func aberrantLoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
136 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
137 if ed, ok := aberrantEnumDescCache.Load(t); ok {
138 return ed.(pref.EnumDescriptor)
139 }
140
141 // Slow-path: construct a bogus, but unique EnumDescriptor.
142 ed := &filedesc.Enum{L2: new(filedesc.EnumL2)}
143 ed.L0.FullName = aberrantDeriveFullName(t) // e.g., github_com.user.repo.MyEnum
144 ed.L0.ParentFile = filedesc.SurrogateProto3
145 ed.L2.Values.List = append(ed.L2.Values.List, filedesc.EnumValue{})
146
147 // TODO: Use the presence of a UnmarshalJSON method to determine proto2?
148
149 vd := &ed.L2.Values.List[0]
150 vd.L0.FullName = ed.L0.FullName + "_UNKNOWN" // e.g., github_com.user.repo.MyEnum_UNKNOWN
151 vd.L0.ParentFile = ed.L0.ParentFile
152 vd.L0.Parent = ed
153
154 // TODO: We could use the String method to obtain some enum value names by
155 // starting at 0 and print the enum until it produces invalid identifiers.
156 // An exhaustive query is clearly impractical, but can be best-effort.
157
158 if ed, ok := aberrantEnumDescCache.LoadOrStore(t, ed); ok {
159 return ed.(pref.EnumDescriptor)
160 }
161 return ed
162}
163
164// aberrantDeriveFullName derives a fully qualified protobuf name for the given Go type
165// The provided name is not guaranteed to be stable nor universally unique.
166// It should be sufficiently unique within a program.
167func aberrantDeriveFullName(t reflect.Type) pref.FullName {
168 sanitize := func(r rune) rune {
169 switch {
170 case r == '/':
171 return '.'
172 case 'a' <= r && r <= 'z', 'A' <= r && r <= 'Z', '0' <= r && r <= '9':
173 return r
174 default:
175 return '_'
176 }
177 }
178 prefix := strings.Map(sanitize, t.PkgPath())
179 suffix := strings.Map(sanitize, t.Name())
180 if suffix == "" {
181 suffix = fmt.Sprintf("UnknownX%X", reflect.ValueOf(t).Pointer())
182 }
183
184 ss := append(strings.Split(prefix, "."), suffix)
185 for i, s := range ss {
186 if s == "" || ('0' <= s[0] && s[0] <= '9') {
187 ss[i] = "x" + s
188 }
189 }
190 return pref.FullName(strings.Join(ss, "."))
191}