blob: 185ef2efa5bfd48718cffe77e890b777daf21733 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301// 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 "reflect"
10
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053011 "google.golang.org/protobuf/reflect/protoreflect"
Naveen Sampath04696f72022-06-13 15:19:14 +053012)
13
14// unwrapper unwraps the value to the underlying value.
15// This is implemented by List and Map.
16type unwrapper interface {
17 protoUnwrap() interface{}
18}
19
20// A Converter coverts to/from Go reflect.Value types and protobuf protoreflect.Value types.
21type Converter interface {
22 // PBValueOf converts a reflect.Value to a protoreflect.Value.
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053023 PBValueOf(reflect.Value) protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +053024
25 // GoValueOf converts a protoreflect.Value to a reflect.Value.
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053026 GoValueOf(protoreflect.Value) reflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +053027
28 // IsValidPB returns whether a protoreflect.Value is compatible with this type.
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053029 IsValidPB(protoreflect.Value) bool
Naveen Sampath04696f72022-06-13 15:19:14 +053030
31 // IsValidGo returns whether a reflect.Value is compatible with this type.
32 IsValidGo(reflect.Value) bool
33
34 // New returns a new field value.
35 // For scalars, it returns the default value of the field.
36 // For composite types, it returns a new mutable value.
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053037 New() protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +053038
39 // Zero returns a new field value.
40 // For scalars, it returns the default value of the field.
41 // For composite types, it returns an immutable, empty value.
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053042 Zero() protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +053043}
44
45// NewConverter matches a Go type with a protobuf field and returns a Converter
46// that converts between the two. Enums must be a named int32 kind that
47// implements protoreflect.Enum, and messages must be pointer to a named
48// struct type that implements protoreflect.ProtoMessage.
49//
50// This matcher deliberately supports a wider range of Go types than what
51// protoc-gen-go historically generated to be able to automatically wrap some
52// v1 messages generated by other forks of protoc-gen-go.
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053053func NewConverter(t reflect.Type, fd protoreflect.FieldDescriptor) Converter {
Naveen Sampath04696f72022-06-13 15:19:14 +053054 switch {
55 case fd.IsList():
56 return newListConverter(t, fd)
57 case fd.IsMap():
58 return newMapConverter(t, fd)
59 default:
60 return newSingularConverter(t, fd)
61 }
Naveen Sampath04696f72022-06-13 15:19:14 +053062}
63
64var (
65 boolType = reflect.TypeOf(bool(false))
66 int32Type = reflect.TypeOf(int32(0))
67 int64Type = reflect.TypeOf(int64(0))
68 uint32Type = reflect.TypeOf(uint32(0))
69 uint64Type = reflect.TypeOf(uint64(0))
70 float32Type = reflect.TypeOf(float32(0))
71 float64Type = reflect.TypeOf(float64(0))
72 stringType = reflect.TypeOf(string(""))
73 bytesType = reflect.TypeOf([]byte(nil))
74 byteType = reflect.TypeOf(byte(0))
75)
76
77var (
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053078 boolZero = protoreflect.ValueOfBool(false)
79 int32Zero = protoreflect.ValueOfInt32(0)
80 int64Zero = protoreflect.ValueOfInt64(0)
81 uint32Zero = protoreflect.ValueOfUint32(0)
82 uint64Zero = protoreflect.ValueOfUint64(0)
83 float32Zero = protoreflect.ValueOfFloat32(0)
84 float64Zero = protoreflect.ValueOfFloat64(0)
85 stringZero = protoreflect.ValueOfString("")
86 bytesZero = protoreflect.ValueOfBytes(nil)
Naveen Sampath04696f72022-06-13 15:19:14 +053087)
88
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053089func newSingularConverter(t reflect.Type, fd protoreflect.FieldDescriptor) Converter {
90 defVal := func(fd protoreflect.FieldDescriptor, zero protoreflect.Value) protoreflect.Value {
91 if fd.Cardinality() == protoreflect.Repeated {
Naveen Sampath04696f72022-06-13 15:19:14 +053092 // Default isn't defined for repeated fields.
93 return zero
94 }
95 return fd.Default()
96 }
97 switch fd.Kind() {
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +053098 case protoreflect.BoolKind:
Naveen Sampath04696f72022-06-13 15:19:14 +053099 if t.Kind() == reflect.Bool {
100 return &boolConverter{t, defVal(fd, boolZero)}
101 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530102 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
Naveen Sampath04696f72022-06-13 15:19:14 +0530103 if t.Kind() == reflect.Int32 {
104 return &int32Converter{t, defVal(fd, int32Zero)}
105 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530106 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
Naveen Sampath04696f72022-06-13 15:19:14 +0530107 if t.Kind() == reflect.Int64 {
108 return &int64Converter{t, defVal(fd, int64Zero)}
109 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530110 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
Naveen Sampath04696f72022-06-13 15:19:14 +0530111 if t.Kind() == reflect.Uint32 {
112 return &uint32Converter{t, defVal(fd, uint32Zero)}
113 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530114 case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
Naveen Sampath04696f72022-06-13 15:19:14 +0530115 if t.Kind() == reflect.Uint64 {
116 return &uint64Converter{t, defVal(fd, uint64Zero)}
117 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530118 case protoreflect.FloatKind:
Naveen Sampath04696f72022-06-13 15:19:14 +0530119 if t.Kind() == reflect.Float32 {
120 return &float32Converter{t, defVal(fd, float32Zero)}
121 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530122 case protoreflect.DoubleKind:
Naveen Sampath04696f72022-06-13 15:19:14 +0530123 if t.Kind() == reflect.Float64 {
124 return &float64Converter{t, defVal(fd, float64Zero)}
125 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530126 case protoreflect.StringKind:
Naveen Sampath04696f72022-06-13 15:19:14 +0530127 if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
128 return &stringConverter{t, defVal(fd, stringZero)}
129 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530130 case protoreflect.BytesKind:
Naveen Sampath04696f72022-06-13 15:19:14 +0530131 if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
132 return &bytesConverter{t, defVal(fd, bytesZero)}
133 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530134 case protoreflect.EnumKind:
Naveen Sampath04696f72022-06-13 15:19:14 +0530135 // Handle enums, which must be a named int32 type.
136 if t.Kind() == reflect.Int32 {
137 return newEnumConverter(t, fd)
138 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530139 case protoreflect.MessageKind, protoreflect.GroupKind:
Naveen Sampath04696f72022-06-13 15:19:14 +0530140 return newMessageConverter(t)
141 }
142 panic(fmt.Sprintf("invalid Go type %v for field %v", t, fd.FullName()))
143}
144
145type boolConverter struct {
146 goType reflect.Type
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530147 def protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +0530148}
149
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530150func (c *boolConverter) PBValueOf(v reflect.Value) protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530151 if v.Type() != c.goType {
152 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
153 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530154 return protoreflect.ValueOfBool(v.Bool())
Naveen Sampath04696f72022-06-13 15:19:14 +0530155}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530156func (c *boolConverter) GoValueOf(v protoreflect.Value) reflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530157 return reflect.ValueOf(v.Bool()).Convert(c.goType)
158}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530159func (c *boolConverter) IsValidPB(v protoreflect.Value) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530160 _, ok := v.Interface().(bool)
161 return ok
162}
163func (c *boolConverter) IsValidGo(v reflect.Value) bool {
164 return v.IsValid() && v.Type() == c.goType
165}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530166func (c *boolConverter) New() protoreflect.Value { return c.def }
167func (c *boolConverter) Zero() protoreflect.Value { return c.def }
Naveen Sampath04696f72022-06-13 15:19:14 +0530168
169type int32Converter struct {
170 goType reflect.Type
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530171 def protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +0530172}
173
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530174func (c *int32Converter) PBValueOf(v reflect.Value) protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530175 if v.Type() != c.goType {
176 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
177 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530178 return protoreflect.ValueOfInt32(int32(v.Int()))
Naveen Sampath04696f72022-06-13 15:19:14 +0530179}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530180func (c *int32Converter) GoValueOf(v protoreflect.Value) reflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530181 return reflect.ValueOf(int32(v.Int())).Convert(c.goType)
182}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530183func (c *int32Converter) IsValidPB(v protoreflect.Value) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530184 _, ok := v.Interface().(int32)
185 return ok
186}
187func (c *int32Converter) IsValidGo(v reflect.Value) bool {
188 return v.IsValid() && v.Type() == c.goType
189}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530190func (c *int32Converter) New() protoreflect.Value { return c.def }
191func (c *int32Converter) Zero() protoreflect.Value { return c.def }
Naveen Sampath04696f72022-06-13 15:19:14 +0530192
193type int64Converter struct {
194 goType reflect.Type
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530195 def protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +0530196}
197
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530198func (c *int64Converter) PBValueOf(v reflect.Value) protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530199 if v.Type() != c.goType {
200 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
201 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530202 return protoreflect.ValueOfInt64(int64(v.Int()))
Naveen Sampath04696f72022-06-13 15:19:14 +0530203}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530204func (c *int64Converter) GoValueOf(v protoreflect.Value) reflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530205 return reflect.ValueOf(int64(v.Int())).Convert(c.goType)
206}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530207func (c *int64Converter) IsValidPB(v protoreflect.Value) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530208 _, ok := v.Interface().(int64)
209 return ok
210}
211func (c *int64Converter) IsValidGo(v reflect.Value) bool {
212 return v.IsValid() && v.Type() == c.goType
213}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530214func (c *int64Converter) New() protoreflect.Value { return c.def }
215func (c *int64Converter) Zero() protoreflect.Value { return c.def }
Naveen Sampath04696f72022-06-13 15:19:14 +0530216
217type uint32Converter struct {
218 goType reflect.Type
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530219 def protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +0530220}
221
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530222func (c *uint32Converter) PBValueOf(v reflect.Value) protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530223 if v.Type() != c.goType {
224 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
225 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530226 return protoreflect.ValueOfUint32(uint32(v.Uint()))
Naveen Sampath04696f72022-06-13 15:19:14 +0530227}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530228func (c *uint32Converter) GoValueOf(v protoreflect.Value) reflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530229 return reflect.ValueOf(uint32(v.Uint())).Convert(c.goType)
230}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530231func (c *uint32Converter) IsValidPB(v protoreflect.Value) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530232 _, ok := v.Interface().(uint32)
233 return ok
234}
235func (c *uint32Converter) IsValidGo(v reflect.Value) bool {
236 return v.IsValid() && v.Type() == c.goType
237}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530238func (c *uint32Converter) New() protoreflect.Value { return c.def }
239func (c *uint32Converter) Zero() protoreflect.Value { return c.def }
Naveen Sampath04696f72022-06-13 15:19:14 +0530240
241type uint64Converter struct {
242 goType reflect.Type
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530243 def protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +0530244}
245
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530246func (c *uint64Converter) PBValueOf(v reflect.Value) protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530247 if v.Type() != c.goType {
248 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
249 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530250 return protoreflect.ValueOfUint64(uint64(v.Uint()))
Naveen Sampath04696f72022-06-13 15:19:14 +0530251}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530252func (c *uint64Converter) GoValueOf(v protoreflect.Value) reflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530253 return reflect.ValueOf(uint64(v.Uint())).Convert(c.goType)
254}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530255func (c *uint64Converter) IsValidPB(v protoreflect.Value) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530256 _, ok := v.Interface().(uint64)
257 return ok
258}
259func (c *uint64Converter) IsValidGo(v reflect.Value) bool {
260 return v.IsValid() && v.Type() == c.goType
261}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530262func (c *uint64Converter) New() protoreflect.Value { return c.def }
263func (c *uint64Converter) Zero() protoreflect.Value { return c.def }
Naveen Sampath04696f72022-06-13 15:19:14 +0530264
265type float32Converter struct {
266 goType reflect.Type
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530267 def protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +0530268}
269
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530270func (c *float32Converter) PBValueOf(v reflect.Value) protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530271 if v.Type() != c.goType {
272 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
273 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530274 return protoreflect.ValueOfFloat32(float32(v.Float()))
Naveen Sampath04696f72022-06-13 15:19:14 +0530275}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530276func (c *float32Converter) GoValueOf(v protoreflect.Value) reflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530277 return reflect.ValueOf(float32(v.Float())).Convert(c.goType)
278}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530279func (c *float32Converter) IsValidPB(v protoreflect.Value) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530280 _, ok := v.Interface().(float32)
281 return ok
282}
283func (c *float32Converter) IsValidGo(v reflect.Value) bool {
284 return v.IsValid() && v.Type() == c.goType
285}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530286func (c *float32Converter) New() protoreflect.Value { return c.def }
287func (c *float32Converter) Zero() protoreflect.Value { return c.def }
Naveen Sampath04696f72022-06-13 15:19:14 +0530288
289type float64Converter struct {
290 goType reflect.Type
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530291 def protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +0530292}
293
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530294func (c *float64Converter) PBValueOf(v reflect.Value) protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530295 if v.Type() != c.goType {
296 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
297 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530298 return protoreflect.ValueOfFloat64(float64(v.Float()))
Naveen Sampath04696f72022-06-13 15:19:14 +0530299}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530300func (c *float64Converter) GoValueOf(v protoreflect.Value) reflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530301 return reflect.ValueOf(float64(v.Float())).Convert(c.goType)
302}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530303func (c *float64Converter) IsValidPB(v protoreflect.Value) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530304 _, ok := v.Interface().(float64)
305 return ok
306}
307func (c *float64Converter) IsValidGo(v reflect.Value) bool {
308 return v.IsValid() && v.Type() == c.goType
309}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530310func (c *float64Converter) New() protoreflect.Value { return c.def }
311func (c *float64Converter) Zero() protoreflect.Value { return c.def }
Naveen Sampath04696f72022-06-13 15:19:14 +0530312
313type stringConverter struct {
314 goType reflect.Type
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530315 def protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +0530316}
317
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530318func (c *stringConverter) PBValueOf(v reflect.Value) protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530319 if v.Type() != c.goType {
320 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
321 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530322 return protoreflect.ValueOfString(v.Convert(stringType).String())
Naveen Sampath04696f72022-06-13 15:19:14 +0530323}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530324func (c *stringConverter) GoValueOf(v protoreflect.Value) reflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530325 // pref.Value.String never panics, so we go through an interface
326 // conversion here to check the type.
327 s := v.Interface().(string)
328 if c.goType.Kind() == reflect.Slice && s == "" {
329 return reflect.Zero(c.goType) // ensure empty string is []byte(nil)
330 }
331 return reflect.ValueOf(s).Convert(c.goType)
332}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530333func (c *stringConverter) IsValidPB(v protoreflect.Value) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530334 _, ok := v.Interface().(string)
335 return ok
336}
337func (c *stringConverter) IsValidGo(v reflect.Value) bool {
338 return v.IsValid() && v.Type() == c.goType
339}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530340func (c *stringConverter) New() protoreflect.Value { return c.def }
341func (c *stringConverter) Zero() protoreflect.Value { return c.def }
Naveen Sampath04696f72022-06-13 15:19:14 +0530342
343type bytesConverter struct {
344 goType reflect.Type
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530345 def protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +0530346}
347
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530348func (c *bytesConverter) PBValueOf(v reflect.Value) protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530349 if v.Type() != c.goType {
350 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
351 }
352 if c.goType.Kind() == reflect.String && v.Len() == 0 {
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530353 return protoreflect.ValueOfBytes(nil) // ensure empty string is []byte(nil)
Naveen Sampath04696f72022-06-13 15:19:14 +0530354 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530355 return protoreflect.ValueOfBytes(v.Convert(bytesType).Bytes())
Naveen Sampath04696f72022-06-13 15:19:14 +0530356}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530357func (c *bytesConverter) GoValueOf(v protoreflect.Value) reflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530358 return reflect.ValueOf(v.Bytes()).Convert(c.goType)
359}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530360func (c *bytesConverter) IsValidPB(v protoreflect.Value) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530361 _, ok := v.Interface().([]byte)
362 return ok
363}
364func (c *bytesConverter) IsValidGo(v reflect.Value) bool {
365 return v.IsValid() && v.Type() == c.goType
366}
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530367func (c *bytesConverter) New() protoreflect.Value { return c.def }
368func (c *bytesConverter) Zero() protoreflect.Value { return c.def }
Naveen Sampath04696f72022-06-13 15:19:14 +0530369
370type enumConverter struct {
371 goType reflect.Type
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530372 def protoreflect.Value
Naveen Sampath04696f72022-06-13 15:19:14 +0530373}
374
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530375func newEnumConverter(goType reflect.Type, fd protoreflect.FieldDescriptor) Converter {
376 var def protoreflect.Value
377 if fd.Cardinality() == protoreflect.Repeated {
378 def = protoreflect.ValueOfEnum(fd.Enum().Values().Get(0).Number())
Naveen Sampath04696f72022-06-13 15:19:14 +0530379 } else {
380 def = fd.Default()
381 }
382 return &enumConverter{goType, def}
383}
384
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530385func (c *enumConverter) PBValueOf(v reflect.Value) protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530386 if v.Type() != c.goType {
387 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
388 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530389 return protoreflect.ValueOfEnum(protoreflect.EnumNumber(v.Int()))
Naveen Sampath04696f72022-06-13 15:19:14 +0530390}
391
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530392func (c *enumConverter) GoValueOf(v protoreflect.Value) reflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530393 return reflect.ValueOf(v.Enum()).Convert(c.goType)
394}
395
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530396func (c *enumConverter) IsValidPB(v protoreflect.Value) bool {
397 _, ok := v.Interface().(protoreflect.EnumNumber)
Naveen Sampath04696f72022-06-13 15:19:14 +0530398 return ok
399}
400
401func (c *enumConverter) IsValidGo(v reflect.Value) bool {
402 return v.IsValid() && v.Type() == c.goType
403}
404
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530405func (c *enumConverter) New() protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530406 return c.def
407}
408
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530409func (c *enumConverter) Zero() protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530410 return c.def
411}
412
413type messageConverter struct {
414 goType reflect.Type
415}
416
417func newMessageConverter(goType reflect.Type) Converter {
418 return &messageConverter{goType}
419}
420
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530421func (c *messageConverter) PBValueOf(v reflect.Value) protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530422 if v.Type() != c.goType {
423 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
424 }
425 if c.isNonPointer() {
426 if v.CanAddr() {
427 v = v.Addr() // T => *T
428 } else {
429 v = reflect.Zero(reflect.PtrTo(v.Type()))
430 }
431 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530432 if m, ok := v.Interface().(protoreflect.ProtoMessage); ok {
433 return protoreflect.ValueOfMessage(m.ProtoReflect())
Naveen Sampath04696f72022-06-13 15:19:14 +0530434 }
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530435 return protoreflect.ValueOfMessage(legacyWrapMessage(v))
Naveen Sampath04696f72022-06-13 15:19:14 +0530436}
437
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530438func (c *messageConverter) GoValueOf(v protoreflect.Value) reflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530439 m := v.Message()
440 var rv reflect.Value
441 if u, ok := m.(unwrapper); ok {
442 rv = reflect.ValueOf(u.protoUnwrap())
443 } else {
444 rv = reflect.ValueOf(m.Interface())
445 }
446 if c.isNonPointer() {
447 if rv.Type() != reflect.PtrTo(c.goType) {
448 panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), reflect.PtrTo(c.goType)))
449 }
450 if !rv.IsNil() {
451 rv = rv.Elem() // *T => T
452 } else {
453 rv = reflect.Zero(rv.Type().Elem())
454 }
455 }
456 if rv.Type() != c.goType {
457 panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), c.goType))
458 }
459 return rv
460}
461
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530462func (c *messageConverter) IsValidPB(v protoreflect.Value) bool {
Naveen Sampath04696f72022-06-13 15:19:14 +0530463 m := v.Message()
464 var rv reflect.Value
465 if u, ok := m.(unwrapper); ok {
466 rv = reflect.ValueOf(u.protoUnwrap())
467 } else {
468 rv = reflect.ValueOf(m.Interface())
469 }
470 if c.isNonPointer() {
471 return rv.Type() == reflect.PtrTo(c.goType)
472 }
473 return rv.Type() == c.goType
474}
475
476func (c *messageConverter) IsValidGo(v reflect.Value) bool {
477 return v.IsValid() && v.Type() == c.goType
478}
479
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530480func (c *messageConverter) New() protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530481 if c.isNonPointer() {
482 return c.PBValueOf(reflect.New(c.goType).Elem())
483 }
484 return c.PBValueOf(reflect.New(c.goType.Elem()))
485}
486
Akash Reddy Kankanala105581b2024-09-11 05:20:38 +0530487func (c *messageConverter) Zero() protoreflect.Value {
Naveen Sampath04696f72022-06-13 15:19:14 +0530488 return c.PBValueOf(reflect.Zero(c.goType))
489}
490
491// isNonPointer reports whether the type is a non-pointer type.
492// This never occurs for generated message types.
493func (c *messageConverter) isNonPointer() bool {
494 return c.goType.Kind() != reflect.Ptr
495}