blob: e71bc2a30b541a7de497057bf018cc241f10fe8d [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
Joe Tsai6dbffb72018-12-04 14:06:19 -080020// wrapEnum wraps v as a protoreflect.ProtoEnum,
Joe Tsai08e00302018-11-26 22:32:06 -080021// where v must be a int32 kind and not implement the v2 API already.
Joe Tsai6dbffb72018-12-04 14:06:19 -080022func wrapEnum(v reflect.Value) pref.ProtoEnum {
23 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)
Joe Tsai6f9095c2018-11-10 14:12:21 -080040 et := ptype.GoEnum(ed, func(et pref.EnumType, n pref.EnumNumber) pref.ProtoEnum {
41 if e, ok := m.Load(n); ok {
42 return e.(pref.ProtoEnum)
43 }
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)
75 _ pref.ProtoEnum = (*enumWrapper)(nil)
76 _ pvalue.Unwrapper = (*enumWrapper)(nil)
Joe Tsai6f9095c2018-11-10 14:12:21 -080077)
78
Joe Tsai90fe9962018-10-18 11:06:29 -070079var enumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
80
Joe Tsaif0c01e42018-11-06 13:05:20 -080081var enumNumberType = reflect.TypeOf(pref.EnumNumber(0))
82
Joe Tsai6dbffb72018-12-04 14:06:19 -080083// loadEnumDesc returns an EnumDescriptor derived from the Go type,
Joe Tsai90fe9962018-10-18 11:06:29 -070084// which must be an int32 kind and not implement the v2 API already.
Joe Tsai6dbffb72018-12-04 14:06:19 -080085func loadEnumDesc(t reflect.Type) pref.EnumDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070086 // Fast-path: check if an EnumDescriptor is cached for this concrete type.
87 if v, ok := enumDescCache.Load(t); ok {
88 return v.(pref.EnumDescriptor)
89 }
90
91 // Slow-path: initialize EnumDescriptor from the proto descriptor.
Joe Tsai6f9095c2018-11-10 14:12:21 -080092 if t.Kind() != reflect.Int32 || t.PkgPath() == "" {
93 panic(fmt.Sprintf("got %v, want named int32 kind", t))
Joe Tsai90fe9962018-10-18 11:06:29 -070094 }
Joe Tsaif0c01e42018-11-06 13:05:20 -080095 if t == enumNumberType {
96 panic(fmt.Sprintf("cannot be %v", t))
97 }
Joe Tsai90fe9962018-10-18 11:06:29 -070098
99 // Derive the enum descriptor from the raw descriptor proto.
100 e := new(ptype.StandaloneEnum)
101 ev := reflect.Zero(t).Interface()
102 if _, ok := ev.(pref.ProtoEnum); ok {
103 panic(fmt.Sprintf("%v already implements proto.Enum", t))
104 }
Joe Tsai6dbffb72018-12-04 14:06:19 -0800105 if ed, ok := ev.(enumV1); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700106 b, idxs := ed.EnumDescriptor()
Joe Tsai6dbffb72018-12-04 14:06:19 -0800107 fd := loadFileDesc(b)
Joe Tsai90fe9962018-10-18 11:06:29 -0700108
109 // Derive syntax.
110 switch fd.GetSyntax() {
111 case "proto2", "":
112 e.Syntax = pref.Proto2
113 case "proto3":
114 e.Syntax = pref.Proto3
115 }
116
117 // Derive the full name and correct enum descriptor.
Joe Tsaie1f8d502018-11-26 18:55:29 -0800118 var ed *descriptorpb.EnumDescriptorProto
Joe Tsai90fe9962018-10-18 11:06:29 -0700119 e.FullName = pref.FullName(fd.GetPackage())
120 if len(idxs) == 1 {
121 ed = fd.EnumType[idxs[0]]
122 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
123 } else {
124 md := fd.MessageType[idxs[0]]
125 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
126 for _, i := range idxs[1 : len(idxs)-1] {
127 md = md.NestedType[i]
128 e.FullName = e.FullName.Append(pref.Name(md.GetName()))
129 }
130 ed = md.EnumType[idxs[len(idxs)-1]]
131 e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
132 }
133
134 // Derive the enum values.
135 for _, vd := range ed.GetValue() {
136 e.Values = append(e.Values, ptype.EnumValue{
137 Name: pref.Name(vd.GetName()),
138 Number: pref.EnumNumber(vd.GetNumber()),
139 })
140 }
141 } else {
Joe Tsai6dbffb72018-12-04 14:06:19 -0800142 // If the type does not implement enumV1, then there is no reliable
Joe Tsai90fe9962018-10-18 11:06:29 -0700143 // way to derive the original protobuf type information.
144 // We are unable to use the global enum registry since it is
145 // unfortunately keyed by the full name, which we do not know.
146 // Furthermore, some generated enums register with a fork of
147 // golang/protobuf so the enum may not even be found in the registry.
148 //
149 // Instead, create a bogus enum descriptor to ensure that
150 // most operations continue to work. For example, textpb and jsonpb
151 // will be unable to parse a message with an enum value by name.
152 e.Syntax = pref.Proto2
153 e.FullName = deriveFullName(t)
154 e.Values = []ptype.EnumValue{{Name: "INVALID", Number: math.MinInt32}}
155 }
156
157 ed, err := ptype.NewEnum(e)
158 if err != nil {
159 panic(err)
160 }
161 enumDescCache.Store(t, ed)
162 return ed
163}