Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

export * from './src/constants';
export { convertDatatableColumnToDataViewFieldSpec } from './src/utils/convert_to_data_view_field_spec';
export { getDataViewFieldOrCreateFromColumnMeta } from './src/utils/get_data_view_field_or_create';
export { createRegExpPatternFrom } from './src/utils/create_regexp_pattern_from';
export { testPatternAgainstAllowedList } from './src/utils/test_pattern_against_allowed_list';
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { isEqual } from 'lodash';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { DatatableColumnMeta } from '@kbn/expressions-plugin/common';
import { convertDatatableColumnToDataViewFieldSpec } from './convert_to_data_view_field_spec';

export const getDataViewFieldOrCreateFromColumnMeta = ({
dataView,
fieldName,
columnMeta,
}: {
dataView: DataView;
fieldName: string;
columnMeta?: DatatableColumnMeta; // based on ES|QL query
}) => {
const dataViewField = dataView.fields.getByName(fieldName);

if (!columnMeta) {
return dataViewField;
}

const fieldSpecFromColumnMeta = convertDatatableColumnToDataViewFieldSpec({
name: fieldName,
id: fieldName,
meta: columnMeta,
});

if (
!dataViewField ||
dataViewField.type !== fieldSpecFromColumnMeta.type ||
!isEqual(dataViewField.esTypes, fieldSpecFromColumnMeta.esTypes)
) {
return dataView.fields.create(fieldSpecFromColumnMeta);
}

return dataViewField;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import { DataView, DataViewField, FieldSpec } from '@kbn/data-views-plugin/public';

export const shallowMockedFields = [
{
Expand Down Expand Up @@ -101,6 +101,10 @@ export const buildDataViewMock = ({
return dataViewFields;
};

dataViewFields.create = (spec: FieldSpec) => {
return new DataViewField(spec);
};

const dataView = {
id: `${name}-id`,
title: `${name}-title`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const buildTableContext = (dataView: DataView, rows: DataTableRecord[]): DataTab
fieldFormats: servicesMock.fieldFormats,
rows,
dataView,
columnsMeta: undefined,
options,
}),
};
Expand Down
Loading