forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 4
/
event.go
86 lines (75 loc) · 1.82 KB
/
event.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
package common
import (
"encoding/json"
"reflect"
"time"
"github.com/elastic/beats/libbeat/logp"
)
func MarshallUnmarshall(v interface{}) (MapStr, error) {
// decode and encode JSON
marshaled, err := json.Marshal(v)
if err != nil {
logp.Warn("marshal err: %v", err)
return nil, err
}
var v1 MapStr
err = json.Unmarshal(marshaled, &v1)
if err != nil {
logp.Warn("unmarshal err: %v")
return nil, err
}
return v1, nil
}
func ConvertToGenericEvent(v MapStr) MapStr {
for key, value := range v {
switch value.(type) {
case Time, *Time:
continue
case time.Location, *time.Location:
continue
case MapStr:
v[key] = ConvertToGenericEvent(value.(MapStr))
continue
case *MapStr:
v[key] = ConvertToGenericEvent(*value.(*MapStr))
continue
default:
typ := reflect.TypeOf(value)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
switch typ.Kind() {
case reflect.Bool:
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
case reflect.Uintptr:
case reflect.Float32, reflect.Float64:
case reflect.Complex64, reflect.Complex128:
case reflect.String:
case reflect.UnsafePointer:
case reflect.Array, reflect.Slice:
//case reflect.Chan:
//case reflect.Func:
//case reflect.Interface:
case reflect.Map:
anothermap, err := MarshallUnmarshall(value)
if err != nil {
logp.Warn("fail to marschall & unmarshall map %v", key)
continue
}
v[key] = anothermap
case reflect.Struct:
anothermap, err := MarshallUnmarshall(value)
if err != nil {
logp.Warn("fail to marschall & unmarshall struct %v", key)
continue
}
v[key] = anothermap
default:
logp.Warn("unknown type %v", typ)
continue
}
}
}
return v
}