-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Expressions] Introduce createTable expression function, and use in L…
…ens (#103788) * [Expressions] Introduce createTable expression function, and use in Lens * Fix test * Fix code style * Fix typo Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
4dae939
commit 0ef3cbe
Showing
12 changed files
with
311 additions
and
10 deletions.
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
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.