blob: bba130dba7a7aaa647f2ee9a42cc9f09c3b7ce3d [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
5package impl
6
7import (
8 "fmt"
9 "math"
10 "reflect"
11 "sync"
12
13 descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor"
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"
16 ptype "github.com/golang/protobuf/v2/reflect/prototype"
17)
18
Joe Tsaif0c01e42018-11-06 13:05:20 -080019// legacyWrapEnum wraps v as a protoreflect.ProtoEnum,
20// where v must be a *struct kind and not implement the v2 API already.
21func legacyWrapEnum(v reflect.Value) pref.ProtoEnum {
22 et := legacyLoadEnumType(v.Type())
23 return et.New(pref.EnumNumber(v.Int()))
24}
25
Joe Tsai6f9095c2018-11-10 14:12:21 -080026var enumTypeCache sync.Map // map[reflect.Type]protoreflect.EnumType
27
Joe Tsaif0c01e42018-11-06 13:05:20 -080028// legacyLoadEnumType dynamically loads a protoreflect.EnumType for t,
29// where t must be an int32 kind and not implement the v2 API already.
30func legacyLoadEnumType(t reflect.Type) pref.EnumType {
Joe Tsai6f9095c2018-11-10 14:12:21 -080031 // Fast-path: check if a EnumType is cached for this concrete type.
Joe Tsaif0c01e42018-11-06 13:05:20 -080032 if et, ok := enumTypeCache.Load(t); ok {
33 return et.(pref.EnumType)
Joe Tsai6f9095c2018-11-10 14:12:21 -080034 }
35
36 // Slow-path: derive enum descriptor and initialize EnumType.
37 var m sync.Map // map[protoreflect.EnumNumber]proto.Enum
Joe Tsaif0c01e42018-11-06 13:05:20 -080038 ed := legacyLoadEnumDesc(t)
Joe Tsai6f9095c2018-11-10 14:12:21 -080039 et := ptype.GoEnum(ed, func(et pref.EnumType, n pref.EnumNumber) pref.ProtoEnum {
40 if e, ok := m.Load(n); ok {
41 return e.(pref.ProtoEnum)
42 }
Joe Tsaif0c01e42018-11-06 13:05:20 -080043 e := &legacyEnumWrapper{num: n, pbTyp: et, goTyp: t}
Joe Tsai6f9095c2018-11-10 14:12:21 -080044 m.Store(n, e)
45 return e
46 })
Joe Tsaif0c01e42018-11-06 13:05:20 -080047 enumTypeCache.Store(t, et)
48 return et.(pref.EnumType)
Joe Tsai6f9095c2018-11-10 14:12:21 -080049}
50
51type legacyEnumWrapper struct {
52 num pref.EnumNumber
53 pbTyp pref.EnumType
54 goTyp reflect.Type
55}
56
57func (e *legacyEnumWrapper) Number() pref.EnumNumber {
58 return e.num
59}
60func (e *legacyEnumWrapper) Type() pref.EnumType {
61 return e.pbTyp
62}
63func (e *legacyEnumWrapper) ProtoReflect() pref.Enum {
64 return e
65}
66func (e *legacyEnumWrapper) Unwrap() interface{} {
67 v := reflect.New(e.goTyp).Elem()
68 v.SetInt(int64(e.num))
69 return v.Interface()
70}
71
72var (
73 _ pref.Enum = (*legacyEnumWrapper)(nil)
74 _ pref.ProtoEnum = (*legacyEnumWrapper)(nil)
75 _ pvalue.Unwrapper = (*legacyEnumWrapper)(nil)
76)
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
82// legacyLoadEnumDesc 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 Tsaif0c01e42018-11-06 13:05:20 -080084func legacyLoadEnumDesc(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()
101 if _, ok := ev.(pref.ProtoEnum); ok {
102 panic(fmt.Sprintf("%v already implements proto.Enum", t))
103 }
104 if ed, ok := ev.(legacyEnum); ok {
105 b, idxs := ed.EnumDescriptor()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800106 fd := legacyLoadFileDesc(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.
117 var ed *descriptorV1.EnumDescriptorProto
118 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 {
141 // If the type does not implement legacyEnum, then there is no reliable
142 // 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}