-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathparser.go
288 lines (228 loc) · 7.29 KB
/
parser.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package roulette
import (
"encoding/xml"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
"unicode"
"github.com/Masterminds/sprig"
"github.com/myntra/roulette/log"
)
// default delimeters
var delimLeft = "<r>"
var delimRight = "</r>"
var defaultSameTypeIndex = map[int]string{
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
}
// Parser interface provides methods for the executor to update the tree, execute the values and get result.
type Parser interface {
Execute(vals interface{})
GetResult() Result
}
// XMLData contains the parsed roulette xml tree
type XMLData struct {
Name xml.Name `xml:"roulette"`
Rulesets []TextTemplateRuleset `xml:"ruleset"`
}
// TextTemplateParser holds the rules from a rule file
type TextTemplateParser struct {
xml XMLData
config TextTemplateParserConfig
defaultFuncs template.FuncMap
bytesBuf *bytesPool
mapBuf *mapPool
sameTypeIndex map[int]string
}
// Execute executes the parser's rulesets
func (p TextTemplateParser) Execute(vals interface{}) {
var err error
for i := range p.xml.Rulesets {
err = p.xml.Rulesets[i].Execute(vals)
if err != nil {
log.Warn(err)
}
}
}
// GetResult returns the parser's result.
func (p TextTemplateParser) GetResult() Result {
return p.config.Result
}
// Compile compiles the parser's rulesets
func (p *TextTemplateParser) compile() error {
replacer := strings.NewReplacer(" ", "", "*", " ")
newLineReplacer := strings.NewReplacer("\n", "")
var _ Ruleset = TextTemplateRuleset{}
for i := range p.xml.Rulesets {
if p.xml.Rulesets[i].FilterTypes == "" {
return fmt.Errorf("Missing required attribute filterTypes")
}
if p.xml.Rulesets[i].DataKey == "" {
return fmt.Errorf("Missing required attribute dataKey")
}
for i, rune := range p.xml.Rulesets[i].FilterTypes {
if !unicode.IsLetter(rune) && i == 0 {
return fmt.Errorf("First character of filterTypes is not a letter")
}
if i == 0 {
break
}
}
// assign buffer pool
p.xml.Rulesets[i].bytesBuf = p.bytesBuf
p.xml.Rulesets[i].mapBuf = p.mapBuf
p.xml.Rulesets[i].sameTypeIndex = p.sameTypeIndex
// limit
if p.xml.Rulesets[i].PrioritiesCount == "all" || p.xml.Rulesets[i].PrioritiesCount == "" {
p.xml.Rulesets[i].limit = len(p.xml.Rulesets[i].Rules)
} else {
prioritiesCount, err := strconv.ParseInt(p.xml.Rulesets[i].PrioritiesCount, 10, 32)
if err != nil {
p.xml.Rulesets[i].limit = len(p.xml.Rulesets[i].Rules)
} else {
p.xml.Rulesets[i].limit = int(prioritiesCount)
}
}
// split filter types
typeName := replacer.Replace(p.xml.Rulesets[i].FilterTypes)
filterTypesArr := strings.Split(typeName, ",")
sort.Strings(filterTypesArr)
regex := regexp.MustCompile(p.xml.Rulesets[i].Workflow)
workflowMatch := true
if len(p.config.WorkflowPattern) > 0 && len(p.xml.Rulesets[i].Workflow) > 0 {
if p.config.IsWildcardWorkflowPattern {
if !wildcardMatcher(p.xml.Rulesets[i].Workflow, p.config.WorkflowPattern) {
workflowMatch = false
}
} else {
// if a regex is a no match
if !regex.MatchString(p.config.WorkflowPattern) {
workflowMatch = false
}
}
}
textTemplateRulesetConfig := textTemplateRulesetConfig{
result: p.config.Result,
filterTypesArr: filterTypesArr,
workflowMatch: workflowMatch,
}
p.xml.Rulesets[i].config = textTemplateRulesetConfig
if p.xml.Rulesets[i].ResultKey == "" {
p.xml.Rulesets[i].ResultKey = "result"
}
resultAllowed := true
if p.xml.Rulesets[i].config.result == nil {
resultAllowed = false
}
// set rule config
for j := range p.xml.Rulesets[i].Rules {
ruleConfig := ruleConfig{
delimLeft: p.config.DelimLeft,
delimRight: p.config.DelimRight,
defaultfuncs: p.defaultFuncs,
userfuncs: p.config.Userfuncs,
}
p.xml.Rulesets[i].Rules[j].config = ruleConfig
// remove all new lines from the expression
p.xml.Rulesets[i].Rules[j].Expr = newLineReplacer.Replace(p.xml.Rulesets[i].Rules[j].Expr)
if strings.Contains(p.xml.Rulesets[i].Rules[j].Expr, p.xml.Rulesets[i].ResultKey) && !resultAllowed {
p.xml.Rulesets[i].Rules[j].config.noResultFunc = true
}
// set expcted types for a rule
for _, typeName := range p.xml.Rulesets[i].config.filterTypesArr {
if strings.Contains(p.xml.Rulesets[i].Rules[j].Expr, typeName) {
p.xml.Rulesets[i].Rules[j].config.expectTypes = append(p.xml.Rulesets[i].Rules[j].config.expectTypes, typeName)
}
}
p.xml.Rulesets[i].Rules[j].config.expectTypesErr = fmt.Errorf("rule expression expected types %s",
p.xml.Rulesets[i].Rules[j].config.expectTypes)
p.xml.Rulesets[i].Rules[j].config.allfuncs = template.FuncMap{}
sort.Strings(p.xml.Rulesets[i].Rules[j].config.expectTypes)
for k, v := range p.xml.Rulesets[i].Rules[j].config.defaultfuncs {
p.xml.Rulesets[i].Rules[j].config.allfuncs[k] = v
}
// append Masterminds/sprig funcs
for k, v := range sprig.FuncMap() {
p.xml.Rulesets[i].Rules[j].config.allfuncs[k] = v
}
for k, v := range p.xml.Rulesets[i].Rules[j].config.userfuncs {
p.xml.Rulesets[i].Rules[j].config.allfuncs[k] = v
}
tmpl, err := template.
New(p.xml.Rulesets[i].Rules[j].Name).Delims(
p.xml.Rulesets[i].Rules[j].config.delimLeft, p.xml.Rulesets[i].Rules[j].config.delimRight).
Funcs(p.xml.Rulesets[i].Rules[j].config.allfuncs).
Parse(p.xml.Rulesets[i].Rules[j].Expr)
p.xml.Rulesets[i].Rules[j].config.template = tmpl
p.xml.Rulesets[i].Rules[j].config.templateErr = err
}
sort.Sort(p.xml.Rulesets[i])
}
return nil
}
// TextTemplateParserConfig sets the optional config for the TextTemplateParser
type TextTemplateParserConfig struct {
Userfuncs template.FuncMap
DelimLeft string
DelimRight string
WorkflowPattern string // filter rulesets based on the pattern
Result Result
IsWildcardWorkflowPattern bool
LogLevel string //info, debug, warn, error, fatal. default is info
LogPath string //stdout, /path/to/file . default is stdout
}
// NewTextTemplateParser returns a new roulette format xml parser.
func NewTextTemplateParser(data []byte, config TextTemplateParserConfig) (Parser, error) {
if config.DelimLeft == "" {
config.DelimLeft = delimLeft
config.DelimRight = delimRight
}
if config.Userfuncs == nil {
config.Userfuncs = template.FuncMap{}
} else {
err := validateFuncs(config.Userfuncs)
if err != nil {
return nil, err
}
}
if config.LogLevel == "" {
config.LogLevel = "info"
}
if config.LogPath == "" {
config.LogPath = "stdout"
}
xmldata := XMLData{}
err := xml.Unmarshal(data, &xmldata)
if err != nil {
return nil, err
}
parser := TextTemplateParser{
config: config,
defaultFuncs: defaultFuncMap,
xml: xmldata,
bytesBuf: newBytesPool(),
mapBuf: newMapPool(),
sameTypeIndex: defaultSameTypeIndex,
}
// compile rulesets
err = parser.compile()
if err != nil {
return nil, err
}
log.Init(config.LogLevel, config.LogPath)
return parser, nil
}
// NewParser returns a TextTemplateParser with default config
func NewParser(data []byte, config ...TextTemplateParserConfig) (Parser, error) {
cfg := TextTemplateParserConfig{}
if len(config) > 0 {
cfg = config[0]
}
return NewTextTemplateParser(data, cfg)
}