-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprotocol_util.go
141 lines (123 loc) · 2.85 KB
/
protocol_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
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
package logpeck
import (
"encoding/json"
"errors"
sjson "github.com/bitly/go-simplejson"
)
// GetString from Json struct
func GetString(j *sjson.Json, key string, required bool) (string, error) {
valJ := j.Get(key)
if valJ.Interface() == nil {
if required {
return "", errors.New("Parse error: need field " + key)
}
return "", nil
}
return valJ.String()
}
// GetStringArray .
func GetStringArray(j *sjson.Json, key string) ([]string, error) {
valJ := j.Get(key)
if valJ.Interface() == nil {
return []string{""}, errors.New("Parse error: need field " + key)
}
return valJ.StringArray()
}
// GetMarshalString .
func GetMarshalString(j *sjson.Json, name string) (string, bool) {
cJ := j.Get(name)
if cJ.Interface() == nil {
return "", false
}
jbyte, err := cJ.MarshalJSON()
if err != nil {
return "", false
}
return string(jbyte), true
}
// Unmarshal .
func (p *PeckTaskConfig) Unmarshal(jsonStr []byte) (e error) {
j, e := sjson.NewJson(jsonStr)
if e != nil {
return e
}
// Parse "Name", required
p.Name, e = GetString(j, "Name", true)
if e != nil {
return e
}
// Parse "LogPath", optional
p.LogPath, e = GetString(j, "LogPath", false)
if e != nil {
return e
}
// Parse "ExtractorConfig", optional
eConfStr, ok := GetMarshalString(j, "Extractor")
if ok {
p.Extractor, e = NewExtractorConfig(eConfStr)
if e != nil {
return e
}
}
// Parse "SenderConfig", optional
p.Sender, e = GetSenderConfig(j)
if e != nil {
return e
}
//Parse "aggregatorConfig", optional
aggregatorConfig := j.Get("Aggregator")
jbyte, e := aggregatorConfig.MarshalJSON()
if e != nil {
return e
}
e = json.Unmarshal(jbyte, &p.Aggregator)
if e != nil {
return e
}
// Parse "FilterExpr", optional
p.Keywords, e = GetString(j, "Keywords", false)
if e != nil {
return e
}
testJ := j.Get("Test")
if e != nil {
p.Test.TestNum = 1
p.Test.Timeout = 1
}
// Parse "TestNum", optional
val, e := testJ.Get("TestNum").Int()
if e != nil {
p.Test.TestNum = 1
}
p.Test.TestNum = val
// Parse "Time", optional
time, e := testJ.Get("Timeout").Int()
if e != nil {
p.Test.Timeout = 1
}
p.Test.Timeout = time
/*
// Parse "Fields", optional
if fields, e := j.Get("Fields").Array(); e == nil {
fmt.Println(len(fields))
for _, field := range fields {
var f PeckField
if name, ok := field.(map[string]interface{})["Name"]; ok {
if f.Name, ok = name.(string); !ok {
return errors.New("Fields format error: Name must be a string")
}
} else {
return errors.New("Fields error: need Name")
}
if value, ok := field.(map[string]interface{})["Value"]; ok {
if f.Value, ok = value.(string); !ok {
return errors.New("Fields format error: Name must be a string")
}
}else {
return errors.New("Fields error: need Value")
}
p.Fields = append(p.Fields, f)
}
}*/
return nil
}