74 bytesType = reflect.TypeOf([]byte(nil)) |
74 bytesType = reflect.TypeOf([]byte(nil)) |
75 byteType = reflect.TypeOf(byte(0)) |
75 byteType = reflect.TypeOf(byte(0)) |
76 ) |
76 ) |
77 |
77 |
78 var ( |
78 var ( |
79 boolZero = pref.ValueOfBool(false) |
79 boolZero = protoreflect.ValueOfBool(false) |
80 int32Zero = pref.ValueOfInt32(0) |
80 int32Zero = protoreflect.ValueOfInt32(0) |
81 int64Zero = pref.ValueOfInt64(0) |
81 int64Zero = protoreflect.ValueOfInt64(0) |
82 uint32Zero = pref.ValueOfUint32(0) |
82 uint32Zero = protoreflect.ValueOfUint32(0) |
83 uint64Zero = pref.ValueOfUint64(0) |
83 uint64Zero = protoreflect.ValueOfUint64(0) |
84 float32Zero = pref.ValueOfFloat32(0) |
84 float32Zero = protoreflect.ValueOfFloat32(0) |
85 float64Zero = pref.ValueOfFloat64(0) |
85 float64Zero = protoreflect.ValueOfFloat64(0) |
86 stringZero = pref.ValueOfString("") |
86 stringZero = protoreflect.ValueOfString("") |
87 bytesZero = pref.ValueOfBytes(nil) |
87 bytesZero = protoreflect.ValueOfBytes(nil) |
88 ) |
88 ) |
89 |
89 |
90 func newSingularConverter(t reflect.Type, fd pref.FieldDescriptor) Converter { |
90 func newSingularConverter(t reflect.Type, fd protoreflect.FieldDescriptor) Converter { |
91 defVal := func(fd pref.FieldDescriptor, zero pref.Value) pref.Value { |
91 defVal := func(fd protoreflect.FieldDescriptor, zero protoreflect.Value) protoreflect.Value { |
92 if fd.Cardinality() == pref.Repeated { |
92 if fd.Cardinality() == protoreflect.Repeated { |
93 // Default isn't defined for repeated fields. |
93 // Default isn't defined for repeated fields. |
94 return zero |
94 return zero |
95 } |
95 } |
96 return fd.Default() |
96 return fd.Default() |
97 } |
97 } |
98 switch fd.Kind() { |
98 switch fd.Kind() { |
99 case pref.BoolKind: |
99 case protoreflect.BoolKind: |
100 if t.Kind() == reflect.Bool { |
100 if t.Kind() == reflect.Bool { |
101 return &boolConverter{t, defVal(fd, boolZero)} |
101 return &boolConverter{t, defVal(fd, boolZero)} |
102 } |
102 } |
103 case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind: |
103 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: |
104 if t.Kind() == reflect.Int32 { |
104 if t.Kind() == reflect.Int32 { |
105 return &int32Converter{t, defVal(fd, int32Zero)} |
105 return &int32Converter{t, defVal(fd, int32Zero)} |
106 } |
106 } |
107 case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind: |
107 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: |
108 if t.Kind() == reflect.Int64 { |
108 if t.Kind() == reflect.Int64 { |
109 return &int64Converter{t, defVal(fd, int64Zero)} |
109 return &int64Converter{t, defVal(fd, int64Zero)} |
110 } |
110 } |
111 case pref.Uint32Kind, pref.Fixed32Kind: |
111 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: |
112 if t.Kind() == reflect.Uint32 { |
112 if t.Kind() == reflect.Uint32 { |
113 return &uint32Converter{t, defVal(fd, uint32Zero)} |
113 return &uint32Converter{t, defVal(fd, uint32Zero)} |
114 } |
114 } |
115 case pref.Uint64Kind, pref.Fixed64Kind: |
115 case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: |
116 if t.Kind() == reflect.Uint64 { |
116 if t.Kind() == reflect.Uint64 { |
117 return &uint64Converter{t, defVal(fd, uint64Zero)} |
117 return &uint64Converter{t, defVal(fd, uint64Zero)} |
118 } |
118 } |
119 case pref.FloatKind: |
119 case protoreflect.FloatKind: |
120 if t.Kind() == reflect.Float32 { |
120 if t.Kind() == reflect.Float32 { |
121 return &float32Converter{t, defVal(fd, float32Zero)} |
121 return &float32Converter{t, defVal(fd, float32Zero)} |
122 } |
122 } |
123 case pref.DoubleKind: |
123 case protoreflect.DoubleKind: |
124 if t.Kind() == reflect.Float64 { |
124 if t.Kind() == reflect.Float64 { |
125 return &float64Converter{t, defVal(fd, float64Zero)} |
125 return &float64Converter{t, defVal(fd, float64Zero)} |
126 } |
126 } |
127 case pref.StringKind: |
127 case protoreflect.StringKind: |
128 if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) { |
128 if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) { |
129 return &stringConverter{t, defVal(fd, stringZero)} |
129 return &stringConverter{t, defVal(fd, stringZero)} |
130 } |
130 } |
131 case pref.BytesKind: |
131 case protoreflect.BytesKind: |
132 if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) { |
132 if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) { |
133 return &bytesConverter{t, defVal(fd, bytesZero)} |
133 return &bytesConverter{t, defVal(fd, bytesZero)} |
134 } |
134 } |
135 case pref.EnumKind: |
135 case protoreflect.EnumKind: |
136 // Handle enums, which must be a named int32 type. |
136 // Handle enums, which must be a named int32 type. |
137 if t.Kind() == reflect.Int32 { |
137 if t.Kind() == reflect.Int32 { |
138 return newEnumConverter(t, fd) |
138 return newEnumConverter(t, fd) |
139 } |
139 } |
140 case pref.MessageKind, pref.GroupKind: |
140 case protoreflect.MessageKind, protoreflect.GroupKind: |
141 return newMessageConverter(t) |
141 return newMessageConverter(t) |
142 } |
142 } |
143 panic(fmt.Sprintf("invalid Go type %v for field %v", t, fd.FullName())) |
143 panic(fmt.Sprintf("invalid Go type %v for field %v", t, fd.FullName())) |
144 } |
144 } |
145 |
145 |
146 type boolConverter struct { |
146 type boolConverter struct { |
147 goType reflect.Type |
147 goType reflect.Type |
148 def pref.Value |
148 def protoreflect.Value |
149 } |
149 } |
150 |
150 |
151 func (c *boolConverter) PBValueOf(v reflect.Value) pref.Value { |
151 func (c *boolConverter) PBValueOf(v reflect.Value) protoreflect.Value { |
152 if v.Type() != c.goType { |
152 if v.Type() != c.goType { |
153 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
153 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
154 } |
154 } |
155 return pref.ValueOfBool(v.Bool()) |
155 return protoreflect.ValueOfBool(v.Bool()) |
156 } |
156 } |
157 func (c *boolConverter) GoValueOf(v pref.Value) reflect.Value { |
157 func (c *boolConverter) GoValueOf(v protoreflect.Value) reflect.Value { |
158 return reflect.ValueOf(v.Bool()).Convert(c.goType) |
158 return reflect.ValueOf(v.Bool()).Convert(c.goType) |
159 } |
159 } |
160 func (c *boolConverter) IsValidPB(v pref.Value) bool { |
160 func (c *boolConverter) IsValidPB(v protoreflect.Value) bool { |
161 _, ok := v.Interface().(bool) |
161 _, ok := v.Interface().(bool) |
162 return ok |
162 return ok |
163 } |
163 } |
164 func (c *boolConverter) IsValidGo(v reflect.Value) bool { |
164 func (c *boolConverter) IsValidGo(v reflect.Value) bool { |
165 return v.IsValid() && v.Type() == c.goType |
165 return v.IsValid() && v.Type() == c.goType |
166 } |
166 } |
167 func (c *boolConverter) New() pref.Value { return c.def } |
167 func (c *boolConverter) New() protoreflect.Value { return c.def } |
168 func (c *boolConverter) Zero() pref.Value { return c.def } |
168 func (c *boolConverter) Zero() protoreflect.Value { return c.def } |
169 |
169 |
170 type int32Converter struct { |
170 type int32Converter struct { |
171 goType reflect.Type |
171 goType reflect.Type |
172 def pref.Value |
172 def protoreflect.Value |
173 } |
173 } |
174 |
174 |
175 func (c *int32Converter) PBValueOf(v reflect.Value) pref.Value { |
175 func (c *int32Converter) PBValueOf(v reflect.Value) protoreflect.Value { |
176 if v.Type() != c.goType { |
176 if v.Type() != c.goType { |
177 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
177 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
178 } |
178 } |
179 return pref.ValueOfInt32(int32(v.Int())) |
179 return protoreflect.ValueOfInt32(int32(v.Int())) |
180 } |
180 } |
181 func (c *int32Converter) GoValueOf(v pref.Value) reflect.Value { |
181 func (c *int32Converter) GoValueOf(v protoreflect.Value) reflect.Value { |
182 return reflect.ValueOf(int32(v.Int())).Convert(c.goType) |
182 return reflect.ValueOf(int32(v.Int())).Convert(c.goType) |
183 } |
183 } |
184 func (c *int32Converter) IsValidPB(v pref.Value) bool { |
184 func (c *int32Converter) IsValidPB(v protoreflect.Value) bool { |
185 _, ok := v.Interface().(int32) |
185 _, ok := v.Interface().(int32) |
186 return ok |
186 return ok |
187 } |
187 } |
188 func (c *int32Converter) IsValidGo(v reflect.Value) bool { |
188 func (c *int32Converter) IsValidGo(v reflect.Value) bool { |
189 return v.IsValid() && v.Type() == c.goType |
189 return v.IsValid() && v.Type() == c.goType |
190 } |
190 } |
191 func (c *int32Converter) New() pref.Value { return c.def } |
191 func (c *int32Converter) New() protoreflect.Value { return c.def } |
192 func (c *int32Converter) Zero() pref.Value { return c.def } |
192 func (c *int32Converter) Zero() protoreflect.Value { return c.def } |
193 |
193 |
194 type int64Converter struct { |
194 type int64Converter struct { |
195 goType reflect.Type |
195 goType reflect.Type |
196 def pref.Value |
196 def protoreflect.Value |
197 } |
197 } |
198 |
198 |
199 func (c *int64Converter) PBValueOf(v reflect.Value) pref.Value { |
199 func (c *int64Converter) PBValueOf(v reflect.Value) protoreflect.Value { |
200 if v.Type() != c.goType { |
200 if v.Type() != c.goType { |
201 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
201 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
202 } |
202 } |
203 return pref.ValueOfInt64(int64(v.Int())) |
203 return protoreflect.ValueOfInt64(int64(v.Int())) |
204 } |
204 } |
205 func (c *int64Converter) GoValueOf(v pref.Value) reflect.Value { |
205 func (c *int64Converter) GoValueOf(v protoreflect.Value) reflect.Value { |
206 return reflect.ValueOf(int64(v.Int())).Convert(c.goType) |
206 return reflect.ValueOf(int64(v.Int())).Convert(c.goType) |
207 } |
207 } |
208 func (c *int64Converter) IsValidPB(v pref.Value) bool { |
208 func (c *int64Converter) IsValidPB(v protoreflect.Value) bool { |
209 _, ok := v.Interface().(int64) |
209 _, ok := v.Interface().(int64) |
210 return ok |
210 return ok |
211 } |
211 } |
212 func (c *int64Converter) IsValidGo(v reflect.Value) bool { |
212 func (c *int64Converter) IsValidGo(v reflect.Value) bool { |
213 return v.IsValid() && v.Type() == c.goType |
213 return v.IsValid() && v.Type() == c.goType |
214 } |
214 } |
215 func (c *int64Converter) New() pref.Value { return c.def } |
215 func (c *int64Converter) New() protoreflect.Value { return c.def } |
216 func (c *int64Converter) Zero() pref.Value { return c.def } |
216 func (c *int64Converter) Zero() protoreflect.Value { return c.def } |
217 |
217 |
218 type uint32Converter struct { |
218 type uint32Converter struct { |
219 goType reflect.Type |
219 goType reflect.Type |
220 def pref.Value |
220 def protoreflect.Value |
221 } |
221 } |
222 |
222 |
223 func (c *uint32Converter) PBValueOf(v reflect.Value) pref.Value { |
223 func (c *uint32Converter) PBValueOf(v reflect.Value) protoreflect.Value { |
224 if v.Type() != c.goType { |
224 if v.Type() != c.goType { |
225 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
225 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
226 } |
226 } |
227 return pref.ValueOfUint32(uint32(v.Uint())) |
227 return protoreflect.ValueOfUint32(uint32(v.Uint())) |
228 } |
228 } |
229 func (c *uint32Converter) GoValueOf(v pref.Value) reflect.Value { |
229 func (c *uint32Converter) GoValueOf(v protoreflect.Value) reflect.Value { |
230 return reflect.ValueOf(uint32(v.Uint())).Convert(c.goType) |
230 return reflect.ValueOf(uint32(v.Uint())).Convert(c.goType) |
231 } |
231 } |
232 func (c *uint32Converter) IsValidPB(v pref.Value) bool { |
232 func (c *uint32Converter) IsValidPB(v protoreflect.Value) bool { |
233 _, ok := v.Interface().(uint32) |
233 _, ok := v.Interface().(uint32) |
234 return ok |
234 return ok |
235 } |
235 } |
236 func (c *uint32Converter) IsValidGo(v reflect.Value) bool { |
236 func (c *uint32Converter) IsValidGo(v reflect.Value) bool { |
237 return v.IsValid() && v.Type() == c.goType |
237 return v.IsValid() && v.Type() == c.goType |
238 } |
238 } |
239 func (c *uint32Converter) New() pref.Value { return c.def } |
239 func (c *uint32Converter) New() protoreflect.Value { return c.def } |
240 func (c *uint32Converter) Zero() pref.Value { return c.def } |
240 func (c *uint32Converter) Zero() protoreflect.Value { return c.def } |
241 |
241 |
242 type uint64Converter struct { |
242 type uint64Converter struct { |
243 goType reflect.Type |
243 goType reflect.Type |
244 def pref.Value |
244 def protoreflect.Value |
245 } |
245 } |
246 |
246 |
247 func (c *uint64Converter) PBValueOf(v reflect.Value) pref.Value { |
247 func (c *uint64Converter) PBValueOf(v reflect.Value) protoreflect.Value { |
248 if v.Type() != c.goType { |
248 if v.Type() != c.goType { |
249 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
249 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
250 } |
250 } |
251 return pref.ValueOfUint64(uint64(v.Uint())) |
251 return protoreflect.ValueOfUint64(uint64(v.Uint())) |
252 } |
252 } |
253 func (c *uint64Converter) GoValueOf(v pref.Value) reflect.Value { |
253 func (c *uint64Converter) GoValueOf(v protoreflect.Value) reflect.Value { |
254 return reflect.ValueOf(uint64(v.Uint())).Convert(c.goType) |
254 return reflect.ValueOf(uint64(v.Uint())).Convert(c.goType) |
255 } |
255 } |
256 func (c *uint64Converter) IsValidPB(v pref.Value) bool { |
256 func (c *uint64Converter) IsValidPB(v protoreflect.Value) bool { |
257 _, ok := v.Interface().(uint64) |
257 _, ok := v.Interface().(uint64) |
258 return ok |
258 return ok |
259 } |
259 } |
260 func (c *uint64Converter) IsValidGo(v reflect.Value) bool { |
260 func (c *uint64Converter) IsValidGo(v reflect.Value) bool { |
261 return v.IsValid() && v.Type() == c.goType |
261 return v.IsValid() && v.Type() == c.goType |
262 } |
262 } |
263 func (c *uint64Converter) New() pref.Value { return c.def } |
263 func (c *uint64Converter) New() protoreflect.Value { return c.def } |
264 func (c *uint64Converter) Zero() pref.Value { return c.def } |
264 func (c *uint64Converter) Zero() protoreflect.Value { return c.def } |
265 |
265 |
266 type float32Converter struct { |
266 type float32Converter struct { |
267 goType reflect.Type |
267 goType reflect.Type |
268 def pref.Value |
268 def protoreflect.Value |
269 } |
269 } |
270 |
270 |
271 func (c *float32Converter) PBValueOf(v reflect.Value) pref.Value { |
271 func (c *float32Converter) PBValueOf(v reflect.Value) protoreflect.Value { |
272 if v.Type() != c.goType { |
272 if v.Type() != c.goType { |
273 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
273 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
274 } |
274 } |
275 return pref.ValueOfFloat32(float32(v.Float())) |
275 return protoreflect.ValueOfFloat32(float32(v.Float())) |
276 } |
276 } |
277 func (c *float32Converter) GoValueOf(v pref.Value) reflect.Value { |
277 func (c *float32Converter) GoValueOf(v protoreflect.Value) reflect.Value { |
278 return reflect.ValueOf(float32(v.Float())).Convert(c.goType) |
278 return reflect.ValueOf(float32(v.Float())).Convert(c.goType) |
279 } |
279 } |
280 func (c *float32Converter) IsValidPB(v pref.Value) bool { |
280 func (c *float32Converter) IsValidPB(v protoreflect.Value) bool { |
281 _, ok := v.Interface().(float32) |
281 _, ok := v.Interface().(float32) |
282 return ok |
282 return ok |
283 } |
283 } |
284 func (c *float32Converter) IsValidGo(v reflect.Value) bool { |
284 func (c *float32Converter) IsValidGo(v reflect.Value) bool { |
285 return v.IsValid() && v.Type() == c.goType |
285 return v.IsValid() && v.Type() == c.goType |
286 } |
286 } |
287 func (c *float32Converter) New() pref.Value { return c.def } |
287 func (c *float32Converter) New() protoreflect.Value { return c.def } |
288 func (c *float32Converter) Zero() pref.Value { return c.def } |
288 func (c *float32Converter) Zero() protoreflect.Value { return c.def } |
289 |
289 |
290 type float64Converter struct { |
290 type float64Converter struct { |
291 goType reflect.Type |
291 goType reflect.Type |
292 def pref.Value |
292 def protoreflect.Value |
293 } |
293 } |
294 |
294 |
295 func (c *float64Converter) PBValueOf(v reflect.Value) pref.Value { |
295 func (c *float64Converter) PBValueOf(v reflect.Value) protoreflect.Value { |
296 if v.Type() != c.goType { |
296 if v.Type() != c.goType { |
297 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
297 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
298 } |
298 } |
299 return pref.ValueOfFloat64(float64(v.Float())) |
299 return protoreflect.ValueOfFloat64(float64(v.Float())) |
300 } |
300 } |
301 func (c *float64Converter) GoValueOf(v pref.Value) reflect.Value { |
301 func (c *float64Converter) GoValueOf(v protoreflect.Value) reflect.Value { |
302 return reflect.ValueOf(float64(v.Float())).Convert(c.goType) |
302 return reflect.ValueOf(float64(v.Float())).Convert(c.goType) |
303 } |
303 } |
304 func (c *float64Converter) IsValidPB(v pref.Value) bool { |
304 func (c *float64Converter) IsValidPB(v protoreflect.Value) bool { |
305 _, ok := v.Interface().(float64) |
305 _, ok := v.Interface().(float64) |
306 return ok |
306 return ok |
307 } |
307 } |
308 func (c *float64Converter) IsValidGo(v reflect.Value) bool { |
308 func (c *float64Converter) IsValidGo(v reflect.Value) bool { |
309 return v.IsValid() && v.Type() == c.goType |
309 return v.IsValid() && v.Type() == c.goType |
310 } |
310 } |
311 func (c *float64Converter) New() pref.Value { return c.def } |
311 func (c *float64Converter) New() protoreflect.Value { return c.def } |
312 func (c *float64Converter) Zero() pref.Value { return c.def } |
312 func (c *float64Converter) Zero() protoreflect.Value { return c.def } |
313 |
313 |
314 type stringConverter struct { |
314 type stringConverter struct { |
315 goType reflect.Type |
315 goType reflect.Type |
316 def pref.Value |
316 def protoreflect.Value |
317 } |
317 } |
318 |
318 |
319 func (c *stringConverter) PBValueOf(v reflect.Value) pref.Value { |
319 func (c *stringConverter) PBValueOf(v reflect.Value) protoreflect.Value { |
320 if v.Type() != c.goType { |
320 if v.Type() != c.goType { |
321 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
321 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
322 } |
322 } |
323 return pref.ValueOfString(v.Convert(stringType).String()) |
323 return protoreflect.ValueOfString(v.Convert(stringType).String()) |
324 } |
324 } |
325 func (c *stringConverter) GoValueOf(v pref.Value) reflect.Value { |
325 func (c *stringConverter) GoValueOf(v protoreflect.Value) reflect.Value { |
326 // pref.Value.String never panics, so we go through an interface |
326 // pref.Value.String never panics, so we go through an interface |
327 // conversion here to check the type. |
327 // conversion here to check the type. |
328 s := v.Interface().(string) |
328 s := v.Interface().(string) |
329 if c.goType.Kind() == reflect.Slice && s == "" { |
329 if c.goType.Kind() == reflect.Slice && s == "" { |
330 return reflect.Zero(c.goType) // ensure empty string is []byte(nil) |
330 return reflect.Zero(c.goType) // ensure empty string is []byte(nil) |
331 } |
331 } |
332 return reflect.ValueOf(s).Convert(c.goType) |
332 return reflect.ValueOf(s).Convert(c.goType) |
333 } |
333 } |
334 func (c *stringConverter) IsValidPB(v pref.Value) bool { |
334 func (c *stringConverter) IsValidPB(v protoreflect.Value) bool { |
335 _, ok := v.Interface().(string) |
335 _, ok := v.Interface().(string) |
336 return ok |
336 return ok |
337 } |
337 } |
338 func (c *stringConverter) IsValidGo(v reflect.Value) bool { |
338 func (c *stringConverter) IsValidGo(v reflect.Value) bool { |
339 return v.IsValid() && v.Type() == c.goType |
339 return v.IsValid() && v.Type() == c.goType |
340 } |
340 } |
341 func (c *stringConverter) New() pref.Value { return c.def } |
341 func (c *stringConverter) New() protoreflect.Value { return c.def } |
342 func (c *stringConverter) Zero() pref.Value { return c.def } |
342 func (c *stringConverter) Zero() protoreflect.Value { return c.def } |
343 |
343 |
344 type bytesConverter struct { |
344 type bytesConverter struct { |
345 goType reflect.Type |
345 goType reflect.Type |
346 def pref.Value |
346 def protoreflect.Value |
347 } |
347 } |
348 |
348 |
349 func (c *bytesConverter) PBValueOf(v reflect.Value) pref.Value { |
349 func (c *bytesConverter) PBValueOf(v reflect.Value) protoreflect.Value { |
350 if v.Type() != c.goType { |
350 if v.Type() != c.goType { |
351 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
351 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
352 } |
352 } |
353 if c.goType.Kind() == reflect.String && v.Len() == 0 { |
353 if c.goType.Kind() == reflect.String && v.Len() == 0 { |
354 return pref.ValueOfBytes(nil) // ensure empty string is []byte(nil) |
354 return protoreflect.ValueOfBytes(nil) // ensure empty string is []byte(nil) |
355 } |
355 } |
356 return pref.ValueOfBytes(v.Convert(bytesType).Bytes()) |
356 return protoreflect.ValueOfBytes(v.Convert(bytesType).Bytes()) |
357 } |
357 } |
358 func (c *bytesConverter) GoValueOf(v pref.Value) reflect.Value { |
358 func (c *bytesConverter) GoValueOf(v protoreflect.Value) reflect.Value { |
359 return reflect.ValueOf(v.Bytes()).Convert(c.goType) |
359 return reflect.ValueOf(v.Bytes()).Convert(c.goType) |
360 } |
360 } |
361 func (c *bytesConverter) IsValidPB(v pref.Value) bool { |
361 func (c *bytesConverter) IsValidPB(v protoreflect.Value) bool { |
362 _, ok := v.Interface().([]byte) |
362 _, ok := v.Interface().([]byte) |
363 return ok |
363 return ok |
364 } |
364 } |
365 func (c *bytesConverter) IsValidGo(v reflect.Value) bool { |
365 func (c *bytesConverter) IsValidGo(v reflect.Value) bool { |
366 return v.IsValid() && v.Type() == c.goType |
366 return v.IsValid() && v.Type() == c.goType |
367 } |
367 } |
368 func (c *bytesConverter) New() pref.Value { return c.def } |
368 func (c *bytesConverter) New() protoreflect.Value { return c.def } |
369 func (c *bytesConverter) Zero() pref.Value { return c.def } |
369 func (c *bytesConverter) Zero() protoreflect.Value { return c.def } |
370 |
370 |
371 type enumConverter struct { |
371 type enumConverter struct { |
372 goType reflect.Type |
372 goType reflect.Type |
373 def pref.Value |
373 def protoreflect.Value |
374 } |
374 } |
375 |
375 |
376 func newEnumConverter(goType reflect.Type, fd pref.FieldDescriptor) Converter { |
376 func newEnumConverter(goType reflect.Type, fd protoreflect.FieldDescriptor) Converter { |
377 var def pref.Value |
377 var def protoreflect.Value |
378 if fd.Cardinality() == pref.Repeated { |
378 if fd.Cardinality() == protoreflect.Repeated { |
379 def = pref.ValueOfEnum(fd.Enum().Values().Get(0).Number()) |
379 def = protoreflect.ValueOfEnum(fd.Enum().Values().Get(0).Number()) |
380 } else { |
380 } else { |
381 def = fd.Default() |
381 def = fd.Default() |
382 } |
382 } |
383 return &enumConverter{goType, def} |
383 return &enumConverter{goType, def} |
384 } |
384 } |
385 |
385 |
386 func (c *enumConverter) PBValueOf(v reflect.Value) pref.Value { |
386 func (c *enumConverter) PBValueOf(v reflect.Value) protoreflect.Value { |
387 if v.Type() != c.goType { |
387 if v.Type() != c.goType { |
388 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
388 panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
389 } |
389 } |
390 return pref.ValueOfEnum(pref.EnumNumber(v.Int())) |
390 return protoreflect.ValueOfEnum(protoreflect.EnumNumber(v.Int())) |
391 } |
391 } |
392 |
392 |
393 func (c *enumConverter) GoValueOf(v pref.Value) reflect.Value { |
393 func (c *enumConverter) GoValueOf(v protoreflect.Value) reflect.Value { |
394 return reflect.ValueOf(v.Enum()).Convert(c.goType) |
394 return reflect.ValueOf(v.Enum()).Convert(c.goType) |
395 } |
395 } |
396 |
396 |
397 func (c *enumConverter) IsValidPB(v pref.Value) bool { |
397 func (c *enumConverter) IsValidPB(v protoreflect.Value) bool { |
398 _, ok := v.Interface().(pref.EnumNumber) |
398 _, ok := v.Interface().(protoreflect.EnumNumber) |
399 return ok |
399 return ok |
400 } |
400 } |
401 |
401 |
402 func (c *enumConverter) IsValidGo(v reflect.Value) bool { |
402 func (c *enumConverter) IsValidGo(v reflect.Value) bool { |
403 return v.IsValid() && v.Type() == c.goType |
403 return v.IsValid() && v.Type() == c.goType |
404 } |
404 } |
405 |
405 |
406 func (c *enumConverter) New() pref.Value { |
406 func (c *enumConverter) New() protoreflect.Value { |
407 return c.def |
407 return c.def |
408 } |
408 } |
409 |
409 |
410 func (c *enumConverter) Zero() pref.Value { |
410 func (c *enumConverter) Zero() protoreflect.Value { |
411 return c.def |
411 return c.def |
412 } |
412 } |
413 |
413 |
414 type messageConverter struct { |
414 type messageConverter struct { |
415 goType reflect.Type |
415 goType reflect.Type |