-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.go
More file actions
561 lines (504 loc) · 16.5 KB
/
value.go
File metadata and controls
561 lines (504 loc) · 16.5 KB
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Package value manipulates(gets,sets,convert etc.) interface{} value,
// convenient and simple, and reflect-based.
package value
import (
"fmt"
"math/bits"
"reflect"
)
// Value ...
type Value struct {
iv interface{}
rv reflect.Value
}
// New new a Value from v
func New(v interface{}) *Value {
return &Value{iv: v}
}
// Get returns the value with the given key.
//
// If t's kind is Map, Get returns the value associated with key in the map.
// If t's kind is Array or Slice, Get returns t's k'th element, the k must be int.
// If t's kind is Struct, Get returns the struct field with the given field name, the k must be string.
// if t's kind is Interface or Ptr, indirect it.
// It returns the nil if k is not found in the t.
// It returns error if t's kind is not Map, Array, Slice or Struct.
func (v *Value) Get(k interface{}) (*Value, error) {
rv := v.getrv()
switch rv.Kind() {
case reflect.Map:
return v.mapGet(k), nil
case reflect.Array, reflect.Slice:
return v.sliceGet(k.(int)), nil
case reflect.Struct:
return v.structGet(k.(string)), nil
case reflect.Interface, reflect.Ptr:
vv := &Value{rv: indirect(rv)}
return vv.Get(k)
default:
return nil, &ErrUnsupportedKind{"Value.Get", rv.Kind()}
}
}
// Set set t's value to v.
//
// If t's value can't setable, returns ErrCannotSet.
// If t's kind and v's kind is not equivalence, returns ErrTypeUnequal.
// It returns nil, that set successful.
//
// If set map key, struct field or array/slice index, using Value.Put.
func (v *Value) Set(iv interface{}) error {
rv := v.getrv()
if rv.Kind() == reflect.Interface || rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
if !rv.CanSet() {
return &ErrCannotSet{"Value.Set"}
}
irv := reflect.ValueOf(iv)
if rv.Kind() != irv.Kind() {
return &ErrTypeUnequal{"Value.Set", rv.Kind(), irv.Kind()}
}
rv.Set(irv)
// reset
v.iv = nil
return nil
}
// Put put k, v to map, array, slice or struct(structed type).
//
// If t's kind is map, the k indicates key of map.
// If t's kind is array/slice, the k indecates index of array/slice.
// If t's kind is struct, the k indecates fieldname of struct.
//
// If k in t, and set k's value to v.
//
// If t's kind is not map, array, slice or struct, returns ErrUnsupportedKind.
func (v *Value) Put(key, val interface{}) (err error) {
rv := v.getrv()
switch rv.Kind() {
case reflect.Map:
return v.mapPut(key, val)
case reflect.Slice:
return v.slicePut(key.(int), val)
case reflect.Ptr:
rvv := indirect(rv)
switch rvv.Kind() {
case reflect.Array:
return (&Value{rv: rvv}).arrayPut(key.(int), val)
case reflect.Struct:
return (&Value{rv: rvv}).structPut(key.(string), val)
default:
return &ErrUnsupportedKind{"Value.Put", v.getrv().Kind()}
}
default:
return &ErrUnsupportedKind{"Value.Put", v.getrv().Kind()}
}
}
// Bytes returns t's underlying value as a []bytes.
// It returns error if t's underlying value is not a slice of bytes.
func (v *Value) Bytes() ([]byte, error) {
tv := v.getrv()
switch tv.Kind() {
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(tv)}).Bytes()
case reflect.Slice:
elemk := tv.Type().Elem().Kind()
if elemk != reflect.Uint8 {
return nil, &ErrUnsupportedKind{"Value.Bytes", "slice of " + elemk.String()}
}
return tv.Bytes(), nil
default:
return nil, &ErrUnsupportedKind{"Value.Bytes", v.getrv().Kind()}
}
}
// Bool returns t's underlying value.
// It returns error if t's kind is not Bool.
func (v *Value) Bool() (bool, error) {
switch v.getrv().Kind() {
case reflect.Bool:
return v.bool(), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Bool()
default:
return false, &ErrUnsupportedKind{"Value.Int", v.getrv().Kind()}
}
}
// Int returns t's underlying value as an int.
// It returns error if t's kind is not Int, Int8, Int16, Int32, Uint8 or Uint16,
// and if t's kind is Int64 or Uint32 also Int is 32 bits.
func (v *Value) Int() (i int, err error) {
switch v.getrv().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
i = int(v.int())
case reflect.Int64:
if bits.UintSize == 64 { // if int size is 64
i = int(v.int())
} else { // if int size is 32
err = &ErrUnsupportedKind{"Value.Int", v.getrv().Kind()}
}
case reflect.Uint8, reflect.Uint16:
i = int(v.uint())
case reflect.Uint32:
if bits.UintSize == 64 {
i = int(v.uint())
} else {
err = &ErrUnsupportedKind{"Value.Int", v.getrv().Kind()}
}
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Int()
default:
err = &ErrUnsupportedKind{"Value.Int", v.getrv().Kind()}
}
return
}
// Int8 returns t's underlying value as an int8.
// It returns error if t's kind is not Int8.
func (v *Value) Int8() (int8, error) {
switch v.getrv().Kind() {
case reflect.Int8:
return int8(v.int()), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Int8()
default:
return 0, &ErrUnsupportedKind{"Value.Int8", v.getrv().Kind()}
}
}
// Int16 returns t's underlying value as an int16.
// It returns error if t's kind is not Int, Int8, Int16, or Uint8.
func (v *Value) Int16() (int16, error) {
switch v.getrv().Kind() {
case reflect.Int8, reflect.Int16:
return int16(v.int()), nil
case reflect.Uint8:
return int16(v.uint()), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Int16()
default:
return 0, &ErrUnsupportedKind{"Value.Int16", v.getrv().Kind()}
}
}
// Int32 returns t's underlying value as an int32.
// It returns error if t's kind is not Int, Int8, Int16, Int32, Uint8 or Uint16,
// and if t's kind is Int also Int is 64 bits.
func (v *Value) Int32() (int32, error) {
switch v.getrv().Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32:
return int32(v.int()), nil
case reflect.Int:
if bits.UintSize == 32 { // 32
return int32(v.int()), nil
}
return 0, &ErrUnsupportedKind{"Value.Int32", v.getrv().Kind()}
case reflect.Uint8, reflect.Uint16:
return int32(v.uint()), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Int32()
default:
return 0, &ErrUnsupportedKind{"Value.Int32", v.getrv().Kind()}
}
}
// Int64 returns t's underlying value as an int64.
// It returns error if t's kind is not Int, Int8, Int16, Int32, Uint8, Uint16, Uint32
// and if t's kind is Uint also Uint is 64 bits.
func (v *Value) Int64() (int64, error) {
switch v.getrv().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.int(), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return int64(v.uint()), nil
case reflect.Uint:
if bits.UintSize == 32 { // 32
return int64(v.uint()), nil
}
return 0, &ErrUnsupportedKind{"Value.Int64", v.getrv().Kind()}
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Int64()
default:
return 0, &ErrUnsupportedKind{"Value.Int64", v.getrv().Kind()}
}
}
// Uint returns t's underlying value as an uint.
// It returns error if t's kind is not Uint, Uint8, Uint16 or Uint32,
// and if t's kind is Uint64 also Uint is 32 bits.
func (v *Value) Uint() (i uint, err error) {
switch v.getrv().Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
i = uint(v.uint())
case reflect.Uint64:
if bits.UintSize == 64 { // 64
i = uint(v.uint())
} else { // 32
err = &ErrUnsupportedKind{"Value.Uint", v.getrv().Kind()}
}
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Uint()
default:
err = &ErrUnsupportedKind{"Value.Uint", v.getrv().Kind()}
}
return
}
// Uint8 returns t's underlying value as an uint8.
// It returns error if t's kind is not Uint8.
func (v *Value) Uint8() (uint8, error) {
switch v.getrv().Kind() {
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Uint8()
case reflect.Uint8:
return uint8(v.uint()), nil
default:
return 0, &ErrUnsupportedKind{"Value.Uint8", v.getrv().Kind()}
}
}
// Uint16 returns t's underlying value as an uint16.
// It returns error if t's kind is not Uint8 or Uint16.
func (v *Value) Uint16() (uint16, error) {
switch v.getrv().Kind() {
case reflect.Uint8, reflect.Uint16:
return uint16(v.uint()), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Uint16()
default:
return 0, &ErrUnsupportedKind{"Value.Uint16", v.getrv().Kind()}
}
}
// Uint32 returns t's underlying value as an uint32.
// It returns error if t's kind is not Uint8, Uint16 or Uint32,
// and if t's kind is Uint also Uint is 64 bits.
func (v *Value) Uint32() (uint32, error) {
switch v.getrv().Kind() {
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return uint32(v.uint()), nil
case reflect.Uint:
if bits.UintSize == 32 { // 32
return uint32(v.uint()), nil
}
return 0, &ErrUnsupportedKind{"Value.Uint32", v.getrv().Kind()}
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Uint32()
default:
return 0, &ErrUnsupportedKind{"Value.Uint32", v.getrv().Kind()}
}
}
// Uint64 returns t's underlying value as an uint64.
// It returns error if t's kind is not Uint*.
func (v *Value) Uint64() (uint64, error) {
switch v.getrv().Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v.uint(), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Uint64()
default:
return 0, &ErrUnsupportedKind{"Value.Uint64", v.getrv().Kind()}
}
}
// Float32 returns t's underlying value as an float32.
// It returns error if t's kind is not Uint*, Int* or Float32.
func (v *Value) Float32() (float32, error) {
switch v.getrv().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float32(v.int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float32(v.uint()), nil
case reflect.Float32:
return float32(v.float()), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Float32()
default:
return 0, &ErrUnsupportedKind{"Value.Float32", v.getrv().Kind()}
}
}
// Float64 returns t's underlying value as an float64.
// It returns error if t's kind is not Uint*, Int* or Float*.
func (v *Value) Float64() (float64, error) {
switch v.getrv().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(v.int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(v.uint()), nil
case reflect.Float32, reflect.Float64:
return v.float(), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Float64()
default:
return 0, &ErrUnsupportedKind{"Value.Float64", v.getrv().Kind()}
}
}
// Complex64 returns t's underlying value as an complex64.
// It returns error if t's kind is not Uint*, Int*, Float32 or Complex64.
func (v *Value) Complex64() (complex64, error) {
switch v.getrv().Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return complex(float32(v.uint()), 0), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return complex(float32(v.int()), 0), nil
case reflect.Float32:
return complex(float32(v.float()), 0), nil
case reflect.Complex64:
return complex64(v.complex_()), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Complex64()
default:
return 0i, &ErrUnsupportedKind{"Value.Complex64", v.getrv().Kind()}
}
}
// Complex128 returns t's underlying value as an complex128.
// It returns error if t's kind is not Uint*, Int*, Float* or Complex*.
func (v *Value) Complex128() (complex128, error) {
switch v.getrv().Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return complex(float64(v.uint()), 0), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return complex(float64(v.int()), 0), nil
case reflect.Float32, reflect.Float64:
return complex(v.float(), 0), nil
case reflect.Complex64, reflect.Complex128:
return v.complex_(), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Complex128()
default:
return 0i, &ErrUnsupportedKind{"Value.Complex128", v.getrv().Kind()}
}
}
// Map returns t's underlying value as a map.
// It returns error if t's kind is not Map, Array, Slice or Struct.
func (v *Value) Map() (map[*Value]*Value, error) {
switch v.getrv().Kind() {
case reflect.Map:
return v.mapMap(), nil
case reflect.Array, reflect.Slice:
return v.sliceMap(), nil
case reflect.Struct:
return v.structMap(), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Map()
default:
return nil, &ErrUnsupportedKind{"Value.Map", v.getrv().Kind()}
}
}
// Slice returns t's underlying value as a slice.
// It returns error if t's kind is not Array, Slice or Struct.
func (v *Value) Slice() ([]*Value, error) {
switch v.getrv().Kind() {
case reflect.Array, reflect.Slice:
return v.sliceSlice(), nil
case reflect.Struct:
return v.structSlice(), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).Slice()
default:
return nil, &ErrUnsupportedKind{"Value.Slice", v.getrv().Kind()}
}
}
// AList returns t's underlying value as an association list.
// It returns error if t's kind is not Map, Array, Slice or Struct.
func (v *Value) AList() ([][2]*Value, error) {
switch v.getrv().Kind() {
case reflect.Map:
return v.mapAList(), nil
case reflect.Array, reflect.Slice:
return v.sliceAList(), nil
case reflect.Struct:
return v.structAList(), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).AList()
default:
return nil, &ErrUnsupportedKind{"Value.AList", v.getrv().Kind()}
}
}
// PList returns t's underlying value as an property list.
// It returns error if t's kind is not Map, Array, Slice or Struct.
func (v *Value) PList() ([]*Value, error) {
switch v.getrv().Kind() {
case reflect.Map:
return v.mapPList(), nil
case reflect.Array, reflect.Slice:
return v.slicePList(), nil
case reflect.Struct:
return v.structPList(), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).PList()
default:
return nil, &ErrUnsupportedKind{"Value.PList", v.getrv().Kind()}
}
}
func (v *Value) Interface() interface{} {
return v.getiv()
}
func (v *Value) Ptr() uintptr {
return v.getrv().Pointer()
}
func (v *Value) String() (string, error) {
iv := v.getiv()
strger, ok := iv.(fmt.Stringer)
if ok {
return strger.String(), nil
}
switch v.getrv().Kind() {
case reflect.Invalid:
return "", nil
case reflect.String:
return v.string(), nil
case reflect.Bool:
return fmt.Sprintf("%t", v.bool()), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return fmt.Sprintf("%d", v.int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return fmt.Sprintf("%d", v.uint()), nil
case reflect.Float32, reflect.Float64:
return fmt.Sprintf("%g", v.float()), nil
case reflect.Complex64, reflect.Complex128:
return fmt.Sprintf("%g", v.complex_()), nil
case reflect.Chan:
return fmt.Sprintf("%p", v.getrv().Interface()), nil
case reflect.UnsafePointer:
return fmt.Sprintf("%x", v.getrv().Interface()), nil
case reflect.Struct:
return fmt.Sprintf("%#v", v.getrv().Interface()), nil
case reflect.Slice, reflect.Array, reflect.Map:
return fmt.Sprintf("%v", v.getrv().Interface()), nil
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).String()
default:
return "", &ErrUnsupportedKind{"Value.String", v.getrv().Kind()}
}
}
type eachDoFunc func(k, v *Value) error
// EachDo ...
func (v *Value) EachDo(f eachDoFunc) error {
switch v.getrv().Kind() {
case reflect.Map, reflect.Array, reflect.Slice, reflect.Struct:
m, err := v.Map()
if err != nil {
return err
}
for k, v := range m {
if err := f(k, v); err != nil {
return err
}
}
case reflect.Chan:
idx := 0
for {
v, ok := v.getrv().Recv()
if !ok {
break
}
if err := f(&Value{iv: idx}, &Value{rv: v}); err != nil {
return err
}
idx++
}
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Uintptr,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
if err := f(nil, v); err != nil {
return err
}
case reflect.Interface, reflect.Ptr:
return (&Value{rv: indirect(v.getrv())}).EachDo(f)
default:
return &ErrUnsupportedKind{"Value.EachDo", v.getrv().Kind()}
}
return nil
}