正在显示
4 个修改的文件
包含
85 行增加
和
75 行删除
stash/filter/addurifieldfilter.go
0 → 100644
1 | +package filter | ||
2 | + | ||
3 | +import ( | ||
4 | + "strings" | ||
5 | + | ||
6 | + "github.com/globalsign/mgo/bson" | ||
7 | +) | ||
8 | + | ||
9 | +func AddUriFieldFilter(inField, outFirld string) FilterFunc { | ||
10 | + return func(m map[string]interface{}) map[string]interface{} { | ||
11 | + if val, ok := m[inField].(string); ok { | ||
12 | + var datas []string | ||
13 | + idx := strings.Index(val, "?") | ||
14 | + if idx < 0 { | ||
15 | + datas = strings.Split(val, "/") | ||
16 | + } else { | ||
17 | + datas = strings.Split(val[:idx], "/") | ||
18 | + } | ||
19 | + | ||
20 | + for i, data := range datas { | ||
21 | + if bson.IsObjectIdHex(data) { | ||
22 | + datas[i] = "*" | ||
23 | + } | ||
24 | + } | ||
25 | + | ||
26 | + m[outFirld] = strings.Join(datas, "/") | ||
27 | + } | ||
28 | + | ||
29 | + return m | ||
30 | + } | ||
31 | +} |
stash/filter/dropfilter.go
0 → 100644
1 | +package filter | ||
2 | + | ||
3 | +import ( | ||
4 | + "strings" | ||
5 | + | ||
6 | + "github.com/tal-tech/go-stash/stash/config" | ||
7 | +) | ||
8 | + | ||
9 | +func DropFilter(conds []config.Condition) FilterFunc { | ||
10 | + return func(m map[string]interface{}) map[string]interface{} { | ||
11 | + var qualify bool | ||
12 | + for _, cond := range conds { | ||
13 | + var qualifyOnce bool | ||
14 | + switch cond.Type { | ||
15 | + case typeMatch: | ||
16 | + qualifyOnce = cond.Value == m[cond.Key] | ||
17 | + case typeContains: | ||
18 | + if val, ok := m[cond.Key].(string); ok { | ||
19 | + qualifyOnce = strings.Contains(val, cond.Value) | ||
20 | + } | ||
21 | + } | ||
22 | + | ||
23 | + switch cond.Op { | ||
24 | + case opAnd: | ||
25 | + if !qualifyOnce { | ||
26 | + return m | ||
27 | + } else { | ||
28 | + qualify = true | ||
29 | + } | ||
30 | + case opOr: | ||
31 | + if qualifyOnce { | ||
32 | + qualify = true | ||
33 | + } | ||
34 | + } | ||
35 | + } | ||
36 | + | ||
37 | + if qualify { | ||
38 | + return nil | ||
39 | + } else { | ||
40 | + return m | ||
41 | + } | ||
42 | + } | ||
43 | +} |
1 | package filter | 1 | package filter |
2 | 2 | ||
3 | -import ( | ||
4 | - "strings" | ||
5 | - | ||
6 | - "github.com/globalsign/mgo/bson" | ||
7 | - "github.com/tal-tech/go-stash/stash/config" | ||
8 | -) | 3 | +import "github.com/tal-tech/go-stash/stash/config" |
9 | 4 | ||
10 | const ( | 5 | const ( |
11 | filterDrop = "drop" | 6 | filterDrop = "drop" |
@@ -32,72 +27,3 @@ func CreateFilters(c config.Config) []FilterFunc { | @@ -32,72 +27,3 @@ func CreateFilters(c config.Config) []FilterFunc { | ||
32 | 27 | ||
33 | return filters | 28 | return filters |
34 | } | 29 | } |
35 | - | ||
36 | -func DropFilter(conds []config.Condition) FilterFunc { | ||
37 | - return func(m map[string]interface{}) map[string]interface{} { | ||
38 | - var qualify bool | ||
39 | - for _, cond := range conds { | ||
40 | - var qualifyOnce bool | ||
41 | - switch cond.Type { | ||
42 | - case typeMatch: | ||
43 | - qualifyOnce = cond.Value == m[cond.Key] | ||
44 | - case typeContains: | ||
45 | - if val, ok := m[cond.Key].(string); ok { | ||
46 | - qualifyOnce = strings.Contains(val, cond.Value) | ||
47 | - } | ||
48 | - } | ||
49 | - | ||
50 | - switch cond.Op { | ||
51 | - case opAnd: | ||
52 | - if !qualifyOnce { | ||
53 | - return m | ||
54 | - } else { | ||
55 | - qualify = true | ||
56 | - } | ||
57 | - case opOr: | ||
58 | - if qualifyOnce { | ||
59 | - qualify = true | ||
60 | - } | ||
61 | - } | ||
62 | - } | ||
63 | - | ||
64 | - if qualify { | ||
65 | - return nil | ||
66 | - } else { | ||
67 | - return m | ||
68 | - } | ||
69 | - } | ||
70 | -} | ||
71 | - | ||
72 | -func RemoveFieldFilter(fields []string) FilterFunc { | ||
73 | - return func(m map[string]interface{}) map[string]interface{} { | ||
74 | - for _, field := range fields { | ||
75 | - delete(m, field) | ||
76 | - } | ||
77 | - return m | ||
78 | - } | ||
79 | -} | ||
80 | - | ||
81 | -func AddUriFieldFilter(inField, outFirld string) FilterFunc { | ||
82 | - return func(m map[string]interface{}) map[string]interface{} { | ||
83 | - if val, ok := m[inField].(string); ok { | ||
84 | - var datas []string | ||
85 | - idx := strings.Index(val, "?") | ||
86 | - if idx < 0 { | ||
87 | - datas = strings.Split(val, "/") | ||
88 | - } else { | ||
89 | - datas = strings.Split(val[:idx], "/") | ||
90 | - } | ||
91 | - | ||
92 | - for i, data := range datas { | ||
93 | - if bson.IsObjectIdHex(data) { | ||
94 | - datas[i] = "*" | ||
95 | - } | ||
96 | - } | ||
97 | - | ||
98 | - m[outFirld] = strings.Join(datas, "/") | ||
99 | - } | ||
100 | - | ||
101 | - return m | ||
102 | - } | ||
103 | -} |
-
请 注册 或 登录 后发表评论