-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
55 lines (49 loc) · 1.1 KB
/
template.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
package ipfix
import (
"encoding/binary"
)
type template struct {
identifier int16
elements []InformationElement
}
func (t template) id() int16 {
return templateSetID
}
func (t template) length() (ret int) {
ret = 4
for _, element := range t.elements {
ret += element.templateSize()
}
return
}
func (t template) serializeTo(buffer scratchBuffer) error {
b, err := buffer.append(4)
if err != nil {
return err
}
binary.BigEndian.PutUint16(b[2:], uint16(len(t.elements)))
binary.BigEndian.PutUint16(b[0:], uint16(t.identifier))
for _, element := range t.elements {
_, err := element.serializeTo(buffer)
if err != nil {
return err
}
}
return nil
}
func (t template) assignDataRecord(record *recordBuffer, values ...interface{}) error {
if len(values) != len(t.elements) {
return TemplateMismatchError{len(values), len(t.elements)}
}
good := record.length()
record.template = t.identifier
values = values[:len(t.elements)]
for i, element := range t.elements {
err := element.serializeDataTo(record, values[i])
if err != nil {
record.reset(good)
return err
}
}
return nil
}