forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mapstr.go
269 lines (227 loc) · 5.81 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package common
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
)
const (
EventMetadataKey = "_event_metadata"
FieldsKey = "fields"
TagsKey = "tags"
)
var ErrorFieldsIsNotMapStr = errors.New("the value stored in fields is not a MapStr")
var ErrorTagsIsNotStringArray = errors.New("the value stored in tags is not a []string")
// Commonly used map of things, used in JSON creation and the like.
type MapStr map[string]interface{}
// EventMetadata contains fields and tags that can be added to an event via
// configuration.
type EventMetadata struct {
Fields MapStr
FieldsUnderRoot bool `config:"fields_under_root"`
Tags []string
}
// 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
}
}
func (m MapStr) Delete(key string) error {
keyParts := strings.Split(key, ".")
keysLen := len(keyParts)
mapp := m
for i := 0; i < keysLen-1; i++ {
keyPart := keyParts[i]
if _, ok := mapp[keyPart]; ok {
mapp, ok = mapp[keyPart].(MapStr)
if !ok {
return fmt.Errorf("unexpected type of %s key", keyPart)
}
} else {
return fmt.Errorf("unknown key %s", keyPart)
}
}
delete(mapp, keyParts[keysLen-1])
return nil
}
func (m MapStr) CopyFieldsTo(to MapStr, key string) error {
keyParts := strings.Split(key, ".")
keysLen := len(keyParts)
toPointer := to
fromPointer := m
for i := 0; i < keysLen-1; i++ {
keyPart := keyParts[i]
var success bool
if _, ok := fromPointer[keyPart]; ok {
if _, already := toPointer[keyPart]; !already {
toPointer[keyPart] = MapStr{}
}
fromPointer, success = fromPointer[keyPart].(MapStr)
if !success {
return fmt.Errorf("Unexpected type of %s key", keyPart)
}
toPointer, success = toPointer[keyPart].(MapStr)
if !success {
return fmt.Errorf("Unexpected type of %s key", keyPart)
}
} else {
return nil
}
}
if _, ok := fromPointer[keyParts[keysLen-1]]; ok {
toPointer[keyParts[keysLen-1]] = fromPointer[keyParts[keysLen-1]]
} else {
return nil
}
return nil
}
func (m MapStr) Clone() MapStr {
result := MapStr{}
for k, v := range m {
mapstr, ok := v.(MapStr)
if ok {
v = mapstr.Clone()
}
result[k] = v
}
return result
}
func (m MapStr) HasKey(key string) (bool, error) {
keyParts := strings.Split(key, ".")
keyPartsLen := len(keyParts)
mapp := m
for i := 0; i < keyPartsLen; i++ {
keyPart := keyParts[i]
if _, ok := mapp[keyPart]; ok {
if i < keyPartsLen-1 {
mapp, ok = mapp[keyPart].(MapStr)
if !ok {
return false, fmt.Errorf("Unknown type of %s key", keyPart)
}
}
} else {
return false, nil
}
}
return true, nil
}
func (m MapStr) StringToPrint() string {
json, err := json.MarshalIndent(m, "", " ")
if err != nil {
return fmt.Sprintf("Not valid json: %v", err)
}
return string(json)
}
// 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)
}
// EnsureCountField sets the 'count' field to 1 if count does not already exist.
func (m MapStr) EnsureCountField() error {
_, exists := m["count"]
if !exists {
m["count"] = 1
}
return nil
}
// String returns the MapStr as a JSON string.
func (m MapStr) String() string {
bytes, err := json.Marshal(m)
if err != nil {
return fmt.Sprintf("Not valid json: %v", err)
}
return string(bytes)
}
// MergeFields merges the top-level keys and values in each source hash (it does
// not perform a deep merge). If the same key exists in both, the value in
// fields takes precedence. If underRoot is true then the contents of the fields
// MapStr is merged with the value of the 'fields' key in ms.
//
// An error is returned if underRoot is true and the value of ms.fields is not a
// MapStr.
func MergeFields(ms, fields MapStr, underRoot bool) error {
if ms == nil || fields == nil {
return nil
}
fieldsMS := ms
if !underRoot {
f, ok := ms[FieldsKey]
if !ok {
fieldsMS = make(MapStr, len(fields))
ms[FieldsKey] = fieldsMS
} else {
// Use existing 'fields' value.
fieldsMS, ok = f.(MapStr)
if !ok {
return ErrorFieldsIsNotMapStr
}
}
}
// Add fields and override.
for k, v := range fields {
fieldsMS[k] = v
}
return nil
}
// AddTag appends a tag to the tags field of ms. If the tags field does not
// exist then it will be created. If the tags field exists and is not a []string
// then an error will be returned. It does not deduplicate the list of tags.
func AddTags(ms MapStr, tags []string) error {
if ms == nil || len(tags) == 0 {
return nil
}
tagsIfc, ok := ms[TagsKey]
if !ok {
ms[TagsKey] = tags
return nil
}
existingTags, ok := tagsIfc.([]string)
if !ok {
return ErrorTagsIsNotStringArray
}
ms[TagsKey] = append(existingTags, tags...)
return nil
}