-
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
[Expressions] Introduce createTable expression function, and use in Lens #103788
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac6c0bc
[Expressions] Introduce createTable expression function, and use in Lens
wylieconlon 81be1bd
Fix test
wylieconlon 480bbc3
Merge remote-tracking branch 'origin/master' into create-table-fn
wylieconlon 39ddde8
Fix code style
wylieconlon 0de832b
Fix typo
wylieconlon dda09c6
Merge branch 'master' into create-table-fn
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/plugins/expressions/common/expression_functions/specs/create_table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import { ExpressionFunctionDefinition } from '../types'; | ||
import { Datatable, DatatableColumn } from '../../expression_types'; | ||
|
||
export interface CreateTableArguments { | ||
ids: string[]; | ||
names: string[] | null; | ||
rowCount: number; | ||
} | ||
|
||
export const createTable: ExpressionFunctionDefinition< | ||
'createTable', | ||
null, | ||
CreateTableArguments, | ||
Datatable | ||
> = { | ||
name: 'createTable', | ||
type: 'datatable', | ||
inputTypes: ['null'], | ||
help: i18n.translate('expressions.functions.createTableHelpText', { | ||
defaultMessage: | ||
'Creates a datatable with a list of columns, and 1 or more empty rows. ' + | ||
'To populate the rows, use {mapColumnFn} or {mathColumnFn}.', | ||
values: { | ||
mathColumnFn: '`mathColumn`', | ||
mapColumnFn: '`mapColumn`', | ||
}, | ||
}), | ||
args: { | ||
ids: { | ||
types: ['string'], | ||
help: i18n.translate('expressions.functions.createTable.args.idsHelpText', { | ||
defaultMessage: | ||
'Column ids to generate in positional order. ID represents the key in the row.', | ||
}), | ||
required: false, | ||
multi: true, | ||
}, | ||
names: { | ||
types: ['string'], | ||
help: i18n.translate('expressions.functions.createTable.args.nameHelpText', { | ||
defaultMessage: | ||
'Column names to generate in positional order. Names are not required to be unique, and default to the ID if not provided.', | ||
}), | ||
required: false, | ||
multi: true, | ||
}, | ||
rowCount: { | ||
types: ['number'], | ||
help: i18n.translate('expressions.functions.createTable.args.rowCountText', { | ||
defaultMessage: | ||
'The number of empty rows to add to the table, to be assigned a value later', | ||
}), | ||
default: 1, | ||
required: false, | ||
}, | ||
}, | ||
fn(input, args) { | ||
const columns: DatatableColumn[] = []; | ||
|
||
(args.ids ?? []).map((id, index) => { | ||
columns.push({ | ||
id, | ||
name: args.names?.[index] ?? id, | ||
meta: { type: 'null' }, | ||
}); | ||
}); | ||
|
||
return { | ||
columns, | ||
// Each row gets a unique object | ||
rows: [...Array(args.rowCount)].map(() => ({})), | ||
type: 'datatable', | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/plugins/expressions/common/expression_functions/specs/tests/create_table.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { functionWrapper } from './utils'; | ||
import { createTable } from '../create_table'; | ||
|
||
describe('clear', () => { | ||
const fn = functionWrapper(createTable); | ||
|
||
it('returns a blank table', () => { | ||
expect(fn(null, {})).toEqual({ | ||
type: 'datatable', | ||
columns: [], | ||
rows: [{}], | ||
}); | ||
}); | ||
|
||
it('creates a table with default names', () => { | ||
expect( | ||
fn(null, { | ||
ids: ['a', 'b'], | ||
rowCount: 3, | ||
}) | ||
).toEqual({ | ||
type: 'datatable', | ||
columns: [ | ||
{ id: 'a', name: 'a', meta: { type: 'null' } }, | ||
{ id: 'b', name: 'b', meta: { type: 'null' } }, | ||
], | ||
rows: [{}, {}, {}], | ||
}); | ||
}); | ||
|
||
it('create a table with names that match by position', () => { | ||
expect( | ||
fn(null, { | ||
ids: ['a', 'b'], | ||
names: ['name'], | ||
}) | ||
).toEqual({ | ||
type: 'datatable', | ||
columns: [ | ||
{ id: 'a', name: 'name', meta: { type: 'null' } }, | ||
{ id: 'b', name: 'b', meta: { type: 'null' } }, | ||
], | ||
rows: [{}], | ||
}); | ||
}); | ||
|
||
it('does provides unique objects for each row', () => { | ||
const table = fn(null, { | ||
ids: ['a', 'b'], | ||
rowCount: 2, | ||
}); | ||
|
||
table.rows[0].a = 'z'; | ||
table.rows[1].b = 5; | ||
|
||
expect(table.rows).toEqual([{ a: 'z' }, { b: 5 }]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ function parseAndExtract( | |
label?: string | ||
) { | ||
const { root, error } = tryToParse(text, operationDefinitionMap); | ||
if (error || !root) { | ||
if (error || typeof root === 'undefined' || root == null) { | ||
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. Same as above. |
||
return { extracted: [], isValid: false }; | ||
} | ||
// before extracting the data run the validation task and throw if invalid | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nitpick:
root == null
is sufficient for bothundefined
andnull