blob: 831b94c27e85ab5318ccfb92931204228e7da347 [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 Tsaie1f8d502018-11-26 18:55:29 -080016
17 descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
Joe Tsai90fe9962018-10-18 11:06:29 -070018)
19
Damien Neila8593ba2019-01-08 16:18:07 -080020// wrapEnum 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.
Damien Neila8593ba2019-01-08 16:18:07 -080022func wrapEnum(v reflect.Value) pref.Enum {
Joe Tsai6dbffb72018-12-04 14:06:19 -080023 et := loadEnumType(v.Type())
Joe Tsaif0c01e42018-11-06 13:05:20 -080024 return et.New(pref.EnumNumber(v.Int()))
25}
26
Joe Tsai6f9095c2018-11-10 14:12:21 -080027var enumTypeCache sync.Map // map[reflect.Type]protoreflect.EnumType
28
Joe Tsai6dbffb72018-12-04 14:06:19 -080029// loadEnumType 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 Tsai6dbffb72018-12-04 14:06:19 -080031func loadEnumType(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 Tsaif0c01e42018-11-06 13:05:20 -080033 if et, ok := enumTypeCache.Load(t); ok {
34 return et.(pref.EnumType)
Joe Tsai6f9095c2018-11-10 14:12:21 -080035 }
36
37 // Slow-path: derive enum descriptor and initialize EnumType.
38 var m sync.Map // map[protoreflect.EnumNumber]proto.Enum
Joe Tsai35ec98f2019-03-25 14:41:32 -070039 ed := LoadEnumDesc(t)
Damien Neila8593ba2019-01-08 16:18:07 -080040 et := ptype.GoEnum(ed, func(et pref.EnumType, n pref.EnumNumber) pref.Enum {
Joe Tsai6f9095c2018-11-10 14:12:21 -080041 if e, ok := m.Load(n); ok {
Damien Neila8593ba2019-01-08 16:18:07 -080042 return e.(pref.Enum)
Joe Tsai6f9095c2018-11-10 14:12:21 -080043 }
Joe Tsai6dbffb72018-12-04 14:06:19 -080044 e := &enumWrapper{num: n, pbTyp: et, goTyp: t}
Joe Tsai6f9095c2018-11-10 14:12:21 -080045 m.Store(n, e)
46 return e
47 })
Joe Tsaib9365042019-03-19 14:14:29 -070048 if et, ok := enumTypeCache.LoadOrStore(t, et); ok {
49 return et.(pref.EnumType)
50 }
51 return et
Joe Tsai6f9095c2018-11-10 14:12:21 -080052}
53
Joe Tsai6dbffb72018-12-04 14:06:19 -080054type enumWrapper struct {
Joe Tsai6f9095c2018-11-10 14:12:21 -080055 num pref.EnumNumber
56 pbTyp pref.EnumType
57 goTyp reflect.Type
58}
59
Joe Tsai6dbffb72018-12-04 14:06:19 -080060func (e *enumWrapper) Number() pref.EnumNumber {
Joe Tsai6f9095c2018-11-10 14:12:21 -080061 return e.num
62}
Joe Tsai6dbffb72018-12-04 14:06:19 -080063func (e *enumWrapper) Type() pref.EnumType {
Joe Tsai6f9095c2018-11-10 14:12:21 -080064 return e.pbTyp
65}
Joe Tsai6dbffb72018-12-04 14:06:19 -080066func (e *enumWrapper) ProtoReflect() pref.Enum {
Joe Tsai6f9095c2018-11-10 14:12:21 -080067 return e
68}
Joe Tsai6dbffb72018-12-04 14:06:19 -080069func (e *enumWrapper) ProtoUnwrap() interface{} {
Joe Tsai6f9095c2018-11-10 14:12:21 -080070 v := reflect.New(e.goTyp).Elem()
71 v.SetInt(int64(e.num))
72 return v.Interface()
73}
74
75var (
Joe Tsai6dbffb72018-12-04 14:06:19 -080076 _ pref.Enum = (*enumWrapper)(nil)
Joe Tsai6dbffb72018-12-04 14:06:19 -080077 _ pvalue.Unwrapper = (*enumWrapper)(nil)
Joe Tsai6f9095c2018-11-10 14:12:21 -080078)
79
Joe Tsai90fe9962018-10-18 11:06:29 -070080var enumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
81
Joe Tsaif0c01e42018-11-06 13:05:20 -080082var enumNumberType = reflect.TypeOf(pref.EnumNumber(0))
83
Joe Tsai35ec98f2019-03-25 14:41:32 -070084// LoadEnumDesc returns an EnumDescriptor derived from the Go type,
Joe Tsai90fe9962018-10-18 11:06:29 -070085// which must be an int32 kind and not implement the v2 API already.
Joe Tsai35ec98f2019-03-25 14:41:32 -070086//
87// This is exported for testing purposes.
88func LoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070089 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
Joe Tsaib9365042019-03-19 14:14:29 -070090 if ed, ok := enumDescCache.Load(t); ok {
91 return ed.(pref.EnumDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -070092 }
93
94 // Slow-path: initialize EnumDescriptor from the proto descriptor.
Joe Tsai6f9095c2018-11-10 14:12:21 -080095 if t.Kind() != reflect.Int32 || t.PkgPath() == "" {
96 panic(fmt.Sprintf("got %v, want named int32 kind", t))
Joe Tsai90fe9962018-10-18 11:06:29 -070097 }
Joe Tsaif0c01e42018-11-06 13:05:20 -080098 if t == enumNumberType {
99 panic(fmt.Sprintf("cannot be %v", t))
100 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700101
102 // Derive the enum descriptor from the raw descriptor proto.
103 e := new(ptype.StandaloneEnum)
104 ev := reflect.Zero(t).Interface()
Damien Neila8593ba2019-01-08 16:18:07 -0800105 if _, ok := ev.(pref.Enum); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700106 panic(fmt.Sprintf("%v already implements proto.Enum", t))
107 }
Joe Tsai6dbffb72018-12-04 14:06:19 -0800108 if ed, ok := ev.(enumV1); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700109 b, idxs := ed.EnumDescriptor()
Joe Tsai35ec98f2019-03-25 14:41:32 -0700110 fd := LoadFileDesc(b)
Joe Tsai90fe9962018-10-18 11:06:29 -0700111
112 // Derive syntax.
113 switch fd.GetSyntax() {
114 case "proto2", "":
115 e.Syntax = pref.Proto2
116 case "proto3":
117 e.Syntax = pref.Proto3
118 }
119
120 // Derive the full name and correct enum descriptor.
Joe Tsaie1f8d502018-11-26 18:55:29 -0800121 var ed *descriptorpb.EnumDescriptorProto
Joe Tsai90fe9962018-10-18 11:06:29 -0700122 e.FullName = pref.FullName(fd.GetPackage())
123 if len(idxs) == 1 {
124 ed = fd.EnumType[idxs[0]]
125 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
126 } else {
127 md := fd.MessageType[idxs[0]]
128 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
129 for _, i := range idxs[1 : len(idxs)-1] {
130 md = md.NestedType[i]
131 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
132 }
133 ed = md.EnumType[idxs[len(idxs)-1]]
134 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
135 }
136
137 // Derive the enum values.
138 for _, vd := range ed.GetValue() {
139 e.Values = append(e.Values, ptype.EnumValue{
140 Name: pref.Name(vd.GetName()),
141 Number: pref.EnumNumber(vd.GetNumber()),
142 })
143 }
144 } else {
Joe Tsai6dbffb72018-12-04 14:06:19 -0800145 // If the type does not implement enumV1, then there is no reliable
Joe Tsai90fe9962018-10-18 11:06:29 -0700146 // way to derive the original protobuf type information.
147 // We are unable to use the global enum registry since it is
148 // unfortunately keyed by the full name, which we do not know.
149 // Furthermore, some generated enums register with a fork of
150 // golang/protobuf so the enum may not even be found in the registry.
151 //
152 // Instead, create a bogus enum descriptor to ensure that
153 // most operations continue to work. For example, textpb and jsonpb
154 // will be unable to parse a message with an enum value by name.
155 e.Syntax = pref.Proto2
156 e.FullName = deriveFullName(t)
157 e.Values = []ptype.EnumValue{{Name: "INVALID", Number: math.MinInt32}}
158 }
159
160 ed, err := ptype.NewEnum(e)
161 if err != nil {
162 panic(err)
163 }
Joe Tsaib9365042019-03-19 14:14:29 -0700164 if ed, ok := enumDescCache.LoadOrStore(t, ed); ok {
165 return ed.(pref.EnumDescriptor)
166 }
Joe Tsai90fe9962018-10-18 11:06:29 -0700167 return ed
168}