-
Notifications
You must be signed in to change notification settings - Fork 2
/
metadata.go
57 lines (47 loc) · 1.13 KB
/
metadata.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
package suricataparser
import (
"errors"
"fmt"
"strings"
)
// Metadata stores parsed meta values - https://suricata.readthedocs.io/en/latest/rules/meta.html#metadata
type Metadata struct {
items []string
}
func (m *Metadata) String() string {
return strings.Join(m.items, ", ")
}
func (m *Metadata) AddMeta(key, value string) {
m.AddItem(fmt.Sprintf("%s %s", key, value))
}
func (m *Metadata) AddItem(item string) {
m.items = append(m.items, item)
}
func (m *Metadata) PopMeta(key string) {
var newItems []string
for _, meta := range m.items {
if !strings.HasPrefix(meta, key) {
newItems = append(newItems, meta)
}
}
m.items = newItems
}
func (m *Metadata) Merge(metadata Metadata) {
for _, item := range metadata.Items() {
m.AddItem(item)
}
}
func (m *Metadata) Items() []string {
return m.items
}
// NewMetadata returns empty Metadata
func NewMetadata() *Metadata {
return &Metadata{[]string{}}
}
// ParseMetadata from raw string
func ParseMetadata(metadata string) (*Metadata, error) {
if metadata == "" {
return nil, errors.New("metadata never empty")
}
return &Metadata{items: strings.Split(metadata, ", ")}, nil
}