Skip to content

Commit

Permalink
Move ui/utils/mapping_setup to NP and use that
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Oct 16, 2019
1 parent 323d71e commit 4e2e833
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,27 @@ import _, { each, reject } from 'lodash';
import { i18n } from '@kbn/i18n';
// @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;
Expand Down Expand Up @@ -81,13 +76,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);
Expand All @@ -98,7 +93,7 @@ export class IndexPattern implements StaticIndexPattern {
});
},
},
type: 'keyword',
type: ES_FIELD_TYPES.KEYWORD,
typeMeta: 'json',
});

Expand Down Expand Up @@ -181,6 +176,7 @@ export class IndexPattern implements StaticIndexPattern {
if (!fieldMapping._deserialize || !name) {
return;
}

response._source[name] = fieldMapping._deserialize(response._source[name]);
});

Expand Down
3 changes: 1 addition & 2 deletions src/legacy/ui/public/saved_objects/saved_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
56 changes: 0 additions & 56 deletions src/legacy/ui/public/utils/__tests__/mapping_setup.js

This file was deleted.

21 changes: 21 additions & 0 deletions src/plugins/kibana_utils/public/field_mapping/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
44 changes: 44 additions & 0 deletions src/plugins/kibana_utils/public/field_mapping/mapping_setup.ts
Original file line number Diff line number Diff line change
@@ -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<string, ShorthandFieldMapObject>): MappingObject => {
return mapValues<Record<string, ShorthandFieldMapObject>>(sh, (val: ShorthandFieldMapObject) => {
const fieldMap = isString(val) ? { type: val } : val;

return fieldMap.type === 'json' ? json : fieldMap;
}) as MappingObject;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, FieldMappingSpec>;
1 change: 1 addition & 0 deletions src/plugins/kibana_utils/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from './store';
export * from './parse';
export * from './render_complete';
export * from './errors';
export * from './field_mapping';

0 comments on commit 4e2e833

Please sign in to comment.