blob: 2a57b0b0153454f5a7c53b490fba59c7a2ce653d [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 Tsai08e00302018-11-26 22:32:06 -08005package legacy
Joe Tsai90fe9962018-10-18 11:06:29 -07006
7import (
8 "fmt"
9 "math"
10 "reflect"
11 "sync"
12
Joe Tsai990b9f52019-03-13 12:56:39 -070013 ptype "github.com/golang/protobuf/v2/internal/prototype"
Joe Tsai6f9095c2018-11-10 14:12:21 -080014 pvalue "github.com/golang/protobuf/v2/internal/value"
Joe Tsai90fe9962018-10-18 11:06:29 -070015 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsai90fe9962018-10-18 11:06:29 -070016)
17
Damien Neila8593ba2019-01-08 16:18:07 -080018// wrapEnum 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.
Damien Neila8593ba2019-01-08 16:18:07 -080020func wrapEnum(v reflect.Value) pref.Enum {
Joe Tsai6dbffb72018-12-04 14:06:19 -080021 et := loadEnumType(v.Type())
Joe Tsaif0c01e42018-11-06 13:05:20 -080022 return et.New(pref.EnumNumber(v.Int()))
23}
24
Joe Tsai6f9095c2018-11-10 14:12:21 -080025var enumTypeCache sync.Map // map[reflect.Type]protoreflect.EnumType
26
Joe Tsai6dbffb72018-12-04 14:06:19 -080027// loadEnumType 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 Tsai6dbffb72018-12-04 14:06:19 -080029func loadEnumType(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 Tsaif0c01e42018-11-06 13:05:20 -080031 if et, ok := enumTypeCache.Load(t); ok {
32 return et.(pref.EnumType)
Joe Tsai6f9095c2018-11-10 14:12:21 -080033 }
34
35 // Slow-path: derive enum descriptor and initialize EnumType.
36 var m sync.Map // map[protoreflect.EnumNumber]proto.Enum
Joe Tsai35ec98f2019-03-25 14:41:32 -070037 ed := LoadEnumDesc(t)
Damien Neila8593ba2019-01-08 16:18:07 -080038 et := ptype.GoEnum(ed, func(et pref.EnumType, n pref.EnumNumber) pref.Enum {
Joe Tsai6f9095c2018-11-10 14:12:21 -080039 if e, ok := m.Load(n); ok {
Damien Neila8593ba2019-01-08 16:18:07 -080040 return e.(pref.Enum)
Joe Tsai6f9095c2018-11-10 14:12:21 -080041 }
Joe Tsai6dbffb72018-12-04 14:06:19 -080042 e := &enumWrapper{num: n, pbTyp: et, goTyp: t}
Joe Tsai6f9095c2018-11-10 14:12:21 -080043 m.Store(n, e)
44 return e
45 })
Joe Tsaib9365042019-03-19 14:14:29 -070046 if et, ok := enumTypeCache.LoadOrStore(t, et); ok {
47 return et.(pref.EnumType)
48 }
49 return et
Joe Tsai6f9095c2018-11-10 14:12:21 -080050}
51
Joe Tsai6dbffb72018-12-04 14:06:19 -080052type enumWrapper struct {
Joe Tsai6f9095c2018-11-10 14:12:21 -080053 num pref.EnumNumber
54 pbTyp pref.EnumType
55 goTyp reflect.Type
56}
57
Joe Tsai0fc49f82019-05-01 12:29:25 -070058// TODO: Remove this.
Joe Tsai6dbffb72018-12-04 14:06:19 -080059func (e *enumWrapper) Type() pref.EnumType {
Joe Tsai6f9095c2018-11-10 14:12:21 -080060 return e.pbTyp
61}
Joe Tsai0fc49f82019-05-01 12:29:25 -070062func (e *enumWrapper) Descriptor() pref.EnumDescriptor {
63 return e.pbTyp.Descriptor()
64}
65func (e *enumWrapper) Number() pref.EnumNumber {
66 return e.num
67}
Joe Tsai6dbffb72018-12-04 14:06:19 -080068func (e *enumWrapper) ProtoReflect() pref.Enum {
Joe Tsai6f9095c2018-11-10 14:12:21 -080069 return e
70}
Joe Tsai6dbffb72018-12-04 14:06:19 -080071func (e *enumWrapper) ProtoUnwrap() interface{} {
Joe Tsai6f9095c2018-11-10 14:12:21 -080072 v := reflect.New(e.goTyp).Elem()
73 v.SetInt(int64(e.num))
74 return v.Interface()
75}
76
77var (
Joe Tsai6dbffb72018-12-04 14:06:19 -080078 _ pref.Enum = (*enumWrapper)(nil)
Joe Tsai6dbffb72018-12-04 14:06:19 -080079 _ pvalue.Unwrapper = (*enumWrapper)(nil)
Joe Tsai6f9095c2018-11-10 14:12:21 -080080)
81
Joe Tsai90fe9962018-10-18 11:06:29 -070082var enumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
83
Joe Tsaif0c01e42018-11-06 13:05:20 -080084var enumNumberType = reflect.TypeOf(pref.EnumNumber(0))
85
Joe Tsai35ec98f2019-03-25 14:41:32 -070086// LoadEnumDesc returns an EnumDescriptor derived from the Go type,
Joe Tsai90fe9962018-10-18 11:06:29 -070087// which must be an int32 kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -070088//
89// This is exported for testing purposes.
90func LoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070091 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
Joe Tsaib9365042019-03-19 14:14:29 -070092 if ed, ok := enumDescCache.Load(t); ok {
93 return ed.(pref.EnumDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -070094 }
95
96 // Slow-path: initialize EnumDescriptor from the proto descriptor.
Joe Tsai6f9095c2018-11-10 14:12:21 -080097 if t.Kind() != reflect.Int32 || t.PkgPath() == "" {
98 panic(fmt.Sprintf("got %v, want named int32 kind", t))
Joe Tsai90fe9962018-10-18 11:06:29 -070099 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800100 if t == enumNumberType {
101 panic(fmt.Sprintf("cannot be %v", t))
102 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700103
104 // Derive the enum descriptor from the raw descriptor proto.
105 e := new(ptype.StandaloneEnum)
106 ev := reflect.Zero(t).Interface()
Damien Neila8593ba2019-01-08 16:18:07 -0800107 if _, ok := ev.(pref.Enum); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700108 panic(fmt.Sprintf("%v already implements proto.Enum", t))
109 }
Joe Tsai6dbffb72018-12-04 14:06:19 -0800110 if ed, ok := ev.(enumV1); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700111 b, idxs := ed.EnumDescriptor()
Damien Neil987d5702019-04-10 11:06:53 -0700112 fd := loadFileDesc(b)
Joe Tsai90fe9962018-10-18 11:06:29 -0700113
114 // Derive syntax.
115 switch fd.GetSyntax() {
116 case "proto2", "":
117 e.Syntax = pref.Proto2
118 case "proto3":
119 e.Syntax = pref.Proto3
120 }
121
122 // Derive the full name and correct enum descriptor.
Damien Neil987d5702019-04-10 11:06:53 -0700123 var ed *enumDescriptorProto
Joe Tsai90fe9962018-10-18 11:06:29 -0700124 e.FullName = pref.FullName(fd.GetPackage())
125 if len(idxs) == 1 {
126 ed = fd.EnumType[idxs[0]]
127 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
128 } else {
129 md := fd.MessageType[idxs[0]]
130 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
131 for _, i := range idxs[1 : len(idxs)-1] {
132 md = md.NestedType[i]
133 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
134 }
135 ed = md.EnumType[idxs[len(idxs)-1]]
136 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
137 }
138
139 // Derive the enum values.
Damien Neil987d5702019-04-10 11:06:53 -0700140 for _, vd := range ed.Value {
Joe Tsai90fe9962018-10-18 11:06:29 -0700141 e.Values = append(e.Values, ptype.EnumValue{
142 Name: pref.Name(vd.GetName()),
143 Number: pref.EnumNumber(vd.GetNumber()),
144 })
145 }
146 } else {
Joe Tsai6dbffb72018-12-04 14:06:19 -0800147 // If the type does not implement enumV1, then there is no reliable
Joe Tsai90fe9962018-10-18 11:06:29 -0700148 // way to derive the original protobuf type information.
149 // We are unable to use the global enum registry since it is
150 // unfortunately keyed by the full name, which we do not know.
151 // Furthermore, some generated enums register with a fork of
152 // golang/protobuf so the enum may not even be found in the registry.
153 //
154 // Instead, create a bogus enum descriptor to ensure that
155 // most operations continue to work. For example, textpb and jsonpb
156 // will be unable to parse a message with an enum value by name.
157 e.Syntax = pref.Proto2
158 e.FullName = deriveFullName(t)
159 e.Values = []ptype.EnumValue{{Name: "INVALID", Number: math.MinInt32}}
160 }
161
162 ed, err := ptype.NewEnum(e)
163 if err != nil {
164 panic(err)
165 }
Joe Tsaib9365042019-03-19 14:14:29 -0700166 if ed, ok := enumDescCache.LoadOrStore(t, ed); ok {
167 return ed.(pref.EnumDescriptor)
168 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700169 return ed
170}