-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.go
111 lines (105 loc) · 2.51 KB
/
conversions.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
package utils
import (
"fmt"
"math"
"strconv"
"strings"
)
// ParseBool returns the bool value represented by string
func ParseBool(str string) bool {
v, err := strconv.ParseBool(str)
if err != nil {
str = strings.ToLower(str)
switch str {
case "on", "yes":
return true
default:
return false
}
}
return v
}
// ConvertToInt64 converts a value to an int
func ConvertToInt64(v interface{}) (int64, error) {
switch v.(type) {
case int:
return int64(v.(int)), nil
case int64:
return v.(int64), nil
case int32:
return int64(v.(int32)), nil
case float64:
return int64(math.Round(v.(float64))), nil
case []byte:
b := v.([]byte)
if len(b) == 0 {
return 0, fmt.Errorf("Cannot parse \"%v\" of type \"%T\" as Int, array is empty", v, v)
}
return int64(b[0]), nil
case string:
vInt, err := strconv.ParseInt(v.(string), 10, 64)
if err != nil {
return 0, err
}
return vInt, nil
}
return 0, fmt.Errorf("Cannot parse \"%v\" of type \"%T\" as Int", v, v)
}
// ConvertToFloat converts a value to a float64
func ConvertToFloat(v interface{}) (float64, error) {
switch v.(type) {
case int:
return float64(v.(int)), nil
case int64:
return float64(v.(int64)), nil
case int32:
return float64(v.(int32)), nil
case float64:
return v.(float64), nil
case string:
vF, err := strconv.ParseFloat(v.(string), 64)
if err != nil {
return 0, err
}
return vF, nil
}
return 0, fmt.Errorf("Cannot parse \"%v\" of type \"%T\" as Float64", v, v)
}
// ConvertToBool converts a value to a bool
func ConvertToBool(v interface{}) (bool, error) {
switch v.(type) {
case bool:
return v.(bool), nil
case []byte:
b := v.([]byte)
if len(b) == 0 {
return false, fmt.Errorf("Cannot parse \"%v\" of type \"%T\" as bool, array is empty", v, v)
}
return b[0] != 0, nil
case string:
vB, err := strconv.ParseBool(v.(string))
if err != nil {
return false, err
}
return vB, nil
}
return false, fmt.Errorf("Cannot parse \"%v\" of type \"%T\" as Bool", v, v)
}
// ConvertToString converts a value to a string
func ConvertToString(v interface{}) (string, error) {
switch v.(type) {
case string:
return v.(string), nil
case bool:
return strconv.FormatBool(v.(bool)), nil
case int:
return strconv.Itoa(v.(int)), nil
case int64:
return strconv.FormatInt(v.(int64), 10), nil
case int32:
return strconv.FormatInt(int64(v.(int32)), 10), nil
case float64:
return strconv.FormatFloat(v.(float64), 'f', -1, 64), nil
}
return "", fmt.Errorf("Cannot parse \"%v\" of type \"%T\" as String", v, v)
}