-
Notifications
You must be signed in to change notification settings - Fork 0
/
iconvert.go
55 lines (49 loc) · 976 Bytes
/
iconvert.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
package docs
import "fmt"
type interData struct {
val interface{}
}
func (d interData) ToInt64() (int64, error) {
switch t := d.val.(type) {
case int:
return int64(t), nil
case int8:
return int64(t), nil
case int16:
return int64(t), nil
case int32:
return int64(t), nil
case int64:
return t, nil
case uint8:
return int64(t), nil
case uint16:
return int64(t), nil
case uint32:
return int64(t), nil
case uint64:
return int64(t), nil
default:
return 0, fmt.Errorf("can not convert to int64, val: %v", d.val)
}
}
func (d interData) ToFloat() (float64, error) {
i, err := d.ToInt64()
if err == nil {
return float64(i), nil
}
switch t := d.val.(type) {
case float32:
return float64(t), nil
case float64:
return t, nil
default:
return 0, fmt.Errorf("can not convert to float, val: %v", d.val)
}
}
func (d interData) ToString() string {
return fmt.Sprintf("%v", d.val)
}
func (d interData) Value() interface{} {
return d.val
}