-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
87 lines (75 loc) · 1.49 KB
/
util.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
package tail
import (
"github.com/vela-ssoc/vela-kit/grep"
"github.com/vela-ssoc/vela-kit/kind"
"github.com/vela-ssoc/vela-kit/lua"
)
func compile(v string) func(string) bool {
m := func(_ string) bool { return true }
if v == "*" {
return m
}
g, err := grep.Compile(v, nil)
if err != nil {
m = func(_ string) bool { return false }
} else {
m = g.Match
}
return m
}
func newJson(val lua.LValue) func([]byte) []byte {
switch val.Type() {
case lua.LTTable:
ex := val.(*lua.LTable)
return func(v []byte) []byte {
buf := kind.NewJsonEncoder()
buf.Tab("")
ex.Range(func(key string, val lua.LValue) {
buf.KV(key, val.String())
})
buf.KV("msg", v)
buf.End("}")
return buf.Bytes()
}
default:
return nil
}
}
func newAddJson(val lua.LValue) func([]byte) []byte {
switch val.Type() {
case lua.LTString:
return func(v []byte) []byte {
n := len(v)
ch := v[n-1]
v[n-1] = ','
v = append(v, lua.S2B(val.String())...)
v = append(v, ch)
return v
}
case lua.LTTable:
ex := val.(*lua.LTable)
return func(v []byte) []byte {
n := len(v)
ch := v[n-1]
v[n-1] = ','
buff := kind.NewJson(v)
ex.Range(func(key string, val lua.LValue) {
buff.KV(key, val.String())
})
v = append(v, ch)
return v
}
default:
return nil
}
}
func newAddRaw(val lua.LValue) func([]byte) []byte {
switch val.Type() {
case lua.LTString:
return func(v []byte) []byte {
return append(v, lua.S2B(val.String())...)
}
default:
return nil
}
}