-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfilter.go
184 lines (155 loc) · 3.62 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package portal
import (
"encoding/json"
"strings"
"sync"
"github.com/pkg/errors"
)
var (
errUnmatchedBrackets = errors.New("unmatched brackets")
errPrefixIsNotBracket = errors.New("filter string must starts with '['")
)
var (
cachedFilterResultMap sync.Map
)
type filterNode struct {
Name string `json:"name"`
Parent *filterNode `json:"-"`
Children []*filterNode `json:"children"`
}
func (node *filterNode) String() string {
data, _ := json.MarshalIndent(node, "", " ")
return string(data)
}
type extractOption struct {
ignoreNodeWithChildren bool
queryByParentName string
queryByParentNameAlias string
}
func extractFilterNodeNames(nodes []*filterNode, opt *extractOption) []string {
if len(nodes) == 0 {
return nil
}
if opt == nil {
opt = &extractOption{}
}
names := make([]string, 0, len(nodes))
for _, n := range nodes {
var parentName string
if n.Parent != nil {
parentName = n.Parent.Name
}
if opt.queryByParentName != "" && parentName != opt.queryByParentName && parentName != opt.queryByParentNameAlias {
continue
}
if opt.ignoreNodeWithChildren && len(n.Children) > 0 {
continue
}
names = append(names, n.Name)
}
return names
}
func parseFilters(filters []string) (map[int][]*filterNode, error) {
return parseFilterString("[" + strings.Join(filters, ",") + "]")
}
// parseFilterString parses filter string to a filter tree (with extra levels).
// Example input:
// 1. [speaker[id,name]]
// 2. [speaker[id,name,vip_info[type,is_active]]]
func parseFilterString(s string) (map[int][]*filterNode, error) {
s = strings.TrimSpace(s)
if s == "" {
return nil, nil
}
if !strings.HasPrefix(s, "[") {
return nil, errPrefixIsNotBracket
}
cachedResult, ok := cachedFilterResultMap.Load(s)
if ok {
rv, _ := cachedResult.(map[int][]*filterNode)
return rv, nil
}
// don't care about non-ascii chars.
filterInBytes := []byte(s)
err := checkBracketPairs(filterInBytes)
if err != nil {
return nil, errors.WithStack(err)
}
result := doParse(filterInBytes)
cachedFilterResultMap.Store(s, result)
return result, nil
}
func checkBracketPairs(s []byte) error {
stack := newStack()
for _, c := range s {
switch c {
case '[':
stack.push(c)
case ']':
x, err := stack.pop()
if err != nil {
return errUnmatchedBrackets
}
if xc, ok := x.(byte); ok {
if xc == '[' {
continue
}
}
return errUnmatchedBrackets
default:
continue
}
}
if stack.size() > 0 {
return errUnmatchedBrackets
}
return nil
}
// doParse parses filter string to a filter tree.
// Note: stupid & ugly~
func doParse(s []byte) map[int][]*filterNode {
var (
wordBuf []byte
levelNodesMap = make(map[int][]*filterNode)
levelParentNodeMap = map[int]*filterNode{-1: nil}
level = -1
)
appendNodes := func() *filterNode {
if len(wordBuf) == 0 {
return nil
}
nthLevelNodes, ok := levelNodesMap[level]
if !ok || nthLevelNodes == nil {
nthLevelNodes = make([]*filterNode, 0)
}
node := &filterNode{Name: string(wordBuf), Parent: levelParentNodeMap[level]}
if node.Parent != nil {
node.Parent.Children = append(node.Parent.Children, node)
}
nthLevelNodes = append(nthLevelNodes, node)
levelNodesMap[level] = nthLevelNodes
wordBuf = make([]byte, 0)
return node
}
for _, char := range s {
switch char {
case '\t', '\n', ' ':
continue
case ',':
_ = appendNodes()
case '[':
node := appendNodes()
level++
if node != nil {
levelParentNodeMap[level] = node
}
case ']':
appendNodes()
level--
default:
wordBuf = append(wordBuf, char)
}
}
appendNodes()
return levelNodesMap
}