Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested fields for all types in common.Schema #7583

Merged
merged 1 commit into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions libbeat/common/schema/mapstriface/mapstriface.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ type ConvMap struct {

// Map drills down in the data dictionary by using the key
func (convMap ConvMap) Map(key string, event common.MapStr, data map[string]interface{}) multierror.Errors {
d, found := data[convMap.Key]
if !found {
d, err := common.MapStr(data).GetValue(convMap.Key)
if err != nil {
err := schema.NewKeyNotFoundError(convMap.Key)
err.Optional = convMap.Optional
err.Required = convMap.Required
Expand Down Expand Up @@ -130,8 +130,8 @@ func Dict(key string, s schema.Schema, opts ...DictSchemaOption) ConvMap {
}

func toStrFromNum(key string, data map[string]interface{}) (interface{}, error) {
emptyIface, exists := data[key]
if !exists {
emptyIface, err := common.MapStr(data).GetValue(key)
if err != nil {
return "", schema.NewKeyNotFoundError(key)
}
switch emptyIface.(type) {
Expand Down Expand Up @@ -184,8 +184,8 @@ func Ifc(key string, opts ...schema.SchemaOption) schema.Conv {
}

func toBool(key string, data map[string]interface{}) (interface{}, error) {
emptyIface, exists := data[key]
if !exists {
emptyIface, err := common.MapStr(data).GetValue(key)
if err != nil {
return false, schema.NewKeyNotFoundError(key)
}
boolean, ok := emptyIface.(bool)
Expand All @@ -202,8 +202,8 @@ func Bool(key string, opts ...schema.SchemaOption) schema.Conv {
}

func toInteger(key string, data map[string]interface{}) (interface{}, error) {
emptyIface, exists := data[key]
if !exists {
emptyIface, err := common.MapStr(data).GetValue(key)
if err != nil {
return 0, schema.NewKeyNotFoundError(key)
}
switch emptyIface.(type) {
Expand Down Expand Up @@ -238,8 +238,8 @@ func Float(key string, opts ...schema.SchemaOption) schema.Conv {
}

func toFloat(key string, data map[string]interface{}) (interface{}, error) {
emptyIface, exists := data[key]
if !exists {
emptyIface, err := common.MapStr(data).GetValue(key)
if err != nil {
return 0.0, schema.NewKeyNotFoundError(key)
}
switch emptyIface.(type) {
Expand Down Expand Up @@ -274,8 +274,8 @@ func Int(key string, opts ...schema.SchemaOption) schema.Conv {
}

func toTime(key string, data map[string]interface{}) (interface{}, error) {
emptyIface, exists := data[key]
if !exists {
emptyIface, err := common.MapStr(data).GetValue(key)
if err != nil {
return common.Time(time.Unix(0, 0)), schema.NewKeyNotFoundError(key)
}

Expand Down
79 changes: 79 additions & 0 deletions libbeat/common/schema/mapstriface/mapstriface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,82 @@ func TestFullFieldPathInErrors(t *testing.T) {
}
}
}

func TestNestedFieldPaths(t *testing.T) {
cases := []struct {
Description string
Input map[string]interface{}
Schema s.Schema
Expected common.MapStr
ExpectError bool
}{
{
"nested values",
map[string]interface{}{
"root": map[string]interface{}{
"foo": "bar",
"float": 4.5,
"int": 4,
"bool": true,
},
},
s.Schema{
"foo": Str("root.foo"),
"float": Float("root.float"),
"int": Int("root.int"),
"bool": Bool("root.bool"),
},
common.MapStr{
"foo": "bar",
"float": float64(4.5),
"int": int64(4),
"bool": true,
},
false,
},
{
"not really nested values, path contains dots",
map[string]interface{}{
"root.foo": "bar",
},
s.Schema{
"foo": Str("root.foo"),
},
common.MapStr{
"foo": "bar",
},
false,
},
{
"nested dict",
map[string]interface{}{
"root": map[string]interface{}{
"dict": map[string]interface{}{
"foo": "bar",
},
},
},
s.Schema{
"dict": Dict("root.dict", s.Schema{
"foo": Str("foo"),
}),
},
common.MapStr{
"dict": common.MapStr{
"foo": "bar",
},
},
false,
},
}

for _, c := range cases {
event, err := c.Schema.Apply(c.Input)
if c.ExpectError {
assert.Error(t, err, c.Description)
} else {
assert.NoError(t, err, c.Description)
assert.Equal(t, c.Expected, event, c.Description)
}
}
}