-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecoder.go
67 lines (61 loc) · 1.82 KB
/
decoder.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
package rfc5424
import (
"io"
"reflect"
)
type Decoder struct {
Reader io.Reader
}
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{Reader: r}
}
func (d Decoder) Decode(ob interface{}) error {
m := Message{}
if _, err := m.ReadFrom(d.Reader); err != nil {
return err
}
return d.decode(&m, ob)
}
func (d Decoder) decode(m *Message, ob interface{}) error {
mt := reflect.TypeOf(ob)
mv := reflect.ValueOf(ob)
reflection := Reflect(mt)
if reflection.SeverityFieldIndex >= 0 {
severity := Emergency + (m.Priority & severityMask)
mv.Field(reflection.SeverityFieldIndex).Set(reflect.ValueOf(severity))
}
if reflection.FacilityFieldIndex >= 0 {
facility := Kernel + ((m.Priority & facilityMask) >> 3)
mv.Field(reflection.FacilityFieldIndex).Set(reflect.ValueOf(facility))
}
if reflection.TimestampFieldIndex >= 0 {
mv.Field(reflection.TimestampFieldIndex).Set(reflect.ValueOf(m.Timestamp))
}
if reflection.HostnameFieldIndex >= 0 {
mv.Field(reflection.HostnameFieldIndex).Set(reflect.ValueOf(m.Hostname))
}
if reflection.AppNameFieldIndex >= 0 {
mv.Field(reflection.AppNameFieldIndex).Set(reflect.ValueOf(m.AppName))
}
if reflection.ProcessIDFieldIndex >= 0 {
mv.Field(reflection.ProcessIDFieldIndex).Set(reflect.ValueOf(m.ProcessID))
}
if reflection.MessageIDFieldIndex >= 0 {
mv.Field(reflection.MessageIDFieldIndex).Set(reflect.ValueOf(m.MessageID))
}
if reflection.MessageFieldIndex >= 0 {
mv.Field(reflection.MessageIDFieldIndex).Set(reflect.ValueOf(m.Message))
}
for _, sd := range m.StructuredData {
for _, param := range sd.Parameters {
fieldReflection := reflection.GetStructuredDataFieldReflection(
sd.ID, param.Name)
if fieldReflection != nil {
continue
}
// XXX: handle other types than strings
mv.Field(fieldReflection.FieldIndex).SetString(param.Value)
}
}
return nil
}