-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
56 lines (49 loc) · 1.11 KB
/
handlers.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
package transform
import (
"strconv"
"strings"
)
type FieldHandler func(interface{}) (interface{}, error)
func StringTrimSpace(a interface{}) (interface{}, error) {
if av, ok := a.(string); ok {
return strings.TrimSpace(av), nil
}
return a, ErrTypeNotMatch
}
func StringToInt(a interface{}) (interface{}, error) {
if av, ok := a.(string); ok {
if ai, err := strconv.Atoi(av); err == nil {
return ai, nil
} else {
return 0, ErrConvertFailed
}
}
return 0, ErrTypeNotMatch
}
func StringToStringSlice(a interface{}) (interface{}, error) {
if av, ok := a.(string); ok {
return strings.Split(av, ","), nil
}
return []string{}, ErrTypeNotMatch
}
func StringToIntSlice(a interface{}) (interface{}, error) {
bv := []int{}
a, err := StringToStringSlice(a)
if err != nil {
return bv, err
}
for _, row := range a.([]string) {
rowInt, err := strconv.Atoi(row)
if err != nil {
return bv, ErrConvertFailed
}
bv = append(bv, rowInt)
}
return bv, nil
}
func Int64ToInt(a interface{}) (interface{}, error) {
if aInt64, ok := a.(int64); ok {
return int(aInt64), nil
}
return 0, ErrTypeNotMatch
}