From 834bc41ec26af1222b6e332788fb22a6f7f31aea Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Thu, 17 Oct 2019 11:56:39 +0300 Subject: [PATCH] Move ui/utils/mapping_setup to NP and use that (#48396) Part of #43443 --- .../index_patterns/index_pattern.test.ts | 23 +++++--- .../index_patterns/index_pattern.tsx | 38 ++++++------- .../ui/public/saved_objects/saved_object.js | 3 +- .../public/utils/__tests__/mapping_setup.js | 56 ------------------- .../public/field_mapping/index.ts | 21 +++++++ .../field_mapping/mapping_setup.test.ts | 55 ++++++++++++++++++ .../public/field_mapping/mapping_setup.ts | 44 +++++++++++++++ .../public/field_mapping/types.ts} | 33 +++-------- src/plugins/kibana_utils/public/index.ts | 1 + 9 files changed, 162 insertions(+), 112 deletions(-) delete mode 100644 src/legacy/ui/public/utils/__tests__/mapping_setup.js create mode 100644 src/plugins/kibana_utils/public/field_mapping/index.ts create mode 100644 src/plugins/kibana_utils/public/field_mapping/mapping_setup.test.ts create mode 100644 src/plugins/kibana_utils/public/field_mapping/mapping_setup.ts rename src/{legacy/ui/public/utils/mapping_setup.js => plugins/kibana_utils/public/field_mapping/types.ts} (55%) diff --git a/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_pattern.test.ts b/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_pattern.test.ts index 479feabd07943..d8bd9623f5c08 100644 --- a/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_pattern.test.ts +++ b/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_pattern.test.ts @@ -34,15 +34,20 @@ jest.mock('ui/registry/field_formats', () => ({ }, })); -jest.mock('ui/utils/mapping_setup', () => ({ - expandShorthand: jest.fn().mockImplementation(() => ({ - id: true, - title: true, - fieldFormatMap: { - _deserialize: jest.fn().mockImplementation(() => []), - }, - })), -})); +jest.mock('../../../../../../plugins/kibana_utils/public', () => { + const originalModule = jest.requireActual('../../../../../../plugins/kibana_utils/public'); + + return { + ...originalModule, + expandShorthand: jest.fn(() => ({ + id: true, + title: true, + fieldFormatMap: { + _deserialize: jest.fn().mockImplementation(() => []), + }, + })), + }; +}); jest.mock('ui/notify', () => ({ toastNotifications: { diff --git a/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_pattern.tsx b/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_pattern.tsx index 7d4361d9c1d25..ba900cd762e20 100644 --- a/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_pattern.tsx +++ b/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_pattern.tsx @@ -25,32 +25,27 @@ import React from 'react'; import chrome from 'ui/chrome'; // @ts-ignore import { fieldFormats } from 'ui/registry/field_formats'; -// @ts-ignore -import { expandShorthand } from 'ui/utils/mapping_setup'; - import { NotificationsSetup, SavedObjectsClientContract } from 'src/core/public'; -import { SavedObjectNotFound, DuplicateField } from '../../../../../../plugins/kibana_utils/public'; -import { findIndexPatternByTitle } from '../utils'; +import { + DuplicateField, + SavedObjectNotFound, + expandShorthand, + FieldMappingSpec, + MappingObject, +} from '../../../../../../plugins/kibana_utils/public'; + +import { findIndexPatternByTitle, getRoutes } from '../utils'; import { IndexPatternMissingIndices } from '../errors'; -import { Field, FieldList, FieldType, FieldListInterface } from '../fields'; +import { Field, FieldList, FieldListInterface, FieldType } from '../fields'; import { createFieldsFetcher } from './_fields_fetcher'; -import { getRoutes } from '../utils'; import { formatHitProvider } from './format_hit'; import { flattenHitWrapper } from './flatten_hit'; import { IIndexPatternsApiClient } from './index_patterns_api_client'; +import { ES_FIELD_TYPES } from '../../../../../../plugins/data/common'; const MAX_ATTEMPTS_TO_RESOLVE_CONFLICTS = 3; const type = 'index-pattern'; -interface FieldMappingSpec { - _serialize: (mapping: any) => string; - _deserialize: (mapping: string) => any; -} - -interface MappingObject { - [key: string]: FieldMappingSpec; -} - export interface StaticIndexPattern { fields: FieldType[]; title: string; @@ -87,13 +82,13 @@ export class IndexPattern implements StaticIndexPattern { private shortDotsEnable: boolean = false; private mapping: MappingObject = expandShorthand({ - title: 'text', - timeFieldName: 'keyword', - intervalName: 'keyword', + title: ES_FIELD_TYPES.TEXT, + timeFieldName: ES_FIELD_TYPES.KEYWORD, + intervalName: ES_FIELD_TYPES.KEYWORD, fields: 'json', sourceFilters: 'json', fieldFormatMap: { - type: 'text', + type: ES_FIELD_TYPES.TEXT, _serialize: (map = {}) => { const serialized = _.transform(map, this.serializeFieldFormatMap); return _.isEmpty(serialized) ? undefined : JSON.stringify(serialized); @@ -104,7 +99,7 @@ export class IndexPattern implements StaticIndexPattern { }); }, }, - type: 'keyword', + type: ES_FIELD_TYPES.KEYWORD, typeMeta: 'json', }); @@ -187,6 +182,7 @@ export class IndexPattern implements StaticIndexPattern { if (!fieldMapping._deserialize || !name) { return; } + response._source[name] = fieldMapping._deserialize(response._source[name]); }); diff --git a/src/legacy/ui/public/saved_objects/saved_object.js b/src/legacy/ui/public/saved_objects/saved_object.js index cf83e6ef057f8..04505552dfc3f 100644 --- a/src/legacy/ui/public/saved_objects/saved_object.js +++ b/src/legacy/ui/public/saved_objects/saved_object.js @@ -32,8 +32,7 @@ import angular from 'angular'; import _ from 'lodash'; -import { InvalidJSONProperty, SavedObjectNotFound } from '../../../../plugins/kibana_utils/public'; -import { expandShorthand } from '../utils/mapping_setup'; +import { InvalidJSONProperty, SavedObjectNotFound, expandShorthand } from '../../../../plugins/kibana_utils/public'; import { SearchSourceProvider } from '../courier/search_source'; import { findObjectByTitle } from './find_object_by_title'; diff --git a/src/legacy/ui/public/utils/__tests__/mapping_setup.js b/src/legacy/ui/public/utils/__tests__/mapping_setup.js deleted file mode 100644 index acd8f82df280c..0000000000000 --- a/src/legacy/ui/public/utils/__tests__/mapping_setup.js +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import _ from 'lodash'; -import ngMock from 'ng_mock'; -import expect from '@kbn/expect'; -import { expandShorthand } from '../mapping_setup'; - -describe('ui/utils/mapping_setup', function () { - beforeEach(ngMock.module('kibana')); - - describe('#expandShorthand()', function () { - it('allows shortcuts for field types by just setting the value to the type name', function () { - const mapping = expandShorthand({ foo: 'boolean' }); - expect(mapping.foo.type).to.be('boolean'); - }); - - it('can set type as an option', function () { - const mapping = expandShorthand({ foo: { type: 'integer' } }); - expect(mapping.foo.type).to.be('integer'); - }); - - describe('when type is json', function () { - it('returned object is type text', function () { - const mapping = expandShorthand({ foo: 'json' }); - expect(mapping.foo.type).to.be('text'); - }); - - it('returned object has _serialize function', function () { - const mapping = expandShorthand({ foo: 'json' }); - expect(_.isFunction(mapping.foo._serialize)).to.be(true); - }); - - it('returned object has _deserialize function', function () { - const mapping = expandShorthand({ foo: 'json' }); - expect(_.isFunction(mapping.foo._serialize)).to.be(true); - }); - }); - }); -}); diff --git a/src/plugins/kibana_utils/public/field_mapping/index.ts b/src/plugins/kibana_utils/public/field_mapping/index.ts new file mode 100644 index 0000000000000..060d5a95790f7 --- /dev/null +++ b/src/plugins/kibana_utils/public/field_mapping/index.ts @@ -0,0 +1,21 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +export { FieldMappingSpec, MappingObject } from './types'; +export { expandShorthand } from './mapping_setup'; diff --git a/src/plugins/kibana_utils/public/field_mapping/mapping_setup.test.ts b/src/plugins/kibana_utils/public/field_mapping/mapping_setup.test.ts new file mode 100644 index 0000000000000..e57699e879a87 --- /dev/null +++ b/src/plugins/kibana_utils/public/field_mapping/mapping_setup.test.ts @@ -0,0 +1,55 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { expandShorthand } from './mapping_setup'; +import { ES_FIELD_TYPES } from '../../../data/common'; + +describe('mapping_setup', () => { + it('allows shortcuts for field types by just setting the value to the type name', () => { + const mapping = expandShorthand({ foo: ES_FIELD_TYPES.BOOLEAN }); + + expect(mapping.foo.type).toBe('boolean'); + }); + + it('can set type as an option', () => { + const mapping = expandShorthand({ foo: { type: ES_FIELD_TYPES.INTEGER } }); + + expect(mapping.foo.type).toBe('integer'); + }); + + describe('when type is json', () => { + it('returned object is type text', () => { + const mapping = expandShorthand({ foo: 'json' }); + + expect(mapping.foo.type).toBe('text'); + }); + + it('returned object has _serialize function', () => { + const mapping = expandShorthand({ foo: 'json' }); + + expect(mapping.foo._serialize).toBeInstanceOf(Function); + }); + + it('returned object has _deserialize function', () => { + const mapping = expandShorthand({ foo: 'json' }); + + expect(mapping.foo._serialize).toBeInstanceOf(Function); + }); + }); +}); diff --git a/src/plugins/kibana_utils/public/field_mapping/mapping_setup.ts b/src/plugins/kibana_utils/public/field_mapping/mapping_setup.ts new file mode 100644 index 0000000000000..495338735337c --- /dev/null +++ b/src/plugins/kibana_utils/public/field_mapping/mapping_setup.ts @@ -0,0 +1,44 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { mapValues, isString } from 'lodash'; +import { ES_FIELD_TYPES } from '../../../../plugins/data/common'; +import { FieldMappingSpec, MappingObject } from './types'; + +/** @private */ +type ShorthandFieldMapObject = FieldMappingSpec | ES_FIELD_TYPES | 'json'; + +const json: FieldMappingSpec = { + type: ES_FIELD_TYPES.TEXT, + _serialize(v) { + if (v) return JSON.stringify(v); + }, + _deserialize(v) { + if (v) return JSON.parse(v); + }, +}; + +/** @public */ +export const expandShorthand = (sh: Record): MappingObject => { + return mapValues>(sh, (val: ShorthandFieldMapObject) => { + const fieldMap = isString(val) ? { type: val } : val; + + return fieldMap.type === 'json' ? json : fieldMap; + }) as MappingObject; +}; diff --git a/src/legacy/ui/public/utils/mapping_setup.js b/src/plugins/kibana_utils/public/field_mapping/types.ts similarity index 55% rename from src/legacy/ui/public/utils/mapping_setup.js rename to src/plugins/kibana_utils/public/field_mapping/types.ts index 6154cc7eeb344..973a58d3baec4 100644 --- a/src/legacy/ui/public/utils/mapping_setup.js +++ b/src/plugins/kibana_utils/public/field_mapping/types.ts @@ -17,29 +17,14 @@ * under the License. */ -import { mapValues } from 'lodash'; +import { ES_FIELD_TYPES } from '../../../data/common'; -const json = { - _serialize: function (val) { - if (val != null) return JSON.stringify(val); - }, - _deserialize: function (val) { - if (val != null) return JSON.parse(val); - } -}; +/** @public */ +export interface FieldMappingSpec { + type: ES_FIELD_TYPES; + _serialize?: (mapping: any) => string | undefined; + _deserialize?: (mapping: string) => any | undefined; +} -export const expandShorthand = function (sh) { - return mapValues(sh || {}, function (val) { - // allow shortcuts for the field types, by just setting the value - // to the type name - if (typeof val === 'string') val = { type: val }; - - if (val.type === 'json') { - val.type = 'text'; - val._serialize = json._serialize; - val._deserialize = json._deserialize; - } - - return val; - }); -}; +/** @public */ +export type MappingObject = Record; diff --git a/src/plugins/kibana_utils/public/index.ts b/src/plugins/kibana_utils/public/index.ts index ab609583f84ee..bac0ef629789a 100644 --- a/src/plugins/kibana_utils/public/index.ts +++ b/src/plugins/kibana_utils/public/index.ts @@ -21,3 +21,4 @@ export * from './store'; export * from './parse'; export * from './render_complete'; export * from './errors'; +export * from './field_mapping';