-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextractor_test.go
159 lines (140 loc) · 3.34 KB
/
extractor_test.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
package logpeck
import (
"fmt"
"testing"
lua "github.com/yuin/gopher-lua"
)
func TestExtractor(*testing.T) {
confStr := `{
"Name":"Lua",
"Config":{
"LuaString":"function conv(s) local ret = {} ret['haha'] = string.sub(s, 2, -2) return ret end" }
}
}`
config, err := NewExtractorConfig(confStr)
if err != nil {
panic(err)
}
fmt.Printf("[NewExtractorConfig]%#v\n", config)
extractor, err := NewExtractor(config)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", extractor)
}
func TestLuaExtractorConfig(*testing.T) {
confStr := `{ "LuaString":"function conv(s) local ret = {} ret['haha'] = string.sub(s, 2, -2) return ret end" }`
config, err := NewLuaExtractorConfig([]byte(confStr))
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", config)
extractor, err := NewLuaExtractor(config)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", extractor)
}
func TestLuaExtractor(*testing.T) {
confStr := `{ "LuaString":"function extract(s) local ret = {} ret['haha'] = string.sub(s, 2, -2) return ret end","Fields":[{"Name":"haha"}] }`
config, err := NewLuaExtractorConfig([]byte(confStr))
if err != nil {
panic(err)
}
le, err := NewLuaExtractor(config)
if err != nil {
panic(err)
}
defer le.Close()
ret, err := le.Extract("12345678")
if err != nil || ret["haha"] != "234567" {
fmt.Printf("-------------%v\n", err)
panic(err)
}
fmt.Printf("%#v\n", ret)
ret, err = le.Extract("87654321")
if err != nil || ret["haha"] != "765432" {
panic(err)
}
fmt.Printf("%#v\n", ret)
}
func TestLua(*testing.T) {
luaStr := `
function conv(s)
local ret = {}
ret["haha"] = string.sub(s, 2, -2)
return ret
end`
testStr := `12345678`
L := lua.NewState()
defer L.Close()
if err := L.DoString(luaStr); err != nil {
panic(err)
}
if err := L.CallByParam(lua.P{
Fn: L.GetGlobal("conv"),
NRet: 1,
Protect: true,
}, lua.LString(testStr)); err != nil {
panic(err)
}
ret := L.Get(-1).(*lua.LTable)
L.Pop(1)
fmt.Println(ret.Type())
fmt.Println(ret.RawGetString("haha"))
}
func TestTextExtractor(*testing.T) {
confStr := `{ "Delimiters":" " ,"fields":[{"Name": "col2", "Value": "$2"},{"Name": "col3", "Value": "$3"},{"Name": "col4", "Value": "$4"}]}`
config, err := NewTextExtractorConfig([]byte(confStr))
if err != nil {
panic(err)
}
fmt.Printf("[NewTextExtractorConfig] %#v\n", config)
extractor, err := NewTextExtractor(config)
if err != nil {
panic(err)
}
fmt.Printf("[NewTextExtractor] %#v\n", extractor)
content := "this is an text extractor"
m, err := extractor.Extract(content)
if err != nil {
panic(err)
}
if m["col2"] != "is" || m["col3"] != "an" || m["col4"] != "text" {
panic(m)
}
fmt.Printf("[Extract] %#v\n", m)
}
func TestJSONExtractor(*testing.T) {
confStr := `{"fields":[{"Name": "k1"},{"Name": "k2.1"},{"Name": "k3.2.3"}]}`
config, err := NewJSONExtractorConfig([]byte(confStr))
if err != nil {
panic(err)
}
fmt.Printf("[NewJSONExtractorConfig] %#v\n", config)
extractor, err := NewJSONExtractor(config)
if err != nil {
panic(err)
}
fmt.Printf("[NewJSONExtractor] %#v\n", extractor)
content := `{
"k1":"v1",
"k2":{
"1":"v2"
},
"k3":{
"2":{
"3":"v3"
}
},
"k4":"v4"
}`
m, err := extractor.Extract(content)
if err != nil {
panic(err)
}
if m["k1"] != "v1" || m["k2.1"] != "v2" || m["k3.2.3"] != "v3" {
panic(m)
}
fmt.Printf("[Extract] %#v\n", m)
}