-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfilter.go
126 lines (115 loc) · 2.94 KB
/
filter.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
// Package filter provide data filter, sanitize, convert process.
//
// Source code and other details for the project are available at GitHub:
//
// https://github.com/gookit/filter
//
// More usage please see README and tests
package filter
import (
"fmt"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/strutil"
)
// Apply a filter by name. for filter value.
func Apply(name string, val any, args []string) (any, error) {
var err error
realName := Name(name)
// don't limit value type
if _, ok := dontLimitType[realName]; ok {
switch realName {
case "int":
val, err = mathutil.ToInt(val)
case "uint":
val, err = mathutil.ToUint(val)
case "int64":
val, err = mathutil.ToInt64(val)
case "float":
val, err = mathutil.ToFloat(val)
case "unique":
val = Unique(val)
case "trimStrings":
if ss, ok := val.([]string); ok {
val = arrutil.TrimStrings(ss)
} else {
err = errInvalidParam
}
case "stringsToInts":
if ss, ok := val.([]string); ok {
val, err = arrutil.StringsToInts(ss)
} else {
err = errInvalidParam
}
}
return val, err
}
// check val is string
var str string
// up: support filter pointer string value
if poStr, ok := val.(*string); ok {
str = *poStr
} else if str, ok = val.(string); !ok {
return nil, fmt.Errorf("filter: '%s' only use for string type, input %T", name, val)
}
// val is must be string.
switch realName {
case "bool":
val, err = strutil.ToBool(str)
case "trim":
val = strutil.Trim(str, args...)
case "trimLeft":
val = strutil.TrimLeft(str, args...)
case "trimRight":
val = strutil.TrimRight(str, args...)
case "title":
val = Title(str)
case "email":
val = strutil.FilterEmail(str)
case "substr":
val = strutil.Substr(str, MustInt(args[0]), MustInt(args[1]))
case "lower":
val = strutil.Lowercase(str)
case "upper":
val = strutil.Uppercase(str)
case "lowerFirst":
val = strutil.LowerFirst(str)
case "upperFirst":
val = strutil.UpperFirst(str)
case "upperWord":
val = strutil.UpperWord(str)
case "snakeCase":
val = strutil.SnakeCase(str, args...)
case "camelCase":
val = strutil.CamelCase(str, args...)
case "URLEncode":
val = strutil.URLEncode(str)
case "URLDecode":
val = strutil.URLDecode(str)
case "escapeJS":
val = strutil.EscapeJS(str)
case "escapeHTML":
val = strutil.EscapeHTML(str)
case "strToInts":
val, err = strutil.ToInts(str, args...)
case "strToSlice":
val = strutil.ToSlice(str, args...)
case "strToTime":
val, err = strutil.ToTime(str, args...)
}
return val, err
}
// GetByPath get value from a map[string]any. eg "top" "top.sub"
func GetByPath(key string, mp map[string]any) (any, bool) {
return maputil.GetByPath(key, mp)
}
func parseArgString(argStr string) (ss []string) {
if argStr == "" { // no arg
return
}
if len(argStr) == 1 { // one char
return []string{argStr}
}
return strutil.Split(argStr, ",")
}