blob: 0ee397f646b67282b596a77fc0a3d591e5bf968a [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 Tsai6dbffb72018-12-04 14:06:19 -080058func (e *enumWrapper) Number() pref.EnumNumber {
Joe Tsai6f9095c2018-11-10 14:12:21 -080059 return e.num
60}
Joe Tsai6dbffb72018-12-04 14:06:19 -080061func (e *enumWrapper) Type() pref.EnumType {
Joe Tsai6f9095c2018-11-10 14:12:21 -080062 return e.pbTyp
63}
Joe Tsai6dbffb72018-12-04 14:06:19 -080064func (e *enumWrapper) ProtoReflect() pref.Enum {
Joe Tsai6f9095c2018-11-10 14:12:21 -080065 return e
66}
Joe Tsai6dbffb72018-12-04 14:06:19 -080067func (e *enumWrapper) ProtoUnwrap() interface{} {
Joe Tsai6f9095c2018-11-10 14:12:21 -080068 v := reflect.New(e.goTyp).Elem()
69 v.SetInt(int64(e.num))
70 return v.Interface()
71}
72
73var (
Joe Tsai6dbffb72018-12-04 14:06:19 -080074 _ pref.Enum = (*enumWrapper)(nil)
Joe Tsai6dbffb72018-12-04 14:06:19 -080075 _ pvalue.Unwrapper = (*enumWrapper)(nil)
Joe Tsai6f9095c2018-11-10 14:12:21 -080076)
77
Joe Tsai90fe9962018-10-18 11:06:29 -070078var enumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
79
Joe Tsaif0c01e42018-11-06 13:05:20 -080080var enumNumberType = reflect.TypeOf(pref.EnumNumber(0))
81
Joe Tsai35ec98f2019-03-25 14:41:32 -070082// LoadEnumDesc returns an EnumDescriptor derived from the Go type,
Joe Tsai90fe9962018-10-18 11:06:29 -070083// which must be an int32 kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -070084//
85// This is exported for testing purposes.
86func LoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070087 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
Joe Tsaib9365042019-03-19 14:14:29 -070088 if ed, ok := enumDescCache.Load(t); ok {
89 return ed.(pref.EnumDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -070090 }
91
92 // Slow-path: initialize EnumDescriptor from the proto descriptor.
Joe Tsai6f9095c2018-11-10 14:12:21 -080093 if t.Kind() != reflect.Int32 || t.PkgPath() == "" {
94 panic(fmt.Sprintf("got %v, want named int32 kind", t))
Joe Tsai90fe9962018-10-18 11:06:29 -070095 }
Joe Tsaif0c01e42018-11-06 13:05:20 -080096 if t == enumNumberType {
97 panic(fmt.Sprintf("cannot be %v", t))
98 }
Joe Tsai90fe9962018-10-18 11:06:29 -070099
100 // Derive the enum descriptor from the raw descriptor proto.
101 e := new(ptype.StandaloneEnum)
102 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 Tsai6dbffb72018-12-04 14:06:19 -0800106 if ed, ok := ev.(enumV1); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700107 b, idxs := ed.EnumDescriptor()
Damien Neil987d5702019-04-10 11:06:53 -0700108 fd := loadFileDesc(b)
Joe Tsai90fe9962018-10-18 11:06:29 -0700109
110 // Derive syntax.
111 switch fd.GetSyntax() {
112 case "proto2", "":
113 e.Syntax = pref.Proto2
114 case "proto3":
115 e.Syntax = pref.Proto3
116 }
117
118 // Derive the full name and correct enum descriptor.
Damien Neil987d5702019-04-10 11:06:53 -0700119 var ed *enumDescriptorProto
Joe Tsai90fe9962018-10-18 11:06:29 -0700120 e.FullName = pref.FullName(fd.GetPackage())
121 if len(idxs) == 1 {
122 ed = fd.EnumType[idxs[0]]
123 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
124 } else {
125 md := fd.MessageType[idxs[0]]
126 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
127 for _, i := range idxs[1 : len(idxs)-1] {
128 md = md.NestedType[i]
129 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
130 }
131 ed = md.EnumType[idxs[len(idxs)-1]]
132 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
133 }
134
135 // Derive the enum values.
Damien Neil987d5702019-04-10 11:06:53 -0700136 for _, vd := range ed.Value {
Joe Tsai90fe9962018-10-18 11:06:29 -0700137 e.Values = append(e.Values, ptype.EnumValue{
138 Name: pref.Name(vd.GetName()),
139 Number: pref.EnumNumber(vd.GetNumber()),
140 })
141 }
142 } else {
Joe Tsai6dbffb72018-12-04 14:06:19 -0800143 // If the type does not implement enumV1, then there is no reliable
Joe Tsai90fe9962018-10-18 11:06:29 -0700144 // way to derive the original protobuf type information.
145 // We are unable to use the global enum registry since it is
146 // unfortunately keyed by the full name, which we do not know.
147 // Furthermore, some generated enums register with a fork of
148 // golang/protobuf so the enum may not even be found in the registry.
149 //
150 // Instead, create a bogus enum descriptor to ensure that
151 // most operations continue to work. For example, textpb and jsonpb
152 // will be unable to parse a message with an enum value by name.
153 e.Syntax = pref.Proto2
154 e.FullName = deriveFullName(t)
155 e.Values = []ptype.EnumValue{{Name: "INVALID", Number: math.MinInt32}}
156 }
157
158 ed, err := ptype.NewEnum(e)
159 if err != nil {
160 panic(err)
161 }
Joe Tsaib9365042019-03-19 14:14:29 -0700162 if ed, ok := enumDescCache.LoadOrStore(t, ed); ok {
163 return ed.(pref.EnumDescriptor)
164 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700165 return ed
166}