blob: 89b5c87507022580b66d5aefdf399dcb357e5e38 [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 Tsai6f9095c2018-11-10 14:12:21 -080013 pvalue "github.com/golang/protobuf/v2/internal/value"
Joe Tsai90fe9962018-10-18 11:06:29 -070014 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
15 ptype "github.com/golang/protobuf/v2/reflect/prototype"
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 Tsaif0c01e42018-11-06 13:05:20 -080048 enumTypeCache.Store(t, et)
49 return et.(pref.EnumType)
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 Tsai6dbffb72018-12-04 14:06:19 -080082// 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 Tsai6dbffb72018-12-04 14:06:19 -080084func loadEnumDesc(t reflect.Type) pref.EnumDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070085 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
86 if v, ok := enumDescCache.Load(t); ok {
87 return v.(pref.EnumDescriptor)
88 }
89
90 // Slow-path: initialize EnumDescriptor from the proto descriptor.
Joe Tsai6f9095c2018-11-10 14:12:21 -080091 if t.Kind() != reflect.Int32 || t.PkgPath() == "" {
92 panic(fmt.Sprintf("got %v, want named int32 kind", t))
Joe Tsai90fe9962018-10-18 11:06:29 -070093 }
Joe Tsaif0c01e42018-11-06 13:05:20 -080094 if t == enumNumberType {
95 panic(fmt.Sprintf("cannot be %v", t))
96 }
Joe Tsai90fe9962018-10-18 11:06:29 -070097
98 // Derive the enum descriptor from the raw descriptor proto.
99 e := new(ptype.StandaloneEnum)
100 ev := reflect.Zero(t).Interface()
Damien Neila8593ba2019-01-08 16:18:07 -0800101 if _, ok := ev.(pref.Enum); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700102 panic(fmt.Sprintf("%v already implements proto.Enum", t))
103 }
Joe Tsai6dbffb72018-12-04 14:06:19 -0800104 if ed, ok := ev.(enumV1); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700105 b, idxs := ed.EnumDescriptor()
Joe Tsai6dbffb72018-12-04 14:06:19 -0800106 fd := loadFileDesc(b)
Joe Tsai90fe9962018-10-18 11:06:29 -0700107
108 // Derive syntax.
109 switch fd.GetSyntax() {
110 case "proto2", "":
111 e.Syntax = pref.Proto2
112 case "proto3":
113 e.Syntax = pref.Proto3
114 }
115
116 // Derive the full name and correct enum descriptor.
Joe Tsaie1f8d502018-11-26 18:55:29 -0800117 var ed *descriptorpb.EnumDescriptorProto
Joe Tsai90fe9962018-10-18 11:06:29 -0700118 e.FullName = pref.FullName(fd.GetPackage())
119 if len(idxs) == 1 {
120 ed = fd.EnumType[idxs[0]]
121 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
122 } else {
123 md := fd.MessageType[idxs[0]]
124 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
125 for _, i := range idxs[1 : len(idxs)-1] {
126 md = md.NestedType[i]
127 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
128 }
129 ed = md.EnumType[idxs[len(idxs)-1]]
130 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
131 }
132
133 // Derive the enum values.
134 for _, vd := range ed.GetValue() {
135 e.Values = append(e.Values, ptype.EnumValue{
136 Name: pref.Name(vd.GetName()),
137 Number: pref.EnumNumber(vd.GetNumber()),
138 })
139 }
140 } else {
Joe Tsai6dbffb72018-12-04 14:06:19 -0800141 // If the type does not implement enumV1, then there is no reliable
Joe Tsai90fe9962018-10-18 11:06:29 -0700142 // way to derive the original protobuf type information.
143 // We are unable to use the global enum registry since it is
144 // unfortunately keyed by the full name, which we do not know.
145 // Furthermore, some generated enums register with a fork of
146 // golang/protobuf so the enum may not even be found in the registry.
147 //
148 // Instead, create a bogus enum descriptor to ensure that
149 // most operations continue to work. For example, textpb and jsonpb
150 // will be unable to parse a message with an enum value by name.
151 e.Syntax = pref.Proto2
152 e.FullName = deriveFullName(t)
153 e.Values = []ptype.EnumValue{{Name: "INVALID", Number: math.MinInt32}}
154 }
155
156 ed, err := ptype.NewEnum(e)
157 if err != nil {
158 panic(err)
159 }
160 enumDescCache.Store(t, ed)
161 return ed
162}