-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextractor.go
64 lines (58 loc) · 1.37 KB
/
extractor.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
package logpeck
import (
"errors"
"strings"
log "github.com/Sirupsen/logrus"
sjson "github.com/bitly/go-simplejson"
)
const (
exTypeLua = "lua"
exTypeJSON = "json"
exTypeText = "text"
)
// Extractor .
type Extractor interface {
Extract(content string) (map[string]interface{}, error)
Close()
}
// NewExtractorConfig .
func NewExtractorConfig(configStr string) (ExtractorConfig, error) {
c := ExtractorConfig{}
j, err := sjson.NewJson([]byte(configStr))
name, err := j.Get("Name").String()
cJ := j.Get("Config")
if err != nil || name == "" {
return c, nil
}
jbyte, err := cJ.MarshalJSON()
if err != nil {
return c, err
}
switch strings.ToLower(name) {
case exTypeLua:
c.Config, err = NewLuaExtractorConfig(jbyte)
case exTypeJSON:
c.Config, err = NewJSONExtractorConfig(jbyte)
case exTypeText:
c.Config, err = NewTextExtractorConfig(jbyte)
default:
err = errors.New("extractor name error: " + c.Name)
}
c.Name = name
log.Infof("[ExtractorConfig] Init finish %#v, %#v", c, err)
return c, err
}
// NewExtractor .
func NewExtractor(c ExtractorConfig) (e Extractor, err error) {
switch strings.ToLower(c.Name) {
case exTypeLua:
e, err = NewLuaExtractor(c.Config)
case exTypeJSON:
e, err = NewJSONExtractor(c.Config)
case exTypeText:
e, err = NewTextExtractor(c.Config)
default:
err = errors.New("extractor name error: " + c.Name)
}
return e, err
}