forked from vjeantet/grok
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.go
83 lines (71 loc) · 1.59 KB
/
tree.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
package grok
import (
"fmt"
)
type nodeP struct {
cnt string
ptn *GrokPattern
cNode []string
}
type path struct {
m map[string]struct{}
l []string
}
func (p *path) String() {
}
func runTree(m map[string]*nodeP) (map[string]*GrokPattern, map[string]string) {
ret := map[string]*GrokPattern{}
invalid := map[string]string{}
pt := &path{
m: map[string]struct{}{},
l: []string{},
}
for name, v := range m {
if err := dfs(ret, m, name, v, pt); err != nil {
invalid[name] = err.Error()
}
}
return ret, invalid
}
func dfs(deP map[string]*GrokPattern, top map[string]*nodeP, startName string, start *nodeP, pt *path) error {
if _, ok := pt.m[startName]; ok {
lineStr := ""
for _, k := range pt.l {
lineStr += k + " -> "
}
lineStr += startName
return fmt.Errorf("circular dependency: pattern %s", lineStr)
}
pt.m[startName] = struct{}{}
pt.l = append(pt.l, startName)
defer func() {
delete(pt.m, startName)
pt.l = pt.l[:len(pt.l)-1]
}()
if _, ok := deP[startName]; ok {
return nil
} else if len(start.cNode) == 0 {
if ptn, err := DenormalizePattern(start.cnt, PatternStorage{deP}); err != nil {
return err
} else {
deP[startName] = ptn
}
return nil
}
for _, name := range start.cNode {
cNode, ok := top[name]
if !ok || cNode == nil {
return fmt.Errorf("no pattern found for %%{%s}", name)
}
// 完成此 node 的编译
if err := dfs(deP, top, name, cNode, pt); err != nil {
return err
}
}
if ptn, err := DenormalizePattern(start.cnt, PatternStorage{deP}); err != nil {
return err
} else {
deP[startName] = ptn
}
return nil
}