in libbeat/common/event.go [144:255]
func (e *GenericEventConverter) normalizeValue(value interface{}, keys ...string) (interface{}, []error) {
if value == nil {
return nil, nil
}
// Normalize time values to a common.Time with UTC time zone.
switch v := value.(type) {
case time.Time:
value = Time(v.UTC())
case []time.Time:
times := make([]Time, 0, len(v))
for _, t := range v {
times = append(times, Time(t.UTC()))
}
value = times
case Time:
value = Time(time.Time(v).UTC())
case []Time:
times := make([]Time, 0, len(v))
for _, t := range v {
times = append(times, Time(time.Time(t).UTC()))
}
value = times
}
switch value.(type) {
case encoding.TextMarshaler:
if reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil() {
return nil, nil
}
text, err := value.(encoding.TextMarshaler).MarshalText()
if err != nil {
return nil, []error{fmt.Errorf("key=%v: error converting %T to string: %w", joinKeys(keys...), value, err)}
}
return string(text), nil
case string, []string:
case bool, []bool:
case int, int8, int16, int32, int64:
case []int, []int8, []int16, []int32, []int64:
case uint, uint8, uint16, uint32:
case uint64:
return value.(uint64) &^ (1 << 63), nil
case []uint, []uint8, []uint16, []uint32:
case []uint64:
arr := value.([]uint64)
mask := false
for _, v := range arr {
if v >= (1 << 63) {
mask = true
break
}
}
if !mask {
return value, nil
}
tmp := make([]uint64, len(arr))
for i, v := range arr {
tmp[i] = v &^ (1 << 63)
}
return tmp, nil
case float32, float64:
case []float32, []float64:
case complex64, complex128:
case []complex64, []complex128:
case Time, []Time:
case mapstr.M:
return e.normalizeMap(value.(mapstr.M), keys...)
case []mapstr.M:
return e.normalizeMapStrSlice(value.([]mapstr.M), keys...)
case map[string]interface{}:
return e.normalizeMap(value.(map[string]interface{}), keys...)
case []map[string]interface{}:
return e.normalizeMapStringSlice(value.([]map[string]interface{}), keys...)
default:
v := reflect.ValueOf(value)
switch v.Type().Kind() {
case reflect.Ptr:
// Dereference pointers.
return e.normalizeValue(followPointer(value), keys...)
case reflect.Bool:
return v.Bool(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v.Uint() &^ (1 << 63), nil
case reflect.Float32, reflect.Float64:
return v.Float(), nil
case reflect.Complex64, reflect.Complex128:
return v.Complex(), nil
case reflect.String:
return v.String(), nil
case reflect.Array, reflect.Slice:
return e.normalizeSlice(v, keys...)
case reflect.Map, reflect.Struct:
var m mapstr.M
err := marshalUnmarshal(value, &m)
if err != nil {
return m, []error{fmt.Errorf("key=%v: error converting %T to mapstr.M: %w", joinKeys(keys...), value, err)}
}
return m, nil
default:
// Drop Uintptr, UnsafePointer, Chan, Func, Interface, and any other
// types not specifically handled above.
return nil, []error{fmt.Errorf("key=%v: error unsupported type=%T value=%#v", joinKeys(keys...), value, value)}
}
}
return value, nil
}