-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackunpack.go
111 lines (87 loc) · 2.28 KB
/
packunpack.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
package syslogsidecar
import (
"fmt"
"github.com/g41797/sputnik"
)
// Creates map[string]string from syslog parts are stored within message
func UnpackToMap(msg sputnik.Msg) (map[string]string, error) {
uh := NewUnpackHelper()
err := Unpack(msg, uh.Put)
if err != nil {
return nil, err
}
return uh.LogParts, nil
}
// For every part of syslog message(stored within msg) runs supplied callback
// See README for the list of partnames
func Unpack(msg sputnik.Msg, f func(partname string, val string) error) error {
if msg == nil {
return fmt.Errorf("nil msg")
}
slm, exists := msg[syslogmessage]
if !exists {
return fmt.Errorf("empty msg")
}
syslogmsgparts, ok := slm.(*syslogmsgparts)
if !ok {
return fmt.Errorf("wrong msg")
}
return syslogmsgparts.Unpack(f)
}
func Pack(msg sputnik.Msg, parts map[string]string) error {
if msg == nil {
return fmt.Errorf("nil msg")
}
count := len(parts)
if count == 0 {
return fmt.Errorf("empty parts")
}
syslogmsgparts, exists := msg[syslogmessage].(*syslogmsgparts)
if !exists {
syslogmsgparts = newsyslogmsgparts()
msg[syslogmessage] = syslogmsgparts
}
var partsDescr []partType
switch count {
case badMessageParts:
partsDescr = formerMessage[:]
case rfc5424Parts:
partsDescr = rfc5424parts[:]
case rfc3164Parts:
partsDescr = rfc3164parts[:]
default:
return fmt.Errorf("wrong parts")
}
return pack(msg, parts, syslogmsgparts, partsDescr)
}
func pack(msg sputnik.Msg, parts map[string]string, syslogmsgparts *syslogmsgparts, expected []partType) error {
count := len(expected)
syslogmsgparts.setRuneAt(0, rune(count))
syslogmsgparts.skip(count + 1)
for i, part := range expected {
val, exists := parts[part.name]
if !exists {
return fmt.Errorf("%s does not exist", part.name)
}
syslogmsgparts.setRuneAt(i+1, rune(syslogmsgparts.appendText(val)))
}
return nil
}
type UnpackHelper struct {
LogParts map[string]string
}
func NewUnpackHelper() UnpackHelper {
var result UnpackHelper
result.LogParts = make(map[string]string)
return result
}
func (hlp *UnpackHelper) Put(name, value string) error {
if hlp.LogParts == nil {
return fmt.Errorf("nil LogParts")
}
if _, present := hlp.LogParts[name]; present {
return fmt.Errorf("%s already exists", name)
}
hlp.LogParts[name] = value
return nil
}