From b998650f59a5a7867fa7707e08c9c1b5afb2a93f Mon Sep 17 00:00:00 2001 From: Catherine Liu Date: Mon, 17 May 2021 11:43:02 -0700 Subject: [PATCH] [Canvas] Fix column object shape in datatable created by CSV function (#98561) (#100226) Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../common/{csv.test.js => csv.test.ts} | 54 ++++++++++--------- .../canvas_plugin_src/functions/common/csv.ts | 6 ++- 2 files changed, 35 insertions(+), 25 deletions(-) rename x-pack/plugins/canvas/canvas_plugin_src/functions/common/{csv.test.js => csv.test.ts} (72%) diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.test.js b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.test.ts similarity index 72% rename from x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.test.js rename to x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.test.ts index 500224a7a3bbe..93cf07a9dd5dd 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.test.js +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.test.ts @@ -5,19 +5,21 @@ * 2.0. */ +// @ts-expect-error untyped lib import { functionWrapper } from '../../../test_helpers/function_wrapper'; import { getFunctionErrors } from '../../../i18n'; import { csv } from './csv'; +import { Datatable } from 'src/plugins/expressions'; const errors = getFunctionErrors().csv; describe('csv', () => { const fn = functionWrapper(csv); - const expected = { + const expected: Datatable = { type: 'datatable', columns: [ - { name: 'name', type: 'string' }, - { name: 'number', type: 'string' }, + { id: 'name', name: 'name', meta: { type: 'string' } }, + { id: 'number', name: 'number', meta: { type: 'string' } }, ], rows: [ { name: 'one', number: '1' }, @@ -69,43 +71,47 @@ fourty two%SPLIT%42`, }); it('should trim column names', () => { - expect( - fn(null, { - data: `foo," bar ", baz, " buz " -1,2,3,4`, - }) - ).toEqual({ + const expectedResult: Datatable = { type: 'datatable', columns: [ - { name: 'foo', type: 'string' }, - { name: 'bar', type: 'string' }, - { name: 'baz', type: 'string' }, - { name: 'buz', type: 'string' }, + { id: 'foo', name: 'foo', meta: { type: 'string' } }, + { id: 'bar', name: 'bar', meta: { type: 'string' } }, + { id: 'baz', name: 'baz', meta: { type: 'string' } }, + { id: 'buz', name: 'buz', meta: { type: 'string' } }, ], rows: [{ foo: '1', bar: '2', baz: '3', buz: '4' }], - }); - }); + }; - it('should handle odd spaces correctly', () => { expect( fn(null, { data: `foo," bar ", baz, " buz " -1," best ",3, " ok" -" good", bad, better , " worst " `, +1,2,3,4`, }) - ).toEqual({ + ).toEqual(expectedResult); + }); + + it('should handle odd spaces correctly', () => { + const expectedResult: Datatable = { type: 'datatable', columns: [ - { name: 'foo', type: 'string' }, - { name: 'bar', type: 'string' }, - { name: 'baz', type: 'string' }, - { name: 'buz', type: 'string' }, + { id: 'foo', name: 'foo', meta: { type: 'string' } }, + { id: 'bar', name: 'bar', meta: { type: 'string' } }, + { id: 'baz', name: 'baz', meta: { type: 'string' } }, + { id: 'buz', name: 'buz', meta: { type: 'string' } }, ], rows: [ { foo: '1', bar: ' best ', baz: '3', buz: ' ok' }, { foo: ' good', bar: ' bad', baz: ' better ', buz: ' worst ' }, ], - }); + }; + + expect( + fn(null, { + data: `foo," bar ", baz, " buz " +1," best ",3, " ok" +" good", bad, better , " worst " `, + }) + ).toEqual(expectedResult); }); it('throws when given invalid csv', () => { diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.ts b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.ts index 1f810e3658c6a..078721fc08d05 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.ts +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/csv.ts @@ -73,7 +73,11 @@ export function csv(): ExpressionFunctionDefinition<'csv', null, Arguments, Data if (i === 0) { // first row, assume header values row.forEach((colName: string) => - acc.columns.push({ name: colName.trim(), type: 'string' }) + acc.columns.push({ + id: colName.trim(), + name: colName.trim(), + meta: { type: 'string' }, + }) ); } else { // any other row is a data row