forked from ahjdzx/libbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapstr.go
87 lines (74 loc) · 1.78 KB
/
mapstr.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
package common
import (
"encoding/json"
"fmt"
"time"
)
// Commonly used map of things, used in JSON creation and the like.
type MapStr map[string]interface{}
// Eventer defines a type its ability to fill a MapStr.
type Eventer interface {
// Add fields to MapStr.
Event(event MapStr) error
}
// MapStrUnion creates a new MapStr containing the union of the
// key-value pairs of the two maps. If the same key is present in
// both, the key-value pairs from dict2 overwrite the ones from dict1.
func MapStrUnion(dict1 MapStr, dict2 MapStr) MapStr {
dict := MapStr{}
for k, v := range dict1 {
dict[k] = v
}
for k, v := range dict2 {
dict[k] = v
}
return dict
}
// Update copies all the key-value pairs from the
// d map overwriting any existing keys.
func (m MapStr) Update(d MapStr) {
for k, v := range d {
m[k] = v
}
}
// Checks if a timestamp field exists and if it doesn't it adds
// one by using the injected now() function as a time source.
func (m MapStr) EnsureTimestampField(now func() time.Time) error {
ts, exists := m["@timestamp"]
if !exists {
m["@timestamp"] = Time(now())
return nil
}
_, is_common_time := ts.(Time)
if is_common_time {
// already perfect
return nil
}
tstime, is_time := ts.(time.Time)
if is_time {
m["@timestamp"] = Time(tstime)
return nil
}
tsstr, is_string := ts.(string)
if is_string {
var err error
m["@timestamp"], err = ParseTime(tsstr)
return err
}
return fmt.Errorf("Don't know how to convert %v to a Time value", ts)
}
func (m MapStr) EnsureCountField() error {
_, exists := m["count"]
if !exists {
m["count"] = 1
}
return nil
}
// Prints the dict as a json
func (m MapStr) String() string {
bytes, err := json.Marshal(m)
if err != nil {
return fmt.Sprintf("Not valid json: %v", err)
}
return string(bytes)
}