Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
70
plugins/parsers/json/json_flattener.go
Normal file
70
plugins/parsers/json/json_flattener.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type JSONFlattener struct {
|
||||
Fields map[string]interface{}
|
||||
}
|
||||
|
||||
// FlattenJSON flattens nested maps/interfaces into a fields map (ignoring bools and string)
|
||||
func (f *JSONFlattener) FlattenJSON(
|
||||
fieldname string,
|
||||
v interface{}) error {
|
||||
if f.Fields == nil {
|
||||
f.Fields = make(map[string]interface{})
|
||||
}
|
||||
|
||||
return f.FullFlattenJSON(fieldname, v, false, false)
|
||||
}
|
||||
|
||||
// FullFlattenJSON flattens nested maps/interfaces into a fields map (including bools and string)
|
||||
func (f *JSONFlattener) FullFlattenJSON(fieldName string, v interface{}, convertString, convertBool bool) error {
|
||||
if f.Fields == nil {
|
||||
f.Fields = make(map[string]interface{})
|
||||
}
|
||||
|
||||
switch t := v.(type) {
|
||||
case map[string]interface{}:
|
||||
for fieldKey, fieldVal := range t {
|
||||
if fieldName != "" {
|
||||
fieldKey = fieldName + "_" + fieldKey
|
||||
}
|
||||
|
||||
err := f.FullFlattenJSON(fieldKey, fieldVal, convertString, convertBool)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case []interface{}:
|
||||
for i, fieldVal := range t {
|
||||
fieldKey := strconv.Itoa(i)
|
||||
if fieldName != "" {
|
||||
fieldKey = fieldName + "_" + fieldKey
|
||||
}
|
||||
err := f.FullFlattenJSON(fieldKey, fieldVal, convertString, convertBool)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case float64:
|
||||
f.Fields[fieldName] = t
|
||||
case string:
|
||||
if !convertString {
|
||||
return nil
|
||||
}
|
||||
f.Fields[fieldName] = v.(string)
|
||||
case bool:
|
||||
if !convertBool {
|
||||
return nil
|
||||
}
|
||||
f.Fields[fieldName] = v.(bool)
|
||||
case nil:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("json flattener: got unexpected type %T with value %v (%s)", t, t, fieldName)
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue