From e6a3f988f6afff547cdd27111bfbdcb7c4945056 Mon Sep 17 00:00:00 2001 From: Matthew Kime Date: Thu, 20 Feb 2020 15:29:39 -0600 Subject: [PATCH] Fix browser date format (#57714) (#58146) * fix browser date formatter --- .../constants/base_formatters.ts | 2 -- .../common/field_formats/converters/index.ts | 1 - .../field_formats/field_formats_registry.ts | 4 +++ .../data/common/field_formats/index.ts | 1 - .../data/common/field_formats/types.ts | 1 + src/plugins/data/common/types.ts | 1 + .../data/public/field_formats/constants.ts | 23 +++++++++++++ .../field_formats/converters/date.test.ts | 0 .../field_formats/converters/date.ts | 9 +++-- .../public/field_formats/converters/index.ts | 20 +++++++++++ .../field_formats_service.test.ts | 34 +++++++++++++++++++ .../field_formats/field_formats_service.ts | 19 +++++++---- .../data/public/field_formats/index.ts | 2 ++ src/plugins/data/public/index.ts | 4 ++- src/plugins/data/public/mocks.ts | 1 + .../query_string_input.test.tsx.snap | 6 ++++ .../field_formats/converters/date_server.ts | 6 ++-- .../server/field_formats/converters/index.ts | 20 +++++++++++ .../field_formats_service.test.ts | 34 +++++++++++++++++++ .../field_formats/field_formats_service.ts | 3 +- src/plugins/data/server/index.ts | 4 --- src/test_utils/public/stub_field_formats.ts | 3 +- 22 files changed, 174 insertions(+), 24 deletions(-) create mode 100644 src/plugins/data/public/field_formats/constants.ts rename src/plugins/data/{common => public}/field_formats/converters/date.test.ts (100%) rename src/plugins/data/{common => public}/field_formats/converters/date.ts (92%) create mode 100644 src/plugins/data/public/field_formats/converters/index.ts create mode 100644 src/plugins/data/public/field_formats/field_formats_service.test.ts rename src/plugins/data/{common => server}/field_formats/converters/date_server.ts (95%) create mode 100644 src/plugins/data/server/field_formats/converters/index.ts create mode 100644 src/plugins/data/server/field_formats/field_formats_service.test.ts diff --git a/src/plugins/data/common/field_formats/constants/base_formatters.ts b/src/plugins/data/common/field_formats/constants/base_formatters.ts index 95aedd02d16d6..6befe8cea71f5 100644 --- a/src/plugins/data/common/field_formats/constants/base_formatters.ts +++ b/src/plugins/data/common/field_formats/constants/base_formatters.ts @@ -23,7 +23,6 @@ import { BoolFormat, BytesFormat, ColorFormat, - DateFormat, DateNanosFormat, DurationFormat, IpFormat, @@ -41,7 +40,6 @@ export const baseFormatters: IFieldFormatType[] = [ BoolFormat, BytesFormat, ColorFormat, - DateFormat, DateNanosFormat, DurationFormat, IpFormat, diff --git a/src/plugins/data/common/field_formats/converters/index.ts b/src/plugins/data/common/field_formats/converters/index.ts index f7e50539b44d8..cc9fae7fc9965 100644 --- a/src/plugins/data/common/field_formats/converters/index.ts +++ b/src/plugins/data/common/field_formats/converters/index.ts @@ -19,7 +19,6 @@ export { UrlFormat } from './url'; export { BytesFormat } from './bytes'; -export { DateFormat } from './date_server'; export { DateNanosFormat } from './date_nanos'; export { RelativeDateFormat } from './relative_date'; export { DurationFormat } from './duration'; diff --git a/src/plugins/data/common/field_formats/field_formats_registry.ts b/src/plugins/data/common/field_formats/field_formats_registry.ts index 9fe9a31307b6a..9fdf1ad9c80fb 100644 --- a/src/plugins/data/common/field_formats/field_formats_registry.ts +++ b/src/plugins/data/common/field_formats/field_formats_registry.ts @@ -95,6 +95,10 @@ export class FieldFormatsRegistry { return undefined; }; + getTypeWithoutMetaParams = (formatId: FieldFormatId): IFieldFormatType | undefined => { + return this.fieldFormats.get(formatId); + }; + /** * Get the default FieldFormat type (class) for * a field type, using the format:defaultTypeMap. diff --git a/src/plugins/data/common/field_formats/index.ts b/src/plugins/data/common/field_formats/index.ts index d7858966f2620..13d3d9d73d43a 100644 --- a/src/plugins/data/common/field_formats/index.ts +++ b/src/plugins/data/common/field_formats/index.ts @@ -27,7 +27,6 @@ export { BoolFormat, BytesFormat, ColorFormat, - DateFormat, DateNanosFormat, DurationFormat, IpFormat, diff --git a/src/plugins/data/common/field_formats/types.ts b/src/plugins/data/common/field_formats/types.ts index 24aa92c67b694..0c16d9f1ac8bf 100644 --- a/src/plugins/data/common/field_formats/types.ts +++ b/src/plugins/data/common/field_formats/types.ts @@ -18,6 +18,7 @@ */ import { FieldFormat } from './field_format'; +export { FieldFormat }; /** @public **/ export type FieldFormatsContentType = 'html' | 'text'; diff --git a/src/plugins/data/common/types.ts b/src/plugins/data/common/types.ts index be0d3230b3a0e..93629c3dbaf62 100644 --- a/src/plugins/data/common/types.ts +++ b/src/plugins/data/common/types.ts @@ -21,3 +21,4 @@ export * from './timefilter/types'; export * from './query/types'; export * from './kbn_field_types/types'; export * from './index_patterns/types'; +export { TextContextTypeConvert, IFieldFormatMetaParams } from './field_formats/types'; diff --git a/src/plugins/data/public/field_formats/constants.ts b/src/plugins/data/public/field_formats/constants.ts new file mode 100644 index 0000000000000..a5c2b4e379908 --- /dev/null +++ b/src/plugins/data/public/field_formats/constants.ts @@ -0,0 +1,23 @@ +/* + * 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 { baseFormatters } from '../../common'; +import { DateFormat } from './converters/date'; + +export const baseFormattersPublic = [DateFormat, ...baseFormatters]; diff --git a/src/plugins/data/common/field_formats/converters/date.test.ts b/src/plugins/data/public/field_formats/converters/date.test.ts similarity index 100% rename from src/plugins/data/common/field_formats/converters/date.test.ts rename to src/plugins/data/public/field_formats/converters/date.test.ts diff --git a/src/plugins/data/common/field_formats/converters/date.ts b/src/plugins/data/public/field_formats/converters/date.ts similarity index 92% rename from src/plugins/data/common/field_formats/converters/date.ts rename to src/plugins/data/public/field_formats/converters/date.ts index 3888df051b118..3e1efdc69dec8 100644 --- a/src/plugins/data/common/field_formats/converters/date.ts +++ b/src/plugins/data/public/field_formats/converters/date.ts @@ -20,9 +20,12 @@ import { i18n } from '@kbn/i18n'; import { memoize, noop } from 'lodash'; import moment from 'moment'; -import { KBN_FIELD_TYPES } from '../../kbn_field_types/types'; -import { FieldFormat } from '../field_format'; -import { TextContextTypeConvert, FIELD_FORMAT_IDS } from '../types'; +import { + FieldFormat, + KBN_FIELD_TYPES, + TextContextTypeConvert, + FIELD_FORMAT_IDS, +} from '../../../common'; export class DateFormat extends FieldFormat { static id = FIELD_FORMAT_IDS.DATE; diff --git a/src/plugins/data/public/field_formats/converters/index.ts b/src/plugins/data/public/field_formats/converters/index.ts new file mode 100644 index 0000000000000..c51111092beca --- /dev/null +++ b/src/plugins/data/public/field_formats/converters/index.ts @@ -0,0 +1,20 @@ +/* + * 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 { DateFormat } from './date'; diff --git a/src/plugins/data/public/field_formats/field_formats_service.test.ts b/src/plugins/data/public/field_formats/field_formats_service.test.ts new file mode 100644 index 0000000000000..e066af28f4699 --- /dev/null +++ b/src/plugins/data/public/field_formats/field_formats_service.test.ts @@ -0,0 +1,34 @@ +/* + * 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 { FieldFormatsService } from './field_formats_service'; +import { coreMock } from '../../../../../src/core/public/mocks'; +import { DateFormat } from './converters/date'; + +describe('FieldFormatService', () => { + test('DateFormat is public version', () => { + const mockCore = coreMock.createSetup(); + const service = new FieldFormatsService(); + service.setup(mockCore); + const fieldFormatsRegistry = service.start(); + const DateFormatFromRegsitry = fieldFormatsRegistry.getTypeWithoutMetaParams('date'); + + expect(DateFormatFromRegsitry).toEqual(DateFormat); + }); +}); diff --git a/src/plugins/data/public/field_formats/field_formats_service.ts b/src/plugins/data/public/field_formats/field_formats_service.ts index 785bedf9b35d3..22c7e90c06130 100644 --- a/src/plugins/data/public/field_formats/field_formats_service.ts +++ b/src/plugins/data/public/field_formats/field_formats_service.ts @@ -18,9 +18,10 @@ */ import { CoreSetup } from 'src/core/public'; -import { FieldFormatsRegistry } from '../../common/field_formats'; +import { FieldFormatsRegistry } from '../../common'; import { deserializeFieldFormat } from './utils/deserialize'; import { FormatFactory } from '../../common/field_formats/utils'; +import { baseFormattersPublic } from './constants'; export class FieldFormatsService { private readonly fieldFormatsRegistry: FieldFormatsRegistry = new FieldFormatsRegistry(); @@ -34,13 +35,17 @@ export class FieldFormatsService { const getConfig = core.uiSettings.get.bind(core.uiSettings); - this.fieldFormatsRegistry.init(getConfig, { - parsedUrl: { - origin: window.location.origin, - pathname: window.location.pathname, - basePath: core.http.basePath.get(), + this.fieldFormatsRegistry.init( + getConfig, + { + parsedUrl: { + origin: window.location.origin, + pathname: window.location.pathname, + basePath: core.http.basePath.get(), + }, }, - }); + baseFormattersPublic + ); return this.fieldFormatsRegistry as FieldFormatsSetup; } diff --git a/src/plugins/data/public/field_formats/index.ts b/src/plugins/data/public/field_formats/index.ts index 4550a5781535f..015d5b39561bb 100644 --- a/src/plugins/data/public/field_formats/index.ts +++ b/src/plugins/data/public/field_formats/index.ts @@ -18,3 +18,5 @@ */ export { FieldFormatsService, FieldFormatsSetup, FieldFormatsStart } from './field_formats_service'; +export { DateFormat } from './converters'; +export { baseFormattersPublic } from './constants'; diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index cdc4167f545af..cbd4bfd348797 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -156,7 +156,6 @@ import { BoolFormat, BytesFormat, ColorFormat, - DateFormat, DateNanosFormat, DurationFormat, IpFormat, @@ -171,6 +170,9 @@ import { serializeFieldFormat, } from '../common/field_formats'; +import { DateFormat } from './field_formats'; +export { baseFormattersPublic } from './field_formats'; + // Field formats helpers namespace: export const fieldFormats = { FieldFormat, diff --git a/src/plugins/data/public/mocks.ts b/src/plugins/data/public/mocks.ts index 2d5cc72597ec4..a2a1a2424fc90 100644 --- a/src/plugins/data/public/mocks.ts +++ b/src/plugins/data/public/mocks.ts @@ -50,6 +50,7 @@ const fieldFormatsMock: IFieldFormatsRegistry = { register: jest.fn(), parseDefaultTypeMap: jest.fn(), deserialize: jest.fn(), + getTypeWithoutMetaParams: jest.fn(), }; const createSetupContract = (): Setup => { diff --git a/src/plugins/data/public/ui/query_string_input/__snapshots__/query_string_input.test.tsx.snap b/src/plugins/data/public/ui/query_string_input/__snapshots__/query_string_input.test.tsx.snap index 06e56aaf3eb0a..93af543fba1a8 100644 --- a/src/plugins/data/public/ui/query_string_input/__snapshots__/query_string_input.test.tsx.snap +++ b/src/plugins/data/public/ui/query_string_input/__snapshots__/query_string_input.test.tsx.snap @@ -184,6 +184,7 @@ exports[`QueryStringInput Should disable autoFocus on EuiFieldText when disableA "getInstance": [MockFunction], "getType": [MockFunction], "getTypeNameByEsTypes": [MockFunction], + "getTypeWithoutMetaParams": [MockFunction], "init": [MockFunction], "parseDefaultTypeMap": [MockFunction], "register": [MockFunction], @@ -839,6 +840,7 @@ exports[`QueryStringInput Should disable autoFocus on EuiFieldText when disableA "getInstance": [MockFunction], "getType": [MockFunction], "getTypeNameByEsTypes": [MockFunction], + "getTypeWithoutMetaParams": [MockFunction], "init": [MockFunction], "parseDefaultTypeMap": [MockFunction], "register": [MockFunction], @@ -1476,6 +1478,7 @@ exports[`QueryStringInput Should pass the query language to the language switche "getInstance": [MockFunction], "getType": [MockFunction], "getTypeNameByEsTypes": [MockFunction], + "getTypeWithoutMetaParams": [MockFunction], "init": [MockFunction], "parseDefaultTypeMap": [MockFunction], "register": [MockFunction], @@ -2128,6 +2131,7 @@ exports[`QueryStringInput Should pass the query language to the language switche "getInstance": [MockFunction], "getType": [MockFunction], "getTypeNameByEsTypes": [MockFunction], + "getTypeWithoutMetaParams": [MockFunction], "init": [MockFunction], "parseDefaultTypeMap": [MockFunction], "register": [MockFunction], @@ -2765,6 +2769,7 @@ exports[`QueryStringInput Should render the given query 1`] = ` "getInstance": [MockFunction], "getType": [MockFunction], "getTypeNameByEsTypes": [MockFunction], + "getTypeWithoutMetaParams": [MockFunction], "init": [MockFunction], "parseDefaultTypeMap": [MockFunction], "register": [MockFunction], @@ -3417,6 +3422,7 @@ exports[`QueryStringInput Should render the given query 1`] = ` "getInstance": [MockFunction], "getType": [MockFunction], "getTypeNameByEsTypes": [MockFunction], + "getTypeWithoutMetaParams": [MockFunction], "init": [MockFunction], "parseDefaultTypeMap": [MockFunction], "register": [MockFunction], diff --git a/src/plugins/data/common/field_formats/converters/date_server.ts b/src/plugins/data/server/field_formats/converters/date_server.ts similarity index 95% rename from src/plugins/data/common/field_formats/converters/date_server.ts rename to src/plugins/data/server/field_formats/converters/date_server.ts index 216af133bb5f5..f4e6296259196 100644 --- a/src/plugins/data/common/field_formats/converters/date_server.ts +++ b/src/plugins/data/server/field_formats/converters/date_server.ts @@ -20,14 +20,14 @@ import { i18n } from '@kbn/i18n'; import { memoize, noop } from 'lodash'; import moment from 'moment-timezone'; -import { KBN_FIELD_TYPES } from '../../kbn_field_types/types'; -import { FieldFormat } from '../field_format'; import { + FieldFormat, + KBN_FIELD_TYPES, TextContextTypeConvert, FIELD_FORMAT_IDS, FieldFormatsGetConfigFn, IFieldFormatMetaParams, -} from '../types'; +} from '../../../common'; export class DateFormat extends FieldFormat { static id = FIELD_FORMAT_IDS.DATE; diff --git a/src/plugins/data/server/field_formats/converters/index.ts b/src/plugins/data/server/field_formats/converters/index.ts new file mode 100644 index 0000000000000..f5c69df972869 --- /dev/null +++ b/src/plugins/data/server/field_formats/converters/index.ts @@ -0,0 +1,20 @@ +/* + * 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 { DateFormat } from './date_server'; diff --git a/src/plugins/data/server/field_formats/field_formats_service.test.ts b/src/plugins/data/server/field_formats/field_formats_service.test.ts new file mode 100644 index 0000000000000..2e7ce0fa435a7 --- /dev/null +++ b/src/plugins/data/server/field_formats/field_formats_service.test.ts @@ -0,0 +1,34 @@ +/* + * 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 { FieldFormatsService } from './field_formats_service'; +import { DateFormat } from './converters/date_server'; +import { coreMock } from '../../../../core/server/mocks'; + +describe('FieldFormatService', () => { + test('DateFormat is server version', async () => { + const service = new FieldFormatsService(); + const fieldFormatsService = await service.start(); + const uiSettings = coreMock.createStart().uiSettings.asScopedToClient({} as any); + const fieldFormatsRegistry = await fieldFormatsService.fieldFormatServiceFactory(uiSettings); + const DateFormatFromRegsitry = fieldFormatsRegistry.getTypeWithoutMetaParams('date'); + + expect(DateFormatFromRegsitry).toEqual(DateFormat); + }); +}); diff --git a/src/plugins/data/server/field_formats/field_formats_service.ts b/src/plugins/data/server/field_formats/field_formats_service.ts index a31e5927ab800..0dac64fb5dc1d 100644 --- a/src/plugins/data/server/field_formats/field_formats_service.ts +++ b/src/plugins/data/server/field_formats/field_formats_service.ts @@ -19,9 +19,10 @@ import { has } from 'lodash'; import { FieldFormatsRegistry, IFieldFormatType, baseFormatters } from '../../common/field_formats'; import { IUiSettingsClient } from '../../../../core/server'; +import { DateFormat } from './converters'; export class FieldFormatsService { - private readonly fieldFormatClasses: IFieldFormatType[] = baseFormatters; + private readonly fieldFormatClasses: IFieldFormatType[] = [DateFormat, ...baseFormatters]; public setup() { return { diff --git a/src/plugins/data/server/index.ts b/src/plugins/data/server/index.ts index 3ee98a318de35..40d367138b60d 100644 --- a/src/plugins/data/server/index.ts +++ b/src/plugins/data/server/index.ts @@ -83,7 +83,6 @@ import { BoolFormat, BytesFormat, ColorFormat, - DateFormat, DateNanosFormat, DurationFormat, IpFormat, @@ -101,13 +100,10 @@ import { export const fieldFormats = { FieldFormatsRegistry, FieldFormat, - serializeFieldFormat, - BoolFormat, BytesFormat, ColorFormat, - DateFormat, DateNanosFormat, DurationFormat, IpFormat, diff --git a/src/test_utils/public/stub_field_formats.ts b/src/test_utils/public/stub_field_formats.ts index 5a20823134ebd..589e93fd600c2 100644 --- a/src/test_utils/public/stub_field_formats.ts +++ b/src/test_utils/public/stub_field_formats.ts @@ -19,12 +19,13 @@ import { CoreSetup } from 'kibana/public'; import { DataPublicPluginStart, fieldFormats } from '../../plugins/data/public'; import { deserializeFieldFormat } from '../../plugins/data/public/field_formats/utils/deserialize'; +import { baseFormattersPublic } from '../../plugins/data/public'; export const getFieldFormatsRegistry = (core: CoreSetup) => { const fieldFormatsRegistry = new fieldFormats.FieldFormatsRegistry(); const getConfig = core.uiSettings.get.bind(core.uiSettings); - fieldFormatsRegistry.init(getConfig, {}); + fieldFormatsRegistry.init(getConfig, {}, baseFormattersPublic); fieldFormatsRegistry.deserialize = deserializeFieldFormat.bind( fieldFormatsRegistry as DataPublicPluginStart['fieldFormats']