-
Notifications
You must be signed in to change notification settings - Fork 4
/
coercers.go
432 lines (401 loc) · 9.64 KB
/
coercers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Code generated by "go run generate_coercers.go"; DO NOT EDIT.
package venom
import "fmt"
// A CoerceErr is returned when incompatible types are attempted to be coerced
type CoerceErr struct {
From interface{}
To string
Err error
}
func (e *CoerceErr) Error() string {
return fmt.Sprintf("venom: can not coerce %T to %q", e.From, e.To)
}
func coerceString(val interface{}) (string, error) {
if value, ok := val.(string); !ok {
return "", &CoerceErr{From: val, To: "string"}
} else {
return value, nil
}
}
func coerceStringSlice(val interface{}) ([]string, error) {
switch val.(type) {
case []string:
return val.([]string), nil
case nil:
return nil, nil
case []interface{}:
var container []string
for _, item := range val.([]interface{}) {
coerced, err := coerceString(item)
if err != nil {
return nil, &CoerceErr{val, "[]string", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]string"}
}
}
func coerceBool(val interface{}) (bool, error) {
if value, ok := val.(bool); !ok {
return false, &CoerceErr{From: val, To: "bool"}
} else {
return value, nil
}
}
func coerceBoolSlice(val interface{}) ([]bool, error) {
switch val.(type) {
case []bool:
return val.([]bool), nil
case nil:
return nil, nil
case []interface{}:
var container []bool
for _, item := range val.([]interface{}) {
coerced, err := coerceBool(item)
if err != nil {
return nil, &CoerceErr{val, "[]bool", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]bool"}
}
}
func coerceInt(val interface{}) (int, error) {
if value, ok := val.(int); !ok {
return 0, &CoerceErr{From: val, To: "int"}
} else {
return value, nil
}
}
func coerceIntSlice(val interface{}) ([]int, error) {
switch val.(type) {
case []int:
return val.([]int), nil
case nil:
return nil, nil
case []interface{}:
var container []int
for _, item := range val.([]interface{}) {
var coerced int
var err error
switch item.(type) {
// this special case is required due to how encoding/json treats
// numbers
case float64:
var coercedFloat float64
coercedFloat, err = coerceFloat64(item)
coerced = int(coercedFloat)
default:
coerced, err = coerceInt(item)
}
if err != nil {
return nil, &CoerceErr{val, "[]int", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]int"}
}
}
func coerceInt8(val interface{}) (int8, error) {
if value, ok := val.(int8); !ok {
return 0, &CoerceErr{From: val, To: "int8"}
} else {
return value, nil
}
}
func coerceInt8Slice(val interface{}) ([]int8, error) {
switch val.(type) {
case []int8:
return val.([]int8), nil
case nil:
return nil, nil
case []interface{}:
var container []int8
for _, item := range val.([]interface{}) {
coerced, err := coerceInt8(item)
if err != nil {
return nil, &CoerceErr{val, "[]int8", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]int8"}
}
}
func coerceInt16(val interface{}) (int16, error) {
if value, ok := val.(int16); !ok {
return 0, &CoerceErr{From: val, To: "int16"}
} else {
return value, nil
}
}
func coerceInt16Slice(val interface{}) ([]int16, error) {
switch val.(type) {
case []int16:
return val.([]int16), nil
case nil:
return nil, nil
case []interface{}:
var container []int16
for _, item := range val.([]interface{}) {
coerced, err := coerceInt16(item)
if err != nil {
return nil, &CoerceErr{val, "[]int16", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]int16"}
}
}
func coerceInt32(val interface{}) (int32, error) {
if value, ok := val.(int32); !ok {
return 0, &CoerceErr{From: val, To: "int32"}
} else {
return value, nil
}
}
func coerceInt32Slice(val interface{}) ([]int32, error) {
switch val.(type) {
case []int32:
return val.([]int32), nil
case nil:
return nil, nil
case []interface{}:
var container []int32
for _, item := range val.([]interface{}) {
coerced, err := coerceInt32(item)
if err != nil {
return nil, &CoerceErr{val, "[]int32", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]int32"}
}
}
func coerceInt64(val interface{}) (int64, error) {
if value, ok := val.(int64); !ok {
return 0, &CoerceErr{From: val, To: "int64"}
} else {
return value, nil
}
}
func coerceInt64Slice(val interface{}) ([]int64, error) {
switch val.(type) {
case []int64:
return val.([]int64), nil
case nil:
return nil, nil
case []interface{}:
var container []int64
for _, item := range val.([]interface{}) {
coerced, err := coerceInt64(item)
if err != nil {
return nil, &CoerceErr{val, "[]int64", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]int64"}
}
}
func coerceUint(val interface{}) (uint, error) {
if value, ok := val.(uint); !ok {
return 0, &CoerceErr{From: val, To: "uint"}
} else {
return value, nil
}
}
func coerceUintSlice(val interface{}) ([]uint, error) {
switch val.(type) {
case []uint:
return val.([]uint), nil
case nil:
return nil, nil
case []interface{}:
var container []uint
for _, item := range val.([]interface{}) {
coerced, err := coerceUint(item)
if err != nil {
return nil, &CoerceErr{val, "[]uint", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]uint"}
}
}
func coerceUint8(val interface{}) (uint8, error) {
if value, ok := val.(uint8); !ok {
return 0, &CoerceErr{From: val, To: "uint8"}
} else {
return value, nil
}
}
func coerceUint8Slice(val interface{}) ([]uint8, error) {
switch val.(type) {
case []uint8:
return val.([]uint8), nil
case nil:
return nil, nil
case []interface{}:
var container []uint8
for _, item := range val.([]interface{}) {
coerced, err := coerceUint8(item)
if err != nil {
return nil, &CoerceErr{val, "[]uint8", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]uint8"}
}
}
func coerceUint16(val interface{}) (uint16, error) {
if value, ok := val.(uint16); !ok {
return 0, &CoerceErr{From: val, To: "uint16"}
} else {
return value, nil
}
}
func coerceUint16Slice(val interface{}) ([]uint16, error) {
switch val.(type) {
case []uint16:
return val.([]uint16), nil
case nil:
return nil, nil
case []interface{}:
var container []uint16
for _, item := range val.([]interface{}) {
coerced, err := coerceUint16(item)
if err != nil {
return nil, &CoerceErr{val, "[]uint16", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]uint16"}
}
}
func coerceUint32(val interface{}) (uint32, error) {
if value, ok := val.(uint32); !ok {
return 0, &CoerceErr{From: val, To: "uint32"}
} else {
return value, nil
}
}
func coerceUint32Slice(val interface{}) ([]uint32, error) {
switch val.(type) {
case []uint32:
return val.([]uint32), nil
case nil:
return nil, nil
case []interface{}:
var container []uint32
for _, item := range val.([]interface{}) {
coerced, err := coerceUint32(item)
if err != nil {
return nil, &CoerceErr{val, "[]uint32", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]uint32"}
}
}
func coerceUint64(val interface{}) (uint64, error) {
if value, ok := val.(uint64); !ok {
return 0, &CoerceErr{From: val, To: "uint64"}
} else {
return value, nil
}
}
func coerceUint64Slice(val interface{}) ([]uint64, error) {
switch val.(type) {
case []uint64:
return val.([]uint64), nil
case nil:
return nil, nil
case []interface{}:
var container []uint64
for _, item := range val.([]interface{}) {
coerced, err := coerceUint64(item)
if err != nil {
return nil, &CoerceErr{val, "[]uint64", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]uint64"}
}
}
func coerceFloat32(val interface{}) (float32, error) {
if value, ok := val.(float32); !ok {
return 0, &CoerceErr{From: val, To: "float32"}
} else {
return value, nil
}
}
func coerceFloat32Slice(val interface{}) ([]float32, error) {
switch val.(type) {
case []float32:
return val.([]float32), nil
case nil:
return nil, nil
case []interface{}:
var container []float32
for _, item := range val.([]interface{}) {
coerced, err := coerceFloat32(item)
if err != nil {
return nil, &CoerceErr{val, "[]float32", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]float32"}
}
}
func coerceFloat64(val interface{}) (float64, error) {
if value, ok := val.(float64); !ok {
return 0, &CoerceErr{From: val, To: "float64"}
} else {
return value, nil
}
}
func coerceFloat64Slice(val interface{}) ([]float64, error) {
switch val.(type) {
case []float64:
return val.([]float64), nil
case nil:
return nil, nil
case []interface{}:
var container []float64
for _, item := range val.([]interface{}) {
coerced, err := coerceFloat64(item)
if err != nil {
return nil, &CoerceErr{val, "[]float64", err}
}
container = append(container, coerced)
}
return container, nil
default:
return nil, &CoerceErr{From: val, To: "[]float64"}
}
}