-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[POC][Idea] Create field mappings on the fly instead of using template #7972
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,6 +119,15 @@ func (f Fields) HasKey(key string) bool { | |
return f.hasKey(keys) | ||
} | ||
|
||
// HasKey checks if inside fields the given key exists | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment on exported method Fields.GetKey should be of the form "GetKey ..." |
||
// 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) GetKey(key string) *Field { | ||
keys := strings.Split(key, ".") | ||
return f.getKey(keys) | ||
} | ||
|
||
// HasNode checks if inside fields the given node exists | ||
// In contrast to HasKey it not only compares the leaf nodes but | ||
// every single key it traverses. | ||
|
@@ -193,6 +202,32 @@ func (f Fields) hasKey(keys []string) bool { | |
return false | ||
} | ||
|
||
func (f Fields) getKey(keys []string) *Field { | ||
// Nothing to compare anymore | ||
if len(keys) == 0 { | ||
return nil | ||
} | ||
|
||
key := keys[0] | ||
keys = keys[1:] | ||
|
||
for _, field := range f { | ||
if field.Name == key { | ||
|
||
if len(field.Fields) > 0 { | ||
return field.Fields.getKey(keys) | ||
} | ||
// Last entry in the tree but still more keys | ||
if len(keys) > 0 { | ||
return nil | ||
} | ||
|
||
return &field | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// GetKeys returns a flat list of keys this Fields contains | ||
func (f Fields) GetKeys() []string { | ||
return f.getKeys("") | ||
|
@@ -216,3 +251,44 @@ func (f Fields) getKeys(namespace string) []string { | |
|
||
return keys | ||
} | ||
|
||
// GetKeys returns a flat list of keys this Fields contains | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment on exported method Fields.GetFlatFields should be of the form "GetFlatFields ..." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment on exported method Fields.GetFlatFields should be of the form "GetFlatFields ..." |
||
func (f Fields) GetFlatFields() map[string]Field { | ||
return f.getFlatFields("") | ||
} | ||
|
||
func (f Fields) getFlatFields(namespace string) map[string]Field { | ||
|
||
var fields = map[string]Field{} | ||
|
||
for _, field := range f { | ||
fieldName := namespace + "." + field.Name | ||
if namespace == "" { | ||
fieldName = field.Name | ||
} | ||
if len(field.Fields) == 0 { | ||
fields[fieldName] = field | ||
} else { | ||
f := field.Fields.getFlatFields(fieldName) | ||
// Iterate through all of them and add them | ||
for k, ff := range f { | ||
fields[k] = ff | ||
} | ||
} | ||
} | ||
|
||
return fields | ||
} | ||
|
||
func (f Fields) Flatten() map[string]Field { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method Fields.Flatten should have comment or be unexported There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method Fields.Flatten should have comment or be unexported |
||
keys := f.getKeys("") | ||
|
||
fields := map[string]Field{} | ||
for _, k := range keys { | ||
field := f.GetKey(k) | ||
field.Name = k | ||
fields[k] = *field | ||
} | ||
|
||
return fields | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,8 @@ var ( | |
dynamicTemplates []common.MapStr | ||
|
||
defaultFields []string | ||
|
||
Fields common.Fields | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported var Fields should have comment or be unexported There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported var Fields should have comment or be unexported |
||
) | ||
|
||
type Template struct { | ||
|
@@ -120,6 +122,8 @@ func (t *Template) load(fields common.Fields) (common.MapStr, error) { | |
t.Lock() | ||
defer t.Unlock() | ||
|
||
Fields = fields | ||
|
||
dynamicTemplates = nil | ||
defaultFields = nil | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment on exported method Fields.GetKey should be of the form "GetKey ..."