-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
66 lines (61 loc) · 1.2 KB
/
entry.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
package splunk
import (
"fmt"
"reflect"
)
type Entry map[string]interface{}
func (log Entry) Add(name string, value interface{}) Entry {
log[name] = value
return log
}
func NewEntry(p ...interface{}) Entry {
if len(p) == 1 {
c, ok := p[0].([]interface{})
if ok {
p = c
}
}
normalized := Entry{}
for _, item := range p {
if item == nil {
continue
}
itemType := reflect.TypeOf(item)
v := reflect.ValueOf(item)
inner := Entry{}
switch itemType.Kind() {
case reflect.Map:
for _, key := range v.MapKeys() {
inner[fmt.Sprint(key.Interface())] = v.MapIndex(key).Interface()
}
case reflect.Ptr, reflect.Interface:
if !v.IsNil() {
inner = NewEntry(v.Elem().Interface())
}
default:
inner[itemType.Name()] = item
}
normalized = Merge(normalized, inner)
}
return normalized
}
func Merge(np Entry, other Entry) Entry {
collisions := map[string]int{}
r := Entry{}
for key, value := range np {
r[key] = value
}
for key, value := range other {
if _, ok := r[key]; ok {
var index int
if index, ok = collisions[key]; !ok {
index = 0
}
index += 1
collisions[key] = index
key = fmt.Sprintf("%s%d", key, index)
}
r[key] = value
}
return r
}