-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode_number.go
46 lines (42 loc) · 1002 Bytes
/
decode_number.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
package jfather
import (
"fmt"
"reflect"
)
func (n *node) decodeNumber(v reflect.Value) error {
switch v.Kind() {
case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
if i64, ok := n.raw.(int64); ok {
v.SetInt(i64)
return nil
}
if f64, ok := n.raw.(float64); ok {
v.SetInt(int64(f64))
return nil
}
case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
if i64, ok := n.raw.(int64); ok {
v.SetUint(uint64(i64))
return nil
}
if f64, ok := n.raw.(float64); ok {
v.SetUint(uint64(f64))
return nil
}
case reflect.Float32, reflect.Float64:
if i64, ok := n.raw.(int64); ok {
v.SetFloat(float64(i64))
return nil
}
if f64, ok := n.raw.(float64); ok {
v.SetFloat(f64)
return nil
}
case reflect.Interface:
v.Set(reflect.ValueOf(n.raw))
return nil
default:
return fmt.Errorf("cannot decode number value to %s target", v.Kind())
}
return fmt.Errorf("internal value is not numeric")
}