blob: f72ddd882f3240122c9a6a2c8956d459e7188d53 [file] [log] [blame]
khenaidoo26721882021-08-11 17:42:52 -04001// 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
Abhay Kumar40252eb2025-10-13 13:25:53 +000011 "google.golang.org/protobuf/reflect/protoreflect"
khenaidoo26721882021-08-11 17:42:52 -040012)
13
14// unwrapper unwraps the value to the underlying value.
15// This is implemented by List and Map.
16type unwrapper interface {
Abhay Kumar40252eb2025-10-13 13:25:53 +000017 protoUnwrap() any
khenaidoo26721882021-08-11 17:42:52 -040018}
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.
Abhay Kumar40252eb2025-10-13 13:25:53 +000023 PBValueOf(reflect.Value) protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -040024
25 // GoValueOf converts a protoreflect.Value to a reflect.Value.
Abhay Kumar40252eb2025-10-13 13:25:53 +000026 GoValueOf(protoreflect.Value) reflect.Value
khenaidoo26721882021-08-11 17:42:52 -040027
28 // IsValidPB returns whether a protoreflect.Value is compatible with this type.
Abhay Kumar40252eb2025-10-13 13:25:53 +000029 IsValidPB(protoreflect.Value) bool
khenaidoo26721882021-08-11 17:42:52 -040030
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.
Abhay Kumar40252eb2025-10-13 13:25:53 +000037 New() protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -040038
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.
Abhay Kumar40252eb2025-10-13 13:25:53 +000042 Zero() protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -040043}
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.
Abhay Kumar40252eb2025-10-13 13:25:53 +000053func NewConverter(t reflect.Type, fd protoreflect.FieldDescriptor) Converter {
khenaidoo26721882021-08-11 17:42:52 -040054 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 }
khenaidoo26721882021-08-11 17:42:52 -040062}
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 (
Abhay Kumar40252eb2025-10-13 13:25:53 +000078 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)
khenaidoo26721882021-08-11 17:42:52 -040087)
88
Abhay Kumar40252eb2025-10-13 13:25:53 +000089func 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 {
khenaidoo26721882021-08-11 17:42:52 -040092 // Default isn't defined for repeated fields.
93 return zero
94 }
95 return fd.Default()
96 }
97 switch fd.Kind() {
Abhay Kumar40252eb2025-10-13 13:25:53 +000098 case protoreflect.BoolKind:
khenaidoo26721882021-08-11 17:42:52 -040099 if t.Kind() == reflect.Bool {
100 return &boolConverter{t, defVal(fd, boolZero)}
101 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000102 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
khenaidoo26721882021-08-11 17:42:52 -0400103 if t.Kind() == reflect.Int32 {
104 return &int32Converter{t, defVal(fd, int32Zero)}
105 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000106 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
khenaidoo26721882021-08-11 17:42:52 -0400107 if t.Kind() == reflect.Int64 {
108 return &int64Converter{t, defVal(fd, int64Zero)}
109 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000110 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
khenaidoo26721882021-08-11 17:42:52 -0400111 if t.Kind() == reflect.Uint32 {
112 return &uint32Converter{t, defVal(fd, uint32Zero)}
113 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000114 case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
khenaidoo26721882021-08-11 17:42:52 -0400115 if t.Kind() == reflect.Uint64 {
116 return &uint64Converter{t, defVal(fd, uint64Zero)}
117 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000118 case protoreflect.FloatKind:
khenaidoo26721882021-08-11 17:42:52 -0400119 if t.Kind() == reflect.Float32 {
120 return &float32Converter{t, defVal(fd, float32Zero)}
121 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000122 case protoreflect.DoubleKind:
khenaidoo26721882021-08-11 17:42:52 -0400123 if t.Kind() == reflect.Float64 {
124 return &float64Converter{t, defVal(fd, float64Zero)}
125 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000126 case protoreflect.StringKind:
khenaidoo26721882021-08-11 17:42:52 -0400127 if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
128 return &stringConverter{t, defVal(fd, stringZero)}
129 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000130 case protoreflect.BytesKind:
khenaidoo26721882021-08-11 17:42:52 -0400131 if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
132 return &bytesConverter{t, defVal(fd, bytesZero)}
133 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000134 case protoreflect.EnumKind:
khenaidoo26721882021-08-11 17:42:52 -0400135 // Handle enums, which must be a named int32 type.
136 if t.Kind() == reflect.Int32 {
137 return newEnumConverter(t, fd)
138 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000139 case protoreflect.MessageKind, protoreflect.GroupKind:
khenaidoo26721882021-08-11 17:42:52 -0400140 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
Abhay Kumar40252eb2025-10-13 13:25:53 +0000147 def protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -0400148}
149
Abhay Kumar40252eb2025-10-13 13:25:53 +0000150func (c *boolConverter) PBValueOf(v reflect.Value) protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400151 if v.Type() != c.goType {
152 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
153 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000154 return protoreflect.ValueOfBool(v.Bool())
khenaidoo26721882021-08-11 17:42:52 -0400155}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000156func (c *boolConverter) GoValueOf(v protoreflect.Value) reflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400157 return reflect.ValueOf(v.Bool()).Convert(c.goType)
158}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000159func (c *boolConverter) IsValidPB(v protoreflect.Value) bool {
khenaidoo26721882021-08-11 17:42:52 -0400160 _, 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}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000166func (c *boolConverter) New() protoreflect.Value { return c.def }
167func (c *boolConverter) Zero() protoreflect.Value { return c.def }
khenaidoo26721882021-08-11 17:42:52 -0400168
169type int32Converter struct {
170 goType reflect.Type
Abhay Kumar40252eb2025-10-13 13:25:53 +0000171 def protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -0400172}
173
Abhay Kumar40252eb2025-10-13 13:25:53 +0000174func (c *int32Converter) PBValueOf(v reflect.Value) protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400175 if v.Type() != c.goType {
176 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
177 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000178 return protoreflect.ValueOfInt32(int32(v.Int()))
khenaidoo26721882021-08-11 17:42:52 -0400179}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000180func (c *int32Converter) GoValueOf(v protoreflect.Value) reflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400181 return reflect.ValueOf(int32(v.Int())).Convert(c.goType)
182}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000183func (c *int32Converter) IsValidPB(v protoreflect.Value) bool {
khenaidoo26721882021-08-11 17:42:52 -0400184 _, 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}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000190func (c *int32Converter) New() protoreflect.Value { return c.def }
191func (c *int32Converter) Zero() protoreflect.Value { return c.def }
khenaidoo26721882021-08-11 17:42:52 -0400192
193type int64Converter struct {
194 goType reflect.Type
Abhay Kumar40252eb2025-10-13 13:25:53 +0000195 def protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -0400196}
197
Abhay Kumar40252eb2025-10-13 13:25:53 +0000198func (c *int64Converter) PBValueOf(v reflect.Value) protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400199 if v.Type() != c.goType {
200 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
201 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000202 return protoreflect.ValueOfInt64(int64(v.Int()))
khenaidoo26721882021-08-11 17:42:52 -0400203}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000204func (c *int64Converter) GoValueOf(v protoreflect.Value) reflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400205 return reflect.ValueOf(int64(v.Int())).Convert(c.goType)
206}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000207func (c *int64Converter) IsValidPB(v protoreflect.Value) bool {
khenaidoo26721882021-08-11 17:42:52 -0400208 _, 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}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000214func (c *int64Converter) New() protoreflect.Value { return c.def }
215func (c *int64Converter) Zero() protoreflect.Value { return c.def }
khenaidoo26721882021-08-11 17:42:52 -0400216
217type uint32Converter struct {
218 goType reflect.Type
Abhay Kumar40252eb2025-10-13 13:25:53 +0000219 def protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -0400220}
221
Abhay Kumar40252eb2025-10-13 13:25:53 +0000222func (c *uint32Converter) PBValueOf(v reflect.Value) protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400223 if v.Type() != c.goType {
224 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
225 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000226 return protoreflect.ValueOfUint32(uint32(v.Uint()))
khenaidoo26721882021-08-11 17:42:52 -0400227}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000228func (c *uint32Converter) GoValueOf(v protoreflect.Value) reflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400229 return reflect.ValueOf(uint32(v.Uint())).Convert(c.goType)
230}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000231func (c *uint32Converter) IsValidPB(v protoreflect.Value) bool {
khenaidoo26721882021-08-11 17:42:52 -0400232 _, 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}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000238func (c *uint32Converter) New() protoreflect.Value { return c.def }
239func (c *uint32Converter) Zero() protoreflect.Value { return c.def }
khenaidoo26721882021-08-11 17:42:52 -0400240
241type uint64Converter struct {
242 goType reflect.Type
Abhay Kumar40252eb2025-10-13 13:25:53 +0000243 def protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -0400244}
245
Abhay Kumar40252eb2025-10-13 13:25:53 +0000246func (c *uint64Converter) PBValueOf(v reflect.Value) protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400247 if v.Type() != c.goType {
248 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
249 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000250 return protoreflect.ValueOfUint64(uint64(v.Uint()))
khenaidoo26721882021-08-11 17:42:52 -0400251}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000252func (c *uint64Converter) GoValueOf(v protoreflect.Value) reflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400253 return reflect.ValueOf(uint64(v.Uint())).Convert(c.goType)
254}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000255func (c *uint64Converter) IsValidPB(v protoreflect.Value) bool {
khenaidoo26721882021-08-11 17:42:52 -0400256 _, 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}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000262func (c *uint64Converter) New() protoreflect.Value { return c.def }
263func (c *uint64Converter) Zero() protoreflect.Value { return c.def }
khenaidoo26721882021-08-11 17:42:52 -0400264
265type float32Converter struct {
266 goType reflect.Type
Abhay Kumar40252eb2025-10-13 13:25:53 +0000267 def protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -0400268}
269
Abhay Kumar40252eb2025-10-13 13:25:53 +0000270func (c *float32Converter) PBValueOf(v reflect.Value) protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400271 if v.Type() != c.goType {
272 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
273 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000274 return protoreflect.ValueOfFloat32(float32(v.Float()))
khenaidoo26721882021-08-11 17:42:52 -0400275}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000276func (c *float32Converter) GoValueOf(v protoreflect.Value) reflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400277 return reflect.ValueOf(float32(v.Float())).Convert(c.goType)
278}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000279func (c *float32Converter) IsValidPB(v protoreflect.Value) bool {
khenaidoo26721882021-08-11 17:42:52 -0400280 _, 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}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000286func (c *float32Converter) New() protoreflect.Value { return c.def }
287func (c *float32Converter) Zero() protoreflect.Value { return c.def }
khenaidoo26721882021-08-11 17:42:52 -0400288
289type float64Converter struct {
290 goType reflect.Type
Abhay Kumar40252eb2025-10-13 13:25:53 +0000291 def protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -0400292}
293
Abhay Kumar40252eb2025-10-13 13:25:53 +0000294func (c *float64Converter) PBValueOf(v reflect.Value) protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400295 if v.Type() != c.goType {
296 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
297 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000298 return protoreflect.ValueOfFloat64(float64(v.Float()))
khenaidoo26721882021-08-11 17:42:52 -0400299}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000300func (c *float64Converter) GoValueOf(v protoreflect.Value) reflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400301 return reflect.ValueOf(float64(v.Float())).Convert(c.goType)
302}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000303func (c *float64Converter) IsValidPB(v protoreflect.Value) bool {
khenaidoo26721882021-08-11 17:42:52 -0400304 _, 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}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000310func (c *float64Converter) New() protoreflect.Value { return c.def }
311func (c *float64Converter) Zero() protoreflect.Value { return c.def }
khenaidoo26721882021-08-11 17:42:52 -0400312
313type stringConverter struct {
314 goType reflect.Type
Abhay Kumar40252eb2025-10-13 13:25:53 +0000315 def protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -0400316}
317
Abhay Kumar40252eb2025-10-13 13:25:53 +0000318func (c *stringConverter) PBValueOf(v reflect.Value) protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400319 if v.Type() != c.goType {
320 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
321 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000322 return protoreflect.ValueOfString(v.Convert(stringType).String())
khenaidoo26721882021-08-11 17:42:52 -0400323}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000324func (c *stringConverter) GoValueOf(v protoreflect.Value) reflect.Value {
325 // protoreflect.Value.String never panics, so we go through an interface
khenaidoo26721882021-08-11 17:42:52 -0400326 // 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}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000333func (c *stringConverter) IsValidPB(v protoreflect.Value) bool {
khenaidoo26721882021-08-11 17:42:52 -0400334 _, 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}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000340func (c *stringConverter) New() protoreflect.Value { return c.def }
341func (c *stringConverter) Zero() protoreflect.Value { return c.def }
khenaidoo26721882021-08-11 17:42:52 -0400342
343type bytesConverter struct {
344 goType reflect.Type
Abhay Kumar40252eb2025-10-13 13:25:53 +0000345 def protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -0400346}
347
Abhay Kumar40252eb2025-10-13 13:25:53 +0000348func (c *bytesConverter) PBValueOf(v reflect.Value) protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400349 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 {
Abhay Kumar40252eb2025-10-13 13:25:53 +0000353 return protoreflect.ValueOfBytes(nil) // ensure empty string is []byte(nil)
khenaidoo26721882021-08-11 17:42:52 -0400354 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000355 return protoreflect.ValueOfBytes(v.Convert(bytesType).Bytes())
khenaidoo26721882021-08-11 17:42:52 -0400356}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000357func (c *bytesConverter) GoValueOf(v protoreflect.Value) reflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400358 return reflect.ValueOf(v.Bytes()).Convert(c.goType)
359}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000360func (c *bytesConverter) IsValidPB(v protoreflect.Value) bool {
khenaidoo26721882021-08-11 17:42:52 -0400361 _, 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}
Abhay Kumar40252eb2025-10-13 13:25:53 +0000367func (c *bytesConverter) New() protoreflect.Value { return c.def }
368func (c *bytesConverter) Zero() protoreflect.Value { return c.def }
khenaidoo26721882021-08-11 17:42:52 -0400369
370type enumConverter struct {
371 goType reflect.Type
Abhay Kumar40252eb2025-10-13 13:25:53 +0000372 def protoreflect.Value
khenaidoo26721882021-08-11 17:42:52 -0400373}
374
Abhay Kumar40252eb2025-10-13 13:25:53 +0000375func 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())
khenaidoo26721882021-08-11 17:42:52 -0400379 } else {
380 def = fd.Default()
381 }
382 return &enumConverter{goType, def}
383}
384
Abhay Kumar40252eb2025-10-13 13:25:53 +0000385func (c *enumConverter) PBValueOf(v reflect.Value) protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400386 if v.Type() != c.goType {
387 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
388 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000389 return protoreflect.ValueOfEnum(protoreflect.EnumNumber(v.Int()))
khenaidoo26721882021-08-11 17:42:52 -0400390}
391
Abhay Kumar40252eb2025-10-13 13:25:53 +0000392func (c *enumConverter) GoValueOf(v protoreflect.Value) reflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400393 return reflect.ValueOf(v.Enum()).Convert(c.goType)
394}
395
Abhay Kumar40252eb2025-10-13 13:25:53 +0000396func (c *enumConverter) IsValidPB(v protoreflect.Value) bool {
397 _, ok := v.Interface().(protoreflect.EnumNumber)
khenaidoo26721882021-08-11 17:42:52 -0400398 return ok
399}
400
401func (c *enumConverter) IsValidGo(v reflect.Value) bool {
402 return v.IsValid() && v.Type() == c.goType
403}
404
Abhay Kumar40252eb2025-10-13 13:25:53 +0000405func (c *enumConverter) New() protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400406 return c.def
407}
408
Abhay Kumar40252eb2025-10-13 13:25:53 +0000409func (c *enumConverter) Zero() protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400410 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
Abhay Kumar40252eb2025-10-13 13:25:53 +0000421func (c *messageConverter) PBValueOf(v reflect.Value) protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400422 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 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000432 if m, ok := v.Interface().(protoreflect.ProtoMessage); ok {
433 return protoreflect.ValueOfMessage(m.ProtoReflect())
khenaidoo26721882021-08-11 17:42:52 -0400434 }
Abhay Kumar40252eb2025-10-13 13:25:53 +0000435 return protoreflect.ValueOfMessage(legacyWrapMessage(v))
khenaidoo26721882021-08-11 17:42:52 -0400436}
437
Abhay Kumar40252eb2025-10-13 13:25:53 +0000438func (c *messageConverter) GoValueOf(v protoreflect.Value) reflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400439 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
Abhay Kumar40252eb2025-10-13 13:25:53 +0000462func (c *messageConverter) IsValidPB(v protoreflect.Value) bool {
khenaidoo26721882021-08-11 17:42:52 -0400463 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
Abhay Kumar40252eb2025-10-13 13:25:53 +0000480func (c *messageConverter) New() protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400481 if c.isNonPointer() {
482 return c.PBValueOf(reflect.New(c.goType).Elem())
483 }
484 return c.PBValueOf(reflect.New(c.goType.Elem()))
485}
486
Abhay Kumar40252eb2025-10-13 13:25:53 +0000487func (c *messageConverter) Zero() protoreflect.Value {
khenaidoo26721882021-08-11 17:42:52 -0400488 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}