forked from lauyoume/gopiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
88 lines (79 loc) · 1.63 KB
/
helper.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
package gopiper
import "strings"
func _filterValue(src interface{}, fn func(v string) (interface{}, error), fnDefaults ...func() (interface{}, error)) (interface{}, error) {
switch vt := src.(type) {
case string:
return fn(vt)
case []string:
for i, v := range vt {
_v, _e := fn(v)
if _e != nil {
vt[i] = _e.Error()
continue
}
vt[i], _ = _v.(string)
}
return vt, nil
case map[string]string:
for i, v := range vt {
_v, _e := fn(v)
if _e != nil {
vt[i] = _e.Error()
continue
}
vt[i], _ = _v.(string)
}
return vt, nil
case []interface{}:
for i, v := range vt {
vt[i], _ = _filterValue(v, fn)
}
return vt, nil
case map[string]interface{}:
for i, v := range vt {
vt[i], _ = _filterValue(v, fn)
}
return vt, nil
default:
if len(fnDefaults) > 0 && fnDefaults[0] != nil {
return fnDefaults[0]()
}
return vt, nil
}
}
func SplitParams(params string, separators ...string) []string {
if len(params) == 0 {
return []string{}
}
separator := `,`
if len(separators) > 0 {
separator = separators[0]
if len(separator) < 1 {
return strings.Split(params, separator)
}
if len(separator) > 1 {
separator = separator[0:1]
}
}
vt := strings.Split(params, separator)
var (
lastEnd string
results []string
)
for k, v := range vt {
lastKey := k - 1
if lastEnd == `\` {
lastVal := vt[lastKey]
vt[lastKey] = lastVal[0:len(lastVal)-1] + separator + v
resultLen := len(results)
if resultLen > 0 {
results[resultLen-1] = vt[lastKey]
}
lastEnd = v[len(v)-1:]
continue
}
lastEnd = v[len(v)-1:]
results = append(results, v)
}
return results
}