-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.go
152 lines (145 loc) · 4.04 KB
/
interface.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
package helpers
import (
"reflect"
"strconv"
"github.com/pkg/errors"
)
// ConvertInterfaceToSlice will convert an interface `value` into slice.
// The `value` is also supporting pointer interface.
func ConvertInterfaceToSlice(value interface{}) interface{} {
value = indirect(value)
vType := reflect.ValueOf(value)
switch vType.Kind() {
case reflect.Array, reflect.Slice:
return value
default:
return []interface{}{value}
}
}
// ConvertInterfaceToBool will convert an interface `value` into boolean.
// The `value` is also supporting pointer interface.
func ConvertInterfaceToBool(value interface{}) (bool, error) {
value = indirect(value)
switch v := value.(type) {
case bool:
return v, nil
case string:
return strconv.ParseBool(v)
case int:
return strconv.ParseBool(strconv.FormatInt(int64(v), 10))
case int8:
return strconv.ParseBool(strconv.FormatInt(int64(v), 10))
case int16:
return strconv.ParseBool(strconv.FormatInt(int64(v), 10))
case int32:
return strconv.ParseBool(strconv.FormatInt(int64(v), 10))
case int64:
return strconv.ParseBool(strconv.FormatInt(v, 10))
case uint:
return strconv.ParseBool(strconv.FormatUint(uint64(v), 10))
case uint8:
return strconv.ParseBool(strconv.FormatUint(uint64(v), 10))
case uint16:
return strconv.ParseBool(strconv.FormatUint(uint64(v), 10))
case uint32:
return strconv.ParseBool(strconv.FormatUint(uint64(v), 10))
case uint64:
return strconv.ParseBool(strconv.FormatUint(v, 10))
case float32:
return strconv.ParseBool(strconv.FormatFloat(float64(v), 'f', -1, 64))
case float64:
return strconv.ParseBool(strconv.FormatFloat(v, 'f', -1, 64))
default:
return false, errors.New("wrong parameter type")
}
}
// ConvertInterfaceToFloat will convert an interface `value` into float.
// The `value` is also supporting pointer interface.
func ConvertInterfaceToFloat(value interface{}) (float64, error) {
value = indirect(value)
switch v := value.(type) {
case string:
return strconv.ParseFloat(v, 64)
case int:
return float64(v), nil
case int8:
return float64(v), nil
case int16:
return float64(v), nil
case int32:
return float64(v), nil
case int64:
return float64(v), nil
case uint:
return float64(v), nil
case uint8:
return float64(v), nil
case uint16:
return float64(v), nil
case uint32:
return float64(v), nil
case uint64:
return float64(v), nil
case float32:
return float64(v), nil
case float64:
return v, nil
default:
return 0, errors.New("wrong parameter type")
}
}
// ConvertInterfaceToString will convert an interface `value` into string.
// The `value` is also supporting pointer interface.
func ConvertInterfaceToString(value interface{}) (string, error) {
value = indirect(value)
switch v := value.(type) {
case string:
return v, nil
case bool:
return strconv.FormatBool(v), nil
case int:
return strconv.Itoa(v), nil
case int8:
return strconv.FormatInt(int64(v), 10), nil
case int16:
return strconv.FormatInt(int64(v), 10), nil
case int32:
return strconv.FormatInt(int64(v), 10), nil
case int64:
return strconv.FormatInt(v, 10), nil
case uint:
return strconv.FormatUint(uint64(v), 10), nil
case uint8:
return strconv.FormatUint(uint64(v), 10), nil
case uint16:
return strconv.FormatUint(uint64(v), 10), nil
case uint32:
return strconv.FormatUint(uint64(v), 10), nil
case uint64:
return strconv.FormatUint(v, 10), nil
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 32), nil
case float64:
return strconv.FormatFloat(v, 'f', -1, 64), nil
default:
return "", errors.New("wrong parameter type")
}
}
// From html/template/content.go
// Copyright 2011 The Go Authors. All rights reserved.
// indirect returns the value, after dereferencing as many times
// as necessary to reach the base type (or nil).
func indirect(i interface{}) interface{} {
if i == nil {
return nil
}
if t := reflect.TypeOf(i); t.Kind() != reflect.Ptr {
// Avoid creating a reflect.Value if it's not a pointer.
return i
}
v := reflect.ValueOf(i)
for v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
return v.Interface()
}