-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[EPM] Adding support for nested fields #64829
Changes from 4 commits
66d5783
ee07bd0
ffb3a34
4dd19a3
5301a3f
22b4a69
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 |
---|---|---|
|
@@ -28,6 +28,8 @@ export interface Field { | |
object_type?: string; | ||
scaling_factor?: number; | ||
dynamic?: 'strict' | boolean; | ||
include_in_parent?: boolean; | ||
include_in_root?: boolean; | ||
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. |
||
|
||
// Kibana specific | ||
analyzed?: boolean; | ||
|
@@ -108,18 +110,54 @@ function dedupFields(fields: Fields): Fields { | |
return f.name === field.name; | ||
}); | ||
if (found) { | ||
// remove name, type, and fields from `field` variable so we avoid merging them into `found` | ||
const { name, type, fields: nestedFields, ...importantFieldProps } = field; | ||
/** | ||
* There are a couple scenarios this if is trying to account for: | ||
* Example 1 | ||
* - name: a.b | ||
* - name: a | ||
* In this scenario found will be `group` and field could be either `object` or `nested` | ||
* Example 2 | ||
* - name: a | ||
* - name: a.b | ||
* In this scenario found could be `object` or `nested` and field will be group | ||
*/ | ||
if ( | ||
(found.type === 'group' || found.type === 'object') && | ||
field.type === 'group' && | ||
field.fields | ||
// only merge if found is a group and field is object, nested, or group. | ||
// Or if found is object, or nested, and field is a group. | ||
// This is to avoid merging two objects, or nested, or object with a nested. | ||
(found.type === 'group' && | ||
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. 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. For now, I'm fine with it, especially with all the comments you added. I'd suggest adding an issue in the kibana repo for beta1 to revisit 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. Sounds good, here's the issue: #64901 |
||
(field.type === 'object' || field.type === 'nested' || field.type === 'group')) || | ||
((found.type === 'object' || found.type === 'nested') && field.type === 'group') | ||
) { | ||
if (!found.fields) { | ||
found.fields = []; | ||
// if the new field has properties let's dedup and concat them with the already existing found variable in | ||
// the array | ||
if (field.fields) { | ||
// if the found type was object or nested it won't have a fields array so let's initialize it | ||
if (!found.fields) { | ||
found.fields = []; | ||
} | ||
found.fields = dedupFields(found.fields.concat(field.fields)); | ||
} | ||
found.type = 'group'; | ||
found.fields = dedupFields(found.fields.concat(field.fields)); | ||
|
||
// if found already had fields or got new ones from the new field coming in we need to assign the right | ||
// type to it | ||
if (found.fields) { | ||
// If this field is supposed to be `nested` and we have fields, we need to preserve the fact that it is | ||
// supposed to be `nested` for when the template is actually generated | ||
if (found.type === 'nested' || field.type === 'nested') { | ||
found.type = 'group-nested'; | ||
} else { | ||
// found was either `group` already or `object` so just set it to `group` | ||
found.type = 'group'; | ||
} | ||
} | ||
// we need to merge in other properties (like `dynamic`) that might exist | ||
Object.assign(found, importantFieldProps); | ||
// if `field.type` wasn't group object or nested, then there's a conflict in types, so lets ignore it | ||
} else { | ||
// only 'group' fields can be merged in this way | ||
// only `group`, `object`, and `nested` fields can be merged in this way | ||
// XXX: don't abort on error for now | ||
// see discussion in https://github.com/elastic/kibana/pull/59894 | ||
// throw new Error( | ||
|
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.
👍 for factoring this out into a separate helper function.
Could we not mutate the input parameter
fieldProps
in these calls? In other places we return a (mutated) copy, so that line would readand it is more readable when we stay consistent throughout the code base.
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.
good point, I updated to keep it similar with the other cases.