blob: 3c24eb3bc3e785abca9a441c85db9d682b26606f [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 Tsaid4211502019-07-02 14:58:02 -070067func (e *legacyEnumWrapper) Type() pref.EnumType {
68 return e.pbTyp
69}
Joe Tsai21ade492019-05-22 13:42:54 -040070func (e *legacyEnumWrapper) Number() pref.EnumNumber {
Joe Tsai0fc49f82019-05-01 12:29:25 -070071 return e.num
72}
Joe Tsai21ade492019-05-22 13:42:54 -040073func (e *legacyEnumWrapper) ProtoReflect() pref.Enum {
Joe Tsai6f9095c2018-11-10 14:12:21 -080074 return e
75}
Joe Tsai21ade492019-05-22 13:42:54 -040076func (e *legacyEnumWrapper) ProtoUnwrap() interface{} {
Joe Tsai6f9095c2018-11-10 14:12:21 -080077 v := reflect.New(e.goTyp).Elem()
78 v.SetInt(int64(e.num))
79 return v.Interface()
80}
81
82var (
Joe Tsai21ade492019-05-22 13:42:54 -040083 _ pref.Enum = (*legacyEnumWrapper)(nil)
84 _ pvalue.Unwrapper = (*legacyEnumWrapper)(nil)
Joe Tsai6f9095c2018-11-10 14:12:21 -080085)
86
Joe Tsai21ade492019-05-22 13:42:54 -040087var legacyEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
Joe Tsai90fe9962018-10-18 11:06:29 -070088
Joe Tsai21ade492019-05-22 13:42:54 -040089var legacyEnumNumberType = reflect.TypeOf(pref.EnumNumber(0))
Joe Tsaif0c01e42018-11-06 13:05:20 -080090
Joe Tsai21ade492019-05-22 13:42:54 -040091// LegacyLoadEnumDesc returns an EnumDescriptor derived from the Go type,
Joe Tsai90fe9962018-10-18 11:06:29 -070092// which must be an int32 kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -070093//
94// This is exported for testing purposes.
Joe Tsai21ade492019-05-22 13:42:54 -040095func LegacyLoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070096 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
Joe Tsai21ade492019-05-22 13:42:54 -040097 if ed, ok := legacyEnumDescCache.Load(t); ok {
Joe Tsaib9365042019-03-19 14:14:29 -070098 return ed.(pref.EnumDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -070099 }
100
Joe Tsaid8881392019-06-06 13:01:53 -0700101 // Slow-path: initialize EnumDescriptor from the raw descriptor.
Joe Tsai90fe9962018-10-18 11:06:29 -0700102 ev := reflect.Zero(t).Interface()
Damien Neila8593ba2019-01-08 16:18:07 -0800103 if _, ok := ev.(pref.Enum); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700104 panic(fmt.Sprintf("%v already implements proto.Enum", t))
105 }
Joe Tsaid8881392019-06-06 13:01:53 -0700106 edV1, ok := ev.(enumV1)
107 if !ok {
Joe Tsai851185d2019-07-01 13:45:52 -0700108 return aberrantLoadEnumDesc(t)
Joe Tsai90fe9962018-10-18 11:06:29 -0700109 }
Joe Tsaid8881392019-06-06 13:01:53 -0700110 b, idxs := edV1.EnumDescriptor()
Joe Tsai90fe9962018-10-18 11:06:29 -0700111
Joe Tsaid8881392019-06-06 13:01:53 -0700112 var ed pref.EnumDescriptor
113 if len(idxs) == 1 {
114 ed = legacyLoadFileDesc(b).Enums().Get(idxs[0])
115 } else {
116 md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
117 for _, i := range idxs[1 : len(idxs)-1] {
118 md = md.Messages().Get(i)
119 }
120 ed = md.Enums().Get(idxs[len(idxs)-1])
Joe Tsai90fe9962018-10-18 11:06:29 -0700121 }
Joe Tsai21ade492019-05-22 13:42:54 -0400122 if ed, ok := legacyEnumDescCache.LoadOrStore(t, ed); ok {
Joe Tsaid8881392019-06-06 13:01:53 -0700123 return ed.(protoreflect.EnumDescriptor)
Joe Tsaib9365042019-03-19 14:14:29 -0700124 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700125 return ed
126}
Joe Tsai851185d2019-07-01 13:45:52 -0700127
128var aberrantEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
129
130// aberrantLoadEnumDesc returns an EnumDescriptor derived from the Go type,
131// which must not implement protoreflect.Enum or enumV1.
132//
133// If the type does not implement enumV1, then there is no reliable
134// way to derive the original protobuf type information.
135// We are unable to use the global enum registry since it is
136// unfortunately keyed by the protobuf full name, which we also do not know.
137// Thus, this produces some bogus enum descriptor based on the Go type name.
138func aberrantLoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
139 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
140 if ed, ok := aberrantEnumDescCache.Load(t); ok {
141 return ed.(pref.EnumDescriptor)
142 }
143
144 // Slow-path: construct a bogus, but unique EnumDescriptor.
145 ed := &filedesc.Enum{L2: new(filedesc.EnumL2)}
146 ed.L0.FullName = aberrantDeriveFullName(t) // e.g., github_com.user.repo.MyEnum
147 ed.L0.ParentFile = filedesc.SurrogateProto3
148 ed.L2.Values.List = append(ed.L2.Values.List, filedesc.EnumValue{})
149
150 // TODO: Use the presence of a UnmarshalJSON method to determine proto2?
151
152 vd := &ed.L2.Values.List[0]
153 vd.L0.FullName = ed.L0.FullName + "_UNKNOWN" // e.g., github_com.user.repo.MyEnum_UNKNOWN
154 vd.L0.ParentFile = ed.L0.ParentFile
155 vd.L0.Parent = ed
156
157 // TODO: We could use the String method to obtain some enum value names by
158 // starting at 0 and print the enum until it produces invalid identifiers.
159 // An exhaustive query is clearly impractical, but can be best-effort.
160
161 if ed, ok := aberrantEnumDescCache.LoadOrStore(t, ed); ok {
162 return ed.(pref.EnumDescriptor)
163 }
164 return ed
165}
166
167// aberrantDeriveFullName derives a fully qualified protobuf name for the given Go type
168// The provided name is not guaranteed to be stable nor universally unique.
169// It should be sufficiently unique within a program.
170func aberrantDeriveFullName(t reflect.Type) pref.FullName {
171 sanitize := func(r rune) rune {
172 switch {
173 case r == '/':
174 return '.'
175 case 'a' <= r && r <= 'z', 'A' <= r && r <= 'Z', '0' <= r && r <= '9':
176 return r
177 default:
178 return '_'
179 }
180 }
181 prefix := strings.Map(sanitize, t.PkgPath())
182 suffix := strings.Map(sanitize, t.Name())
183 if suffix == "" {
184 suffix = fmt.Sprintf("UnknownX%X", reflect.ValueOf(t).Pointer())
185 }
186
187 ss := append(strings.Split(prefix, "."), suffix)
188 for i, s := range ss {
189 if s == "" || ('0' <= s[0] && s[0] <= '9') {
190 ss[i] = "x" + s
191 }
192 }
193 return pref.FullName(strings.Join(ss, "."))
194}