forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
values.go
65 lines (53 loc) · 1.48 KB
/
values.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
package outputs
import "github.com/elastic/beats/libbeat/common"
// Values is a recursive key/value store for use by output plugins and publisher
// pipeline to share context-dependent values.
type Values struct {
parent *Values
key, value interface{}
}
// ValueWith creates new key/value store shadowing potentially old keys.
func ValueWith(parent *Values, key interface{}, value interface{}) *Values {
return &Values{
parent: parent,
key: key,
value: value,
}
}
// Append creates new key/value store from existing store by adding a new
// key/value pair potentially shadowing an already present key/value pair.
func (v *Values) Append(key, value interface{}) *Values {
if v.IsEmpty() {
return ValueWith(nil, key, value)
}
return ValueWith(v, key, value)
}
// IsEmpty returns true if key/value store is empty.
func (v *Values) IsEmpty() bool {
return v == nil || (v.parent == nil && v.key == nil && v.value == nil)
}
// Get retrieves a value for the given key.
func (v *Values) Get(key interface{}) (interface{}, bool) {
if v == nil {
return nil, false
}
if v.key == key {
return v.value, true
}
return v.parent.Get(key)
}
// standard outputs values utilities
type keyMetadata int
func ValuesWithMetadata(parent *Values, meta common.MapStr) *Values {
return parent.Append(keyMetadata(0), meta)
}
func GetMetadata(v *Values) common.MapStr {
value, ok := v.Get(keyMetadata(0))
if !ok {
return nil
}
if m, ok := value.(common.MapStr); ok {
return m
}
return nil
}