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

Move Field(s) to common. #5271

Merged
merged 1 commit into from
Sep 29, 2017
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
116 changes: 116 additions & 0 deletions libbeat/common/field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package common

import (
"fmt"
"strings"

"github.com/elastic/go-ucfg/yaml"
)

//This reflects allowed attributes for field definitions in the fields.yml.
//No logic is put into this data structure.
//The purpose is to enable using different kinds of transformation, on top of the same data structure.
//Current transformation:
// -ElasticSearch Template
//Future:
// -Kibana Index Pattern

type Field struct {
Name string `config:"name"`
Type string `config:"type"`
Description string `config:"description"`
Format string `config:"format"`
ScalingFactor int `config:"scaling_factor"`
Fields Fields `config:"fields"`
MultiFields Fields `config:"multi_fields"`
ObjectType string `config:"object_type"`
Enabled *bool `config:"enabled"`
Analyzer string `config:"analyzer"`
SearchAnalyzer string `config:"search_analyzer"`
Norms bool `config:"norms"`
Dynamic DynamicType `config:"dynamic"`
Index *bool `config:"index"`
DocValues *bool `config:"doc_values"`

Path string
}

type Fields []Field

func LoadFieldsYaml(path string) (Fields, error) {
keys := []Field{}

cfg, err := yaml.NewConfigWithFile(path)
if err != nil {
return nil, err
}
cfg.Unpack(&keys)

fields := Fields{}

for _, key := range keys {
fields = append(fields, key.Fields...)
}
return fields, nil
}

// HasKey checks if inside fields the given key exists
// The key can be in the form of a.b.c and it will check if the nested field exist
// In case the key is `a` and there is a value `a.b` false is return as it only
// returns true if it's a leave node
func (f Fields) HasKey(key string) bool {
keys := strings.Split(key, ".")
return f.hasKey(keys)
}

// Recursively generates the correct key based on the dots
// The mapping requires "properties" between each layer. This is added here.
func GenerateKey(key string) string {
if strings.Contains(key, ".") {
keys := strings.SplitN(key, ".", 2)
key = keys[0] + ".properties." + GenerateKey(keys[1])
}
return key
}

type DynamicType struct{ Value interface{} }

func (d *DynamicType) Unpack(s string) error {
switch s {
case "true":
d.Value = true
case "false":
d.Value = false
case "strict":
d.Value = s
default:
return fmt.Errorf("'%v' is invalid dynamic setting", s)
}
return nil
}

func (f Fields) hasKey(keys []string) bool {
// Nothing to compare anymore
if len(keys) == 0 {
return false
}

key := keys[0]
keys = keys[1:]

for _, field := range f {
if field.Name == key {

if len(field.Fields) > 0 {
return field.Fields.hasKey(keys)
}
// Last entry in the tree but still more keys
if len(keys) > 0 {
return false
}

return true
}
}
return false
}
117 changes: 117 additions & 0 deletions libbeat/common/field_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package common

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/elastic/go-ucfg/yaml"
)

func TestFieldsHasKey(t *testing.T) {
tests := []struct {
key string
fields Fields
result bool
}{
{
key: "test.find",
fields: Fields{},
result: false,
},
{
key: "test.find",
fields: Fields{
Field{Name: "test"},
Field{Name: "find"},
},
result: false,
},
{
key: "test.find",
fields: Fields{
Field{
Name: "test", Fields: Fields{
Field{
Name: "find",
},
},
},
},
result: true,
},
{
key: "test",
fields: Fields{
Field{
Name: "test", Fields: Fields{
Field{
Name: "find",
},
},
},
},
result: false,
},
}

for _, test := range tests {
assert.Equal(t, test.result, test.fields.HasKey(test.key))
}
}

func TestDynamicYaml(t *testing.T) {
tests := []struct {
input []byte
output Field
error bool
}{
{
input: []byte(`
name: test
dynamic: true`),
output: Field{
Name: "test",
Dynamic: DynamicType{true},
},
},
{
input: []byte(`
name: test
dynamic: "true"`),
output: Field{
Name: "test",
Dynamic: DynamicType{true},
},
},
{
input: []byte(`
name: test
dynamic: "blue"`),
error: true,
},
{
input: []byte(`
name: test
dynamic: "strict"`),
output: Field{
Name: "test",
Dynamic: DynamicType{"strict"},
},
},
}

for _, test := range tests {
keys := Field{}

cfg, err := yaml.NewConfig(test.input)
assert.NoError(t, err)
err = cfg.Unpack(&keys)

if err != nil {
assert.True(t, test.error)
} else {
assert.Equal(t, test.output.Dynamic, keys.Dynamic)
}
}
}
Loading