-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlogstash_config_helper.go
663 lines (566 loc) · 17.5 KB
/
logstash_config_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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package config
//go:generate pigeon -nolint -optimize-grammar -o logstash_config.go logstash_config.peg
import (
"bytes"
"fmt"
"strconv"
"strings"
"github.com/breml/logstash-config/ast"
)
type exceptionalCommentsWarnings []string
func (w exceptionalCommentsWarnings) Clone() interface{} {
clone := make(exceptionalCommentsWarnings, 0, len(w))
clone = append(clone, w...)
return clone
}
func (c *current) initState() error {
if _, ok := c.state[exceptionalCommentWarning]; !ok {
warnings := make(exceptionalCommentsWarnings, 0)
c.state[exceptionalCommentWarning] = warnings
}
return nil
}
func (c *current) initParser() (bool, error) {
farthestFailure = []errPos{}
return true, nil
}
func (c *current) ret(el interface{}) (interface{}, error) {
return el, nil
}
func (c *current) retConfig(el interface{}) (interface{}, error) {
conf, _ := el.(ast.Config)
warnings, _ := c.state[exceptionalCommentWarning].(exceptionalCommentsWarnings)
conf.Warnings = warnings
return conf, nil
}
func (c *current) commentBlock(comment1 interface{}, spaceBefore bool, spaceAfter bool) ast.CommentBlock {
comments1 := toIfaceSlice(comment1)
var comments []ast.Comment
for _, icb1 := range comments1 {
switch t := icb1.(type) {
case []ast.Comment:
// If spaceBefore is enabled and it is the first comment and there are
// actualy comment in t, enable space before for the first comment.
if spaceBefore && len(comments) == 0 && len(t) > 0 {
t[0].SpaceBefore = true
}
comments = append(comments, t...)
case ast.Whitespace:
// If spaceAfter is preserved.
if len(comments) > 0 && spaceAfter {
comments[len(comments)-1].SpaceAfter = true
continue
}
}
}
return comments
}
func (c *current) configSection(ps1, psComment1 interface{}) (ast.PluginSection, error) {
ps, ok := ps1.(ast.PluginSection)
if !ok {
return ast.PluginSection{}, fmt.Errorf("Value is not a PluginSection: %#v", ps1)
}
psComments := c.commentBlock(psComment1, false, false)
ps.CommentBlock = psComments
return ps, nil
}
func (c *current) config(ps1, pss1, psComment1, footerComment1 interface{}) (ast.Config, error) {
var (
input []ast.PluginSection
filter []ast.PluginSection
output []ast.PluginSection
)
ips := toIfaceSlice(ps1)
ips = append(ips, toIfaceSlice(pss1)...)
psComment := c.commentBlock(psComment1, false, true)
footerComments := c.commentBlock(footerComment1, true, false)
first := true
for _, ips1 := range ips {
if ps, ok := ips1.(ast.PluginSection); ok {
if first {
ps.CommentBlock = psComment
first = false
}
switch ps.PluginType {
case ast.Input:
input = append(input, ps)
case ast.Filter:
filter = append(filter, ps)
case ast.Output:
output = append(output, ps)
default:
return ast.Config{}, fmt.Errorf("PluginType is not supported: %#v", ps)
}
} else {
return ast.Config{}, fmt.Errorf("Value is not a PluginSection: %#v", ips1)
}
}
return ast.Config{
Input: input,
Filter: filter,
Output: output,
FooterComment: footerComments,
}, nil
}
func (c *current) warnComment() error {
ok, _ := c.globalStore[exceptionalCommentWarning].(bool)
if ok && bytes.Contains(c.text, []byte("#")) {
warnings, _ := c.state[exceptionalCommentWarning].(exceptionalCommentsWarnings)
warnings = append(warnings, fmt.Sprintf("exceptional comment at %s", c.pos))
c.state[exceptionalCommentWarning] = warnings
}
return nil
}
func (c *current) whitespace() (ast.Whitespace, error) {
return ast.Whitespace{}, nil
}
func (c *current) comment() ([]ast.Comment, error) {
if ignoreComment, ok := c.globalStore[ignoreComment].(bool); ok && ignoreComment {
return nil, nil
}
lines := strings.Split(strings.Trim(strings.ReplaceAll(string(c.text), "\r", ""), "\n"), "\n")
var commentLines []ast.Comment
var space bool
for i, line := range lines {
idx := strings.Index(line, "#")
if idx == -1 {
if i > 0 {
space = true
}
continue
}
var c string
if len(line) > idx+1 {
c = string(line[idx+1:])
}
c = strings.Trim(c, " ")
commentLines = append(commentLines, ast.NewComment(c, space))
space = false
}
return commentLines, nil
}
func (c *current) pluginSection(pt1, bops1, footerComment1 interface{}) (ast.PluginSection, error) {
pt := ast.PluginType(pt1.(int))
ibops := toIfaceSlice(bops1)
var bops []ast.BranchOrPlugin
for _, bop := range ibops {
bop := bop.(ast.BranchOrPlugin)
bops = append(bops, bop)
}
return ast.PluginSection{
Start: c.astPos(),
PluginType: pt,
BranchOrPlugins: bops,
FooterComment: c.commentBlock(footerComment1, false, false),
}, nil
}
func (c *current) plugin(name1, attributes1, comment1, footerComment1 interface{}) (ast.Plugin, error) {
var attributes []ast.Attribute
if attributes1 != nil {
attributes = attributes1.([]ast.Attribute)
}
name := name1.(ast.StringAttribute)
p := ast.NewPlugin(name.ValueString(), attributes...)
p.Start = name.Pos()
p.Comment = c.commentBlock(comment1, false, false)
p.FooterComment = c.commentBlock(footerComment1, false, false)
return p, nil
}
func (c *current) attributes(attribute1, attributes1, comment1 interface{}) ([]ast.Attribute, error) {
attribute, _ := c.attributeComment(attribute1, comment1, false)
iattributes := toIfaceSlice(attribute)
iattributes = append(iattributes, toIfaceSlice(attributes1)...)
var attributes []ast.Attribute
for _, attr := range iattributes {
if attr, ok := attr.(ast.Attribute); ok {
attributes = append(attributes, attr)
} else {
return nil, fmt.Errorf("Argument is not an attribute: %#v", attr)
}
}
return attributes, nil
}
func (c *current) attributeComment(attribute1, comment1 interface{}, spaceBefore bool) (ast.Attribute, error) {
var attribute ast.Attribute
switch attr := attribute1.(type) {
case ast.StringAttribute:
attr.Comment = c.commentBlock(comment1, spaceBefore, false)
attribute = attr
case ast.NumberAttribute:
attr.Comment = c.commentBlock(comment1, spaceBefore, false)
attribute = attr
case ast.ArrayAttribute:
attr.Comment = c.commentBlock(comment1, spaceBefore, false)
attribute = attr
case ast.HashAttribute:
attr.Comment = c.commentBlock(comment1, spaceBefore, false)
attribute = attr
case ast.PluginAttribute:
attr.Comment = c.commentBlock(comment1, spaceBefore, false)
attribute = attr
default:
return nil, fmt.Errorf("Unsupported attribute type %#v", attribute1)
}
return attribute, nil
}
func (c *current) attribute(name, value interface{}) (ast.Attribute, error) {
var key ast.StringAttribute
switch name := name.(type) {
case ast.StringAttribute:
key = name
case string:
key = ast.NewStringAttribute("", name, ast.Bareword)
default:
return nil, fmt.Errorf("Type for attribute name is not supported: %#v", name)
}
switch value := value.(type) {
case ast.StringAttribute:
sa := ast.NewStringAttribute(key.ValueString(), value.Value(), value.StringAttributeType())
sa.Start = c.astPos()
return sa, nil
case ast.NumberAttribute:
na := ast.NewNumberAttribute(key.ValueString(), value.Value())
na.Start = c.astPos()
return na, nil
case ast.ArrayAttribute:
aa := ast.NewArrayAttribute(key.ValueString(), value.Value()...)
aa.Start = c.astPos()
aa.FooterComment = value.FooterComment
return aa, nil
case ast.HashAttribute:
ha := ast.NewHashAttribute(key.ValueString(), value.Value()...)
ha.Start = c.astPos()
ha.FooterComment = value.FooterComment
return ha, nil
case ast.Plugin:
pa := ast.NewPluginAttribute(key.ValueString(), value)
pa.Start = c.astPos()
return pa, nil
default:
return nil, fmt.Errorf("Type of value %#v with name %s is not supported", value, key.ValueString())
}
}
func (c *current) string(sat ast.StringAttributeType) (ast.StringAttribute, error) {
var s ast.StringAttribute
if sat == ast.Bareword {
s = ast.NewStringAttribute("", string(c.text), sat)
} else {
str, _ := c.enclosedValue()
s = ast.NewStringAttribute("", str, sat)
}
s.Start = c.astPos()
return s, nil
}
func (c *current) regexp() (ast.Regexp, error) {
val, _ := c.enclosedValue()
r := ast.NewRegexp(val)
r.Start = c.astPos()
return r, nil
}
func (c *current) number() (ast.NumberAttribute, error) {
f, err := strconv.ParseFloat(string(c.text), 64)
if err != nil {
// TODO: is this possible to happen? are all values, which are valid floats in Logstash/Ruby also valid floats in Go?
return ast.NumberAttribute{}, err
}
return ast.NewNumberAttribute("", f), nil
}
func (c *current) array(attributes1, footerComment1 interface{}) (ast.ArrayAttribute, error) {
var attributes []ast.Attribute
if attributes1 != nil {
attributes = attributes1.([]ast.Attribute)
}
a := ast.NewArrayAttribute("", attributes...)
a.FooterComment = c.commentBlock(footerComment1, false, false)
return a, nil
}
func (c *current) hash(attributes1, footerComment1 interface{}) (ast.HashAttribute, error) {
var hashentries []ast.HashEntry
if attributes1 != nil {
hashentries = attributes1.([]ast.HashEntry)
}
a := ast.NewHashAttribute("", hashentries...)
a.FooterComment = c.commentBlock(footerComment1, false, false)
return a, nil
}
func (c *current) hashentries(attribute, attributes1 interface{}) ([]ast.HashEntry, error) {
entry := attribute.(ast.HashEntry)
if len(entry.Comment) > 0 {
entry.Comment[0].SpaceBefore = false
}
iattributes := toIfaceSlice(entry)
iattributes = append(iattributes, toIfaceSlice(attributes1)...)
var attributes []ast.HashEntry
for _, attr := range iattributes {
if attr, ok := attr.(ast.HashEntry); ok {
attributes = append(attributes, attr)
} else {
return nil, fmt.Errorf("Argument is not an attribute")
}
}
return attributes, nil
}
func (c *current) hashentry(name, value, comment interface{}) (ast.HashEntry, error) {
key := name.(ast.HashEntryKey)
he := ast.NewHashEntry(key, value.(ast.Attribute))
he.Start = c.astPos()
he.Comment = c.commentBlock(comment, true, false)
return he, nil
}
func (c *current) elseIfComment(eib1, eibComment1 interface{}) (ast.ElseIfBlock, error) {
eib := eib1.(ast.ElseIfBlock)
eib.Comment = c.commentBlock(eibComment1, false, false)
return eib, nil
}
func (c *current) elseComment(eb1, ebComment1 interface{}) (ast.ElseBlock, error) {
eb := eb1.(ast.ElseBlock)
eb.Comment = c.commentBlock(ebComment1, false, false)
return eb, nil
}
func (c *current) branch(ifBlock1, elseIfBlocks1, elseBlock1, ifComment interface{}) (ast.Branch, error) {
ielseIfBlocks := toIfaceSlice(elseIfBlocks1)
var elseIfBlocks []ast.ElseIfBlock
for _, elseIfBlock := range ielseIfBlocks {
if elseIfBlock, ok := elseIfBlock.(ast.ElseIfBlock); ok {
elseIfBlocks = append(elseIfBlocks, elseIfBlock)
} else {
return ast.Branch{}, fmt.Errorf("Argument is not an elseIfBlock: %#v", elseIfBlock)
}
}
var elseBlock ast.ElseBlock
if elseBlock1 != nil {
elseBlock = elseBlock1.(ast.ElseBlock)
}
ifBlock := ifBlock1.(ast.IfBlock)
ifBlock.Comment = c.commentBlock(ifComment, false, false)
return ast.NewBranch(ifBlock, elseBlock, elseIfBlocks...), nil
}
func (c *current) branchOrPluginComment(bop1, comment1 interface{}) (ast.BranchOrPlugin, error) {
var eop ast.BranchOrPlugin
switch t := bop1.(type) {
case ast.Plugin:
t.Comment = c.commentBlock(comment1, false, false)
eop = t
case ast.Branch:
t.IfBlock.Comment = c.commentBlock(comment1, false, false)
eop = t
default:
return nil, fmt.Errorf("invalid value for if block")
}
return eop, nil
}
func (c *current) ifBlock(cond, bops, comment1 interface{}) (ast.IfBlock, error) {
ib := ast.NewIfBlock(cond.(ast.Condition), c.branchOrPlugins(bops)...)
ib.Start = c.astPos()
ib.FooterComment = c.commentBlock(comment1, false, false)
return ib, nil
}
func (c *current) elseIfBlock(cond, bops, comment1 interface{}) (ast.ElseIfBlock, error) {
eib := ast.NewElseIfBlock(cond.(ast.Condition), c.branchOrPlugins(bops)...)
eib.Start = c.astPos()
eib.FooterComment = c.commentBlock(comment1, false, false)
return eib, nil
}
func (c *current) elseBlock(bops, comment1 interface{}) (ast.ElseBlock, error) {
eb := ast.NewElseBlock(c.branchOrPlugins(bops)...)
eb.Start = c.astPos()
eb.FooterComment = c.commentBlock(comment1, false, false)
return eb, nil
}
func (c *current) branchOrPlugins(bops1 interface{}) []ast.BranchOrPlugin {
bops := toIfaceSlice(bops1)
var branchOrPlugins []ast.BranchOrPlugin
for _, bop := range bops {
if bop, ok := bop.(ast.BranchOrPlugin); ok {
branchOrPlugins = append(branchOrPlugins, bop)
} else {
return []ast.BranchOrPlugin{}
}
}
return branchOrPlugins
}
func (c *current) condition(expr, exprs interface{}) (ast.Condition, error) {
iexprs := toIfaceSlice(expr)
iexprs = append(iexprs, toIfaceSlice(exprs)...)
var expressions []ast.Expression
for _, ex := range iexprs {
if ex, ok := ex.(ast.Expression); ok {
expressions = append(expressions, ex)
} else {
return ast.Condition{}, fmt.Errorf("Argument is not an expression: %#v", ex)
}
}
return ast.NewCondition(expressions...), nil
}
func (c *current) expression(bo, expr1 interface{}) (ast.Expression, error) {
expr := expr1.(ast.Expression)
expr.SetBoolOperator(bo.(ast.BooleanOperator))
return expr, nil
}
func (c *current) conditionExpression(cond interface{}) (ast.ConditionExpression, error) {
ce := ast.NewConditionExpression(ast.BooleanOperator{Op: ast.NoOperator}, cond.(ast.Condition))
ce.Start = c.astPos()
return ce, nil
}
func (c *current) negativeExpression(cond interface{}) (ast.NegativeConditionExpression, error) {
ne := ast.NewNegativeConditionExpression(ast.BooleanOperator{Op: ast.NoOperator}, cond.(ast.Condition))
ne.Start = c.astPos()
return ne, nil
}
func (c *current) negativeSelector(sel interface{}) (ast.NegativeSelectorExpression, error) {
ns := ast.NewNegativeSelectorExpression(ast.BooleanOperator{Op: ast.NoOperator}, sel.(ast.Selector))
ns.Start = c.astPos()
return ns, nil
}
func (c *current) inExpression(lv, rv interface{}) (ast.InExpression, error) {
ie := ast.NewInExpression(ast.BooleanOperator{Op: ast.NoOperator}, lv.(ast.Rvalue), rv.(ast.Rvalue))
ie.Start = c.astPos()
return ie, nil
}
func (c *current) notInExpression(lv, rv interface{}) (ast.NotInExpression, error) {
nie := ast.NewNotInExpression(ast.BooleanOperator{Op: ast.NoOperator}, lv.(ast.Rvalue), rv.(ast.Rvalue))
nie.Start = c.astPos()
return nie, nil
}
func (c *current) compareExpression(lv, co, rv interface{}) (ast.CompareExpression, error) {
ce := ast.NewCompareExpression(ast.BooleanOperator{Op: ast.NoOperator}, lv.(ast.Rvalue), co.(ast.CompareOperator), rv.(ast.Rvalue))
ce.Start = c.astPos()
return ce, nil
}
func (c *current) regexpExpression(lv, ro, rv interface{}) (ast.RegexpExpression, error) {
re := ast.NewRegexpExpression(ast.BooleanOperator{Op: ast.NoOperator}, lv.(ast.Rvalue), ro.(ast.RegexpOperator), rv.(ast.StringOrRegexp))
re.Start = c.astPos()
return re, nil
}
func (c *current) rvalue(rv interface{}) (ast.RvalueExpression, error) {
re := ast.NewRvalueExpression(ast.BooleanOperator{Op: ast.NoOperator}, rv.(ast.Rvalue))
re.Start = c.astPos()
return re, nil
}
func (c *current) compareOperator() (ast.CompareOperator, error) {
switch string(c.text) {
case "==":
return ast.CompareOperator{
Op: ast.Equal,
Start: c.astPos(),
}, nil
case "!=":
return ast.CompareOperator{
Op: ast.NotEqual,
Start: c.astPos(),
}, nil
case "<=":
return ast.CompareOperator{
Op: ast.LessOrEqual,
Start: c.astPos(),
}, nil
case ">=":
return ast.CompareOperator{
Op: ast.GreaterOrEqual,
Start: c.astPos(),
}, nil
case "<":
return ast.CompareOperator{
Op: ast.LessThan,
Start: c.astPos(),
}, nil
case ">":
return ast.CompareOperator{
Op: ast.GreaterThan,
Start: c.astPos(),
}, nil
}
return ast.CompareOperator{
Op: ast.Undefined,
Start: c.astPos(),
}, nil
}
func (c *current) regexpOperator() (ast.RegexpOperator, error) {
switch string(c.text) {
case "=~":
return ast.RegexpOperator{
Op: ast.RegexpMatch,
Start: c.astPos(),
}, nil
case "!~":
return ast.RegexpOperator{
Op: ast.RegexpNotMatch,
Start: c.astPos(),
}, nil
}
return ast.RegexpOperator{
Op: ast.Undefined,
Start: c.astPos(),
}, nil
}
func (c *current) booleanOperator() (ast.BooleanOperator, error) {
switch string(c.text) {
case "and":
return ast.BooleanOperator{
Op: ast.And,
Start: c.astPos(),
}, nil
case "or":
return ast.BooleanOperator{
Op: ast.Or,
Start: c.astPos(),
}, nil
case "xor":
return ast.BooleanOperator{
Op: ast.Xor,
Start: c.astPos(),
}, nil
case "nand":
return ast.BooleanOperator{
Op: ast.Nand,
Start: c.astPos(),
}, nil
}
return ast.BooleanOperator{
Op: ast.Undefined,
Start: c.astPos(),
}, nil
}
func (c *current) selector(ses1 interface{}) (ast.Selector, error) {
ises := toIfaceSlice(ses1)
var ses []ast.SelectorElement
for _, se := range ises {
ses = append(ses, se.(ast.SelectorElement))
}
s := ast.NewSelector(ses)
s.Start = c.astPos()
return s, nil
}
func (c *current) selectorElement() (ast.SelectorElement, error) {
value := string(c.text)
se := ast.NewSelectorElement(value[1 : len(value)-1])
se.Start = c.astPos()
return se, nil
}
func (c *current) enclosedValue() (string, error) {
return string(c.text[1 : len(c.text)-1]), nil
}
func (c *current) str() (string, error) {
return string(c.text), nil
}
func (c *current) astPos() ast.Pos {
p := ast.Pos{
Line: c.pos.line,
Column: c.pos.col,
Offset: c.pos.offset,
}
return p
}
func toIfaceSlice(v interface{}) []interface{} {
if v == nil {
return nil
}
switch v1 := v.(type) {
case []interface{}:
return v1
case interface{}:
return []interface{}{v1}
default:
return nil
}
}