blob: 23eebd8eb60d3da6fb04673aaf8af17dfb36cac5 [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 Tsai6dbffb72018-12-04 14:06:19 -080039 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 Tsai6dbffb72018-12-04 14:06:19 -080084// 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 Tsai6dbffb72018-12-04 14:06:19 -080086func 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()
Joe Tsai6dbffb72018-12-04 14:06:19 -0800108 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.
Joe Tsaie1f8d502018-11-26 18:55:29 -0800119 var ed *descriptorpb.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.
136 for _, vd := range ed.GetValue() {
137 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}