forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapstriface.go
223 lines (195 loc) · 6.16 KB
/
mapstriface.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
/*
Package mapstriface contains utilities for transforming map[string]interface{} objects
into metricbeat events. For example, given this input object:
input := map[string]interface{}{
"testString": "hello",
"testInt": 42,
"testIntFromFloat": 42.0,
"testIntFromInt64": int64(42),
"testBool": true,
"testObj": map[string]interface{}{
"testObjString": "hello, object",
},
"testNonNestedObj": "hello from top level",
}
And the requirement to transform it into this one:
common.MapStr{
"test_string": "hello",
"test_int": int64(42),
"test_int_from_float": int64(42),
"test_int_from_int64": int64(42),
"test_bool": true,
"test_time": common.Time(ts),
"test_obj_1": common.MapStr{
"test": "hello from top level",
},
"test_obj_2": common.MapStr{
"test": "hello, object",
},
}
It can be done with the following code:
schema := s.Schema{
"test_string": Str("testString"),
"test_int": Int("testInt"),
"test_int_from_float": Int("testIntFromFloat"),
"test_int_from_int64": Int("testIntFromInt64"),
"test_bool": Bool("testBool"),
"test_time": Time("testTime"),
"test_obj_1": s.Object{
"test": Str("testNonNestedObj"),
},
"test_obj_2": Dict("testObj", s.Schema{
"test": Str("testObjString"),
}),
}
output := schema.Apply(input)
Note that this allows for converting, renaming, and restructuring the data.
*/
package mapstriface
import (
"fmt"
"time"
"encoding/json"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/schema"
)
type ConvMap struct {
Key string // The key in the data map
Schema schema.Schema // The schema describing how to convert the sub-map
Optional bool
}
// Map drills down in the data dictionary by using the key
func (convMap ConvMap) Map(key string, event common.MapStr, data map[string]interface{}) {
subData, ok := data[convMap.Key].(map[string]interface{})
if !ok {
if !convMap.Optional {
logp.Err("Error accessing sub-dictionary `%s`", convMap.Key)
}
return
}
subEvent := common.MapStr{}
convMap.Schema.ApplyTo(subEvent, subData)
event[key] = subEvent
}
func (convMap ConvMap) HasKey(key string) bool {
if convMap.Key == key {
return true
}
return convMap.Schema.HasKey(key)
}
func Dict(key string, s schema.Schema, opts ...DictSchemaOption) ConvMap {
return dictSetOptions(ConvMap{Key: key, Schema: s}, opts)
}
func toStrFromNum(key string, data map[string]interface{}) (interface{}, error) {
emptyIface, exists := data[key]
if !exists {
return false, fmt.Errorf("Key %s not found", key)
}
switch emptyIface.(type) {
case int, int32, int64, uint, uint32, uint64, float32, float64:
return fmt.Sprintf("%v", emptyIface), nil
case json.Number:
return string(emptyIface.(json.Number)), nil
default:
return "", fmt.Errorf("Expected number, found %T", emptyIface)
}
}
// StrFromNum creates a schema.Conv object that transforms numbers to strings.
func StrFromNum(key string, opts ...schema.SchemaOption) schema.Conv {
return schema.SetOptions(schema.Conv{Key: key, Func: toStrFromNum}, opts)
}
func toStr(key string, data map[string]interface{}) (interface{}, error) {
emptyIface, err := common.MapStr(data).GetValue(key)
if err != nil {
fmt.Println(err)
return "", fmt.Errorf("Key %s not found", key)
}
str, ok := emptyIface.(string)
if !ok {
return "", fmt.Errorf("Expected string, found %T", emptyIface)
}
return str, nil
}
// Str creates a schema.Conv object for converting strings.
func Str(key string, opts ...schema.SchemaOption) schema.Conv {
return schema.SetOptions(schema.Conv{Key: key, Func: toStr}, opts)
}
func toBool(key string, data map[string]interface{}) (interface{}, error) {
emptyIface, exists := data[key]
if !exists {
return false, fmt.Errorf("Key %s not found", key)
}
boolean, ok := emptyIface.(bool)
if !ok {
return false, fmt.Errorf("Expected bool, found %T", emptyIface)
}
return boolean, nil
}
// Bool creates a Conv object for converting booleans.
func Bool(key string, opts ...schema.SchemaOption) schema.Conv {
return schema.SetOptions(schema.Conv{Key: key, Func: toBool}, opts)
}
func toInteger(key string, data map[string]interface{}) (interface{}, error) {
emptyIface, exists := data[key]
if !exists {
return 0, fmt.Errorf("Key %s not found", key)
}
switch emptyIface.(type) {
case int64:
return emptyIface.(int64), nil
case int:
return int64(emptyIface.(int)), nil
case float64:
return int64(emptyIface.(float64)), nil
case json.Number:
num := emptyIface.(json.Number)
i64, err := num.Int64()
if err == nil {
return i64, nil
}
f64, err := num.Float64()
if err == nil {
return int64(f64), nil
}
return 0, fmt.Errorf("Expected integer, found json.Number (%v) that cannot be converted", num)
default:
return 0, fmt.Errorf("Expected integer, found %T", emptyIface)
}
}
// Int creates a Conv object for converting integers. Acceptable input
// types are int64, int, and float64.
func Int(key string, opts ...schema.SchemaOption) schema.Conv {
return schema.SetOptions(schema.Conv{Key: key, Func: toInteger}, opts)
}
func toTime(key string, data map[string]interface{}) (interface{}, error) {
emptyIface, exists := data[key]
if !exists {
return common.Time(time.Unix(0, 0)), fmt.Errorf("Key %s not found", key)
}
ts, ok := emptyIface.(time.Time)
if !ok {
return common.Time(time.Unix(0, 0)), fmt.Errorf("Expected date, found %T", emptyIface)
}
return common.Time(ts), nil
}
// Time creates a Conv object for converting Time objects.
func Time(key string, opts ...schema.SchemaOption) schema.Conv {
return schema.SetOptions(schema.Conv{Key: key, Func: toTime}, opts)
}
// SchemaOption is for adding optional parameters to the conversion
// functions
type DictSchemaOption func(c ConvMap) ConvMap
// The optional flag suppresses the error message in case the key
// doesn't exist or results in an error.
func DictOptional(c ConvMap) ConvMap {
c.Optional = true
return c
}
// setOptions adds the optional flags to the Conv object
func dictSetOptions(c ConvMap, opts []DictSchemaOption) ConvMap {
for _, opt := range opts {
c = opt(c)
}
return c
}