-
Notifications
You must be signed in to change notification settings - Fork 0
/
numeric.go
123 lines (111 loc) · 2.98 KB
/
numeric.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
package sprigmath
import (
"math"
"reflect"
"strconv"
"github.com/pkg/errors"
)
//
// Copied from sprig, BSD license
//
// toFloat64 converts 64-bit floats
func toFloat64(v interface{}) (float64, error) {
if str, ok := v.(string); ok {
iv, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, errors.Errorf("cannot convert %v to float64", v)
}
return iv, nil
}
val := reflect.Indirect(reflect.ValueOf(v))
switch val.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
return float64(val.Int()), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return float64(val.Uint()), nil
case reflect.Uint, reflect.Uint64:
return float64(val.Uint()), nil
case reflect.Float32, reflect.Float64:
return val.Float(), nil
case reflect.Bool:
if val.Bool() == true {
return 1, nil
}
return 0, nil
default:
return 0, errors.Errorf("cannot convert %v to float64", v)
}
}
func toInt(v interface{}) (int, error) {
//It's not optimal. Bud I don't want duplicate toInt64 code.
vv, err := toInt64(v)
return int(vv), err
}
// toInt64 converts integer types to 64-bit integers
func toInt64(v interface{}) (int64, error) {
if str, ok := v.(string); ok {
iv, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return 0, errors.Errorf("cannot convert %v to int64", v)
}
return iv, nil
}
val := reflect.Indirect(reflect.ValueOf(v))
switch val.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
return val.Int(), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return int64(val.Uint()), nil
case reflect.Uint, reflect.Uint64:
tv := val.Uint()
if tv <= math.MaxInt64 {
return int64(tv), nil
}
return math.MaxInt64, errors.Errorf("%v is too big", tv)
case reflect.Float32, reflect.Float64:
return int64(val.Float()), nil
case reflect.Bool:
if val.Bool() == true {
return 1, nil
}
return 0, nil
default:
return 0, errors.Errorf("cannot convert %v to int64", v)
}
}
// converts to either an int64 or a float64
func toNumber(v interface{}) (interface{}, error) {
if str, ok := v.(string); ok {
iv, err := strconv.ParseInt(str, 10, 64)
if err == nil {
return iv, nil
}
fv, err := strconv.ParseFloat(str, 64)
if err == nil {
return fv, nil
}
return nil, errors.Errorf("%v is not a float64 or int64", v)
}
val := reflect.Indirect(reflect.ValueOf(v))
switch val.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
return val.Int(), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return int64(val.Uint()), nil
case reflect.Uint, reflect.Uint64:
tv := val.Uint()
if tv <= math.MaxInt64 {
return int64(tv), nil
}
return math.MaxInt64, errors.Errorf("%v is too big", tv)
case reflect.Float32, reflect.Float64:
return val.Float(), nil
case reflect.Bool:
if val.Bool() == true {
return 1, nil
}
return 0, nil
default:
return nil, errors.Errorf("cannot convert %v to float64 or int64", v)
}
}