From a68405faea08f654ee38fe561ebe1fe5494cb72a Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 31 Mar 2021 12:36:27 +0200 Subject: [PATCH] [ML] Fix types. Move shared utils to ML. --- x-pack/plugins/ml/common/index.ts | 2 ++ .../common/util/runtime_field_utils.test.ts} | 8 +++---- .../ml/common/util/runtime_field_utils.ts | 15 ++++++------ .../common/api_schemas/type_guards.ts | 2 +- .../transform/common/shared_imports.ts | 8 ++++++- .../transform/common/types/index_pattern.ts | 2 +- .../transform/common/types/transform.ts | 2 +- .../transform/common/types/transform_stats.ts | 2 +- .../plugins/transform/common/utils/errors.ts | 2 +- .../common/utils/object_utils.test.ts | 24 +------------------ .../transform/common/utils/object_utils.ts | 13 ---------- .../transform/public/app/common/pivot_aggs.ts | 2 +- .../public/app/common/pivot_group_by.ts | 2 +- .../transform/public/app/common/request.ts | 2 +- .../lib/authorization/components/common.ts | 2 +- .../advanced_runtime_mappings_editor.tsx | 3 ++- .../step_create/step_create_form.tsx | 2 +- .../filter_agg/components/filter_agg_form.tsx | 2 +- .../common/get_pivot_dropdown_options.ts | 2 +- .../components/step_define/common/types.ts | 19 +-------------- .../server/routes/api/transforms_nodes.ts | 2 +- 21 files changed, 37 insertions(+), 81 deletions(-) rename x-pack/plugins/{transform/public/app/sections/create_transform/components/step_define/common/types.test.ts => ml/common/util/runtime_field_utils.test.ts} (92%) diff --git a/x-pack/plugins/ml/common/index.ts b/x-pack/plugins/ml/common/index.ts index 0b95f2a23c136b..c15aa8f414fb1b 100644 --- a/x-pack/plugins/ml/common/index.ts +++ b/x-pack/plugins/ml/common/index.ts @@ -9,5 +9,7 @@ export { ES_CLIENT_TOTAL_HITS_RELATION } from './types/es_client'; export { ChartData } from './types/field_histograms'; export { ANOMALY_SEVERITY, ANOMALY_THRESHOLD, SEVERITY_COLORS } from './constants/anomalies'; export { getSeverityColor, getSeverityType } from './util/anomaly_utils'; +export { isPopulatedObject } from './util/object_utils'; +export { isRuntimeMappings } from './util/runtime_field_utils'; export { composeValidators, patternValidator } from './util/validators'; export { extractErrorMessage } from './util/errors'; diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/types.test.ts b/x-pack/plugins/ml/common/util/runtime_field_utils.test.ts similarity index 92% rename from x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/types.test.ts rename to x-pack/plugins/ml/common/util/runtime_field_utils.test.ts index ec90d31a0d1698..2824a55c2f7406 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/types.test.ts +++ b/x-pack/plugins/ml/common/util/runtime_field_utils.test.ts @@ -5,10 +5,10 @@ * 2.0. */ -import { isRuntimeField, isRuntimeMappings } from './types'; +import { isRuntimeField, isRuntimeMappings } from './runtime_field_utils'; -describe('Transform: step_define type guards', () => { - it('isRuntimeField()', () => { +describe('ML runtime field utils', () => { + describe('isRuntimeField()', () => { expect(isRuntimeField(1)).toBe(false); expect(isRuntimeField(null)).toBe(false); expect(isRuntimeField([])).toBe(false); @@ -21,7 +21,7 @@ describe('Transform: step_define type guards', () => { expect(isRuntimeField({ type: 'keyword', script: 'some script' })).toBe(true); }); - it('isRuntimeMappings()', () => { + describe('isRuntimeMappings()', () => { expect(isRuntimeMappings(1)).toBe(false); expect(isRuntimeMappings(null)).toBe(false); expect(isRuntimeMappings([])).toBe(false); diff --git a/x-pack/plugins/ml/common/util/runtime_field_utils.ts b/x-pack/plugins/ml/common/util/runtime_field_utils.ts index f2df5821268cd3..6d911ecd5d3cba 100644 --- a/x-pack/plugins/ml/common/util/runtime_field_utils.ts +++ b/x-pack/plugins/ml/common/util/runtime_field_utils.ts @@ -9,19 +9,18 @@ import { isPopulatedObject } from './object_utils'; import { RUNTIME_FIELD_TYPES } from '../../../../../src/plugins/data/common'; import type { RuntimeField, RuntimeMappings } from '../types/fields'; +type RuntimeType = typeof RUNTIME_FIELD_TYPES[number]; + export function isRuntimeField(arg: unknown): arg is RuntimeField { return ( - isPopulatedObject(arg) && - ((Object.keys(arg).length === 1 && arg.hasOwnProperty('type')) || - (Object.keys(arg).length === 2 && - arg.hasOwnProperty('type') && - arg.hasOwnProperty('script') && + ((isPopulatedObject(arg, ['type']) && Object.keys(arg).length === 1) || + (isPopulatedObject(arg, ['type', 'script']) && + Object.keys(arg).length === 2 && (typeof arg.script === 'string' || - (isPopulatedObject(arg.script) && + (isPopulatedObject(arg.script, ['source']) && Object.keys(arg.script).length === 1 && - arg.script.hasOwnProperty('source') && typeof arg.script.source === 'string')))) && - RUNTIME_FIELD_TYPES.includes(arg.type) + RUNTIME_FIELD_TYPES.includes(arg.type as RuntimeType) ); } diff --git a/x-pack/plugins/transform/common/api_schemas/type_guards.ts b/x-pack/plugins/transform/common/api_schemas/type_guards.ts index abcabb7d0aeb54..4b66de9be20d28 100644 --- a/x-pack/plugins/transform/common/api_schemas/type_guards.ts +++ b/x-pack/plugins/transform/common/api_schemas/type_guards.ts @@ -8,7 +8,7 @@ import type { estypes } from '@elastic/elasticsearch'; import type { EsIndex } from '../types/es_index'; -import { isPopulatedObject } from '../utils/object_utils'; +import { isPopulatedObject } from '../shared_imports'; // To be able to use the type guards on the client side, we need to make sure we don't import // the code of '@kbn/config-schema' but just its types, otherwise the client side code will diff --git a/x-pack/plugins/transform/common/shared_imports.ts b/x-pack/plugins/transform/common/shared_imports.ts index c9b4e21e2e8e82..38cfb6bc457f19 100644 --- a/x-pack/plugins/transform/common/shared_imports.ts +++ b/x-pack/plugins/transform/common/shared_imports.ts @@ -5,4 +5,10 @@ * 2.0. */ -export { composeValidators, patternValidator, ChartData } from '../../ml/common'; +export { + composeValidators, + isPopulatedObject, + isRuntimeMappings, + patternValidator, + ChartData, +} from '../../ml/common'; diff --git a/x-pack/plugins/transform/common/types/index_pattern.ts b/x-pack/plugins/transform/common/types/index_pattern.ts index 1efa79bce26130..70c332bc6857df 100644 --- a/x-pack/plugins/transform/common/types/index_pattern.ts +++ b/x-pack/plugins/transform/common/types/index_pattern.ts @@ -7,7 +7,7 @@ import type { IndexPattern } from '../../../../../src/plugins/data/common'; -import { isPopulatedObject } from '../utils/object_utils'; +import { isPopulatedObject } from '../shared_imports'; // Custom minimal type guard for IndexPattern to check against the attributes used in transforms code. export function isIndexPattern(arg: any): arg is IndexPattern { diff --git a/x-pack/plugins/transform/common/types/transform.ts b/x-pack/plugins/transform/common/types/transform.ts index 079c4650bd47b9..f1e7efdadca9d5 100644 --- a/x-pack/plugins/transform/common/types/transform.ts +++ b/x-pack/plugins/transform/common/types/transform.ts @@ -7,7 +7,7 @@ import { EuiComboBoxOptionOption } from '@elastic/eui/src/components/combo_box/types'; import type { LatestFunctionConfig, PutTransformsRequestSchema } from '../api_schemas/transforms'; -import { isPopulatedObject } from '../utils/object_utils'; +import { isPopulatedObject } from '../shared_imports'; import { PivotGroupByDict } from './pivot_group_by'; import { PivotAggDict } from './pivot_aggs'; diff --git a/x-pack/plugins/transform/common/types/transform_stats.ts b/x-pack/plugins/transform/common/types/transform_stats.ts index fc193abc981244..00ffa40b84d3b3 100644 --- a/x-pack/plugins/transform/common/types/transform_stats.ts +++ b/x-pack/plugins/transform/common/types/transform_stats.ts @@ -6,7 +6,7 @@ */ import { TransformState, TRANSFORM_STATE } from '../constants'; -import { isPopulatedObject } from '../utils/object_utils'; +import { isPopulatedObject } from '../shared_imports'; import { TransformId } from './transform'; export interface TransformStats { diff --git a/x-pack/plugins/transform/common/utils/errors.ts b/x-pack/plugins/transform/common/utils/errors.ts index 7dfb9202c7800f..2aff8f332b1308 100644 --- a/x-pack/plugins/transform/common/utils/errors.ts +++ b/x-pack/plugins/transform/common/utils/errors.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { isPopulatedObject } from './object_utils'; +import { isPopulatedObject } from '../shared_imports'; export interface ErrorResponse { body: { diff --git a/x-pack/plugins/transform/common/utils/object_utils.test.ts b/x-pack/plugins/transform/common/utils/object_utils.test.ts index fd0edeeb42a443..c99adf6b6d1894 100644 --- a/x-pack/plugins/transform/common/utils/object_utils.test.ts +++ b/x-pack/plugins/transform/common/utils/object_utils.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { getNestedProperty, isPopulatedObject } from './object_utils'; +import { getNestedProperty } from './object_utils'; describe('object_utils', () => { test('getNestedProperty()', () => { @@ -68,26 +68,4 @@ describe('object_utils', () => { expect(typeof test11).toBe('number'); expect(test11).toBe(0); }); - - test('isPopulatedObject()', () => { - expect(isPopulatedObject(0)).toBe(false); - expect(isPopulatedObject('')).toBe(false); - expect(isPopulatedObject(null)).toBe(false); - expect(isPopulatedObject({})).toBe(false); - expect(isPopulatedObject({ attribute: 'value' })).toBe(true); - expect(isPopulatedObject({ attribute: 'value' }, ['otherAttribute'])).toBe(false); - expect(isPopulatedObject({ attribute: 'value' }, ['attribute'])).toBe(true); - expect( - isPopulatedObject({ attribute1: 'value1', attribute2: 'value2' }, [ - 'attribute1', - 'attribute2', - ]) - ).toBe(true); - expect( - isPopulatedObject({ attribute1: 'value1', attribute2: 'value2' }, [ - 'attribute1', - 'otherAttribute', - ]) - ).toBe(false); - }); }); diff --git a/x-pack/plugins/transform/common/utils/object_utils.ts b/x-pack/plugins/transform/common/utils/object_utils.ts index d0f72db8489ce0..605af489143607 100644 --- a/x-pack/plugins/transform/common/utils/object_utils.ts +++ b/x-pack/plugins/transform/common/utils/object_utils.ts @@ -51,16 +51,3 @@ export const setNestedProperty = (obj: Record, accessor: string, va return obj; }; - -export const isPopulatedObject = >( - arg: unknown, - requiredAttributes: string[] = [] -): arg is T => { - return ( - typeof arg === 'object' && - arg !== null && - Object.keys(arg).length > 0 && - (requiredAttributes.length === 0 || - requiredAttributes.every((d) => ({}.hasOwnProperty.call(arg, d)))) - ); -}; diff --git a/x-pack/plugins/transform/public/app/common/pivot_aggs.ts b/x-pack/plugins/transform/public/app/common/pivot_aggs.ts index ce89f3433150ed..03e06d36f9319c 100644 --- a/x-pack/plugins/transform/public/app/common/pivot_aggs.ts +++ b/x-pack/plugins/transform/public/app/common/pivot_aggs.ts @@ -14,7 +14,7 @@ import type { Dictionary } from '../../../common/types/common'; import type { EsFieldName } from '../../../common/types/fields'; import type { PivotAgg, PivotSupportedAggs } from '../../../common/types/pivot_aggs'; import { PIVOT_SUPPORTED_AGGS } from '../../../common/types/pivot_aggs'; -import { isPopulatedObject } from '../../../common/utils/object_utils'; +import { isPopulatedObject } from '../../../common/shared_imports'; import { getAggFormConfig } from '../sections/create_transform/components/step_define/common/get_agg_form_config'; import { PivotAggsConfigFilter } from '../sections/create_transform/components/step_define/common/filter_agg/types'; diff --git a/x-pack/plugins/transform/public/app/common/pivot_group_by.ts b/x-pack/plugins/transform/public/app/common/pivot_group_by.ts index 8702cfb731fd6f..0ad059fd29950a 100644 --- a/x-pack/plugins/transform/public/app/common/pivot_group_by.ts +++ b/x-pack/plugins/transform/public/app/common/pivot_group_by.ts @@ -9,7 +9,7 @@ import { AggName } from '../../../common/types/aggregations'; import { Dictionary } from '../../../common/types/common'; import { EsFieldName } from '../../../common/types/fields'; import { GenericAgg } from '../../../common/types/pivot_group_by'; -import { isPopulatedObject } from '../../../common/utils/object_utils'; +import { isPopulatedObject } from '../../../common/shared_imports'; import { KBN_FIELD_TYPES } from '../../../../../../src/plugins/data/common'; import { PivotAggsConfigWithUiSupport } from './pivot_aggs'; diff --git a/x-pack/plugins/transform/public/app/common/request.ts b/x-pack/plugins/transform/public/app/common/request.ts index fcc58c1eaa4646..dbd78b43d7abef 100644 --- a/x-pack/plugins/transform/public/app/common/request.ts +++ b/x-pack/plugins/transform/public/app/common/request.ts @@ -17,7 +17,7 @@ import type { PutTransformsPivotRequestSchema, PutTransformsRequestSchema, } from '../../../common/api_schemas/transforms'; -import { isPopulatedObject } from '../../../common/utils/object_utils'; +import { isPopulatedObject } from '../../../common/shared_imports'; import { DateHistogramAgg, HistogramAgg, TermsAgg } from '../../../common/types/pivot_group_by'; import { isIndexPattern } from '../../../common/types/index_pattern'; diff --git a/x-pack/plugins/transform/public/app/lib/authorization/components/common.ts b/x-pack/plugins/transform/public/app/lib/authorization/components/common.ts index 4f2d7ef85524a0..5599e3f4232778 100644 --- a/x-pack/plugins/transform/public/app/lib/authorization/components/common.ts +++ b/x-pack/plugins/transform/public/app/lib/authorization/components/common.ts @@ -8,7 +8,7 @@ import { i18n } from '@kbn/i18n'; import { Privileges } from '../../../../../common/types/privileges'; -import { isPopulatedObject } from '../../../../../common/utils/object_utils'; +import { isPopulatedObject } from '../../../../../common/shared_imports'; export interface Capabilities { canGetTransform: boolean; diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/advanced_runtime_mappings_editor/advanced_runtime_mappings_editor.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/advanced_runtime_mappings_editor/advanced_runtime_mappings_editor.tsx index 1e8397a4d9cc34..1e6e6a971a81a6 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/advanced_runtime_mappings_editor/advanced_runtime_mappings_editor.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/advanced_runtime_mappings_editor/advanced_runtime_mappings_editor.tsx @@ -12,8 +12,9 @@ import { EuiCodeEditor } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; +import { isRuntimeMappings } from '../../../../../../common/shared_imports'; + import { StepDefineFormHook } from '../step_define'; -import { isRuntimeMappings } from '../step_define/common/types'; export const AdvancedRuntimeMappingsEditor: FC = memo( ({ diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.tsx index a7f2a3cd7178d5..36bdca79216227 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_create/step_create_form.tsx @@ -47,7 +47,7 @@ import { PutTransformsPivotRequestSchema, } from '../../../../../../common/api_schemas/transforms'; import type { RuntimeField } from '../../../../../../../../../src/plugins/data/common/index_patterns'; -import { isPopulatedObject } from '../../../../../../common/utils/object_utils'; +import { isPopulatedObject } from '../../../../../../common/shared_imports'; import { isLatestTransform } from '../../../../../../common/types/transform'; export interface StepDetailsExposedState { diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.tsx index 3b5d6e0e504973..9b349541a78a3e 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/filter_agg_form.tsx @@ -16,7 +16,7 @@ import { getFilterAggTypeConfig } from '../config'; import type { FilterAggType, PivotAggsConfigFilter } from '../types'; import type { RuntimeMappings } from '../../types'; import { getKibanaFieldTypeFromEsType } from '../../get_pivot_dropdown_options'; -import { isPopulatedObject } from '../../../../../../../../../common/utils/object_utils'; +import { isPopulatedObject } from '../../../../../../../../../common/shared_imports'; /** * Resolves supported filters for provided field. diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/get_pivot_dropdown_options.ts b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/get_pivot_dropdown_options.ts index 8d85988424e270..957439810adc72 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/get_pivot_dropdown_options.ts +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/get_pivot_dropdown_options.ts @@ -13,6 +13,7 @@ import { } from '../../../../../../../../../../src/plugins/data/public'; import { getNestedProperty } from '../../../../../../../common/utils/object_utils'; +import { isRuntimeMappings } from '../../../../../../../common/shared_imports'; import { DropDownLabel, @@ -26,7 +27,6 @@ import { import { getDefaultAggregationConfig } from './get_default_aggregation_config'; import { getDefaultGroupByConfig } from './get_default_group_by_config'; import type { Field, StepDefineExposedState } from './types'; -import { isRuntimeMappings } from './types'; const illegalEsAggNameChars = /[[\]>]/g; diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/types.ts b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/types.ts index 30bbc2d33cfad6..8b3b33fdde3efb 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/types.ts +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/types.ts @@ -24,7 +24,7 @@ import { } from '../../../../../../../common/types/transform'; import { LatestFunctionConfig } from '../../../../../../../common/api_schemas/transforms'; -import { isPopulatedObject } from '../../../../../../../common/utils/object_utils'; +import { isPopulatedObject } from '../../../../../../../common/shared_imports'; export interface ErrorMessage { query: string; @@ -72,23 +72,6 @@ export interface StepDefineExposedState { isRuntimeMappingsEditorEnabled: boolean; } -export function isRuntimeField(arg: unknown): arg is RuntimeField { - return ( - ((isPopulatedObject(arg, ['type']) && Object.keys(arg).length === 1) || - (isPopulatedObject(arg, ['type', 'script']) && - Object.keys(arg).length === 2 && - (typeof arg.script === 'string' || - (isPopulatedObject(arg.script, ['source']) && - Object.keys(arg.script).length === 1 && - typeof arg.script.source === 'string')))) && - RUNTIME_FIELD_TYPES.includes(arg.type as RuntimeType) - ); -} - -export function isRuntimeMappings(arg: unknown): arg is RuntimeMappings { - return isPopulatedObject(arg) && Object.values(arg).every((d) => isRuntimeField(d)); -} - export function isPivotPartialRequest(arg: unknown): arg is { pivot: PivotConfigDefinition } { return isPopulatedObject(arg, ['pivot']); } diff --git a/x-pack/plugins/transform/server/routes/api/transforms_nodes.ts b/x-pack/plugins/transform/server/routes/api/transforms_nodes.ts index bb54dc3f19d1db..c9a0795c322103 100644 --- a/x-pack/plugins/transform/server/routes/api/transforms_nodes.ts +++ b/x-pack/plugins/transform/server/routes/api/transforms_nodes.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { isPopulatedObject } from '../../../common/utils/object_utils'; +import { isPopulatedObject } from '../../../common/shared_imports'; import { RouteDependencies } from '../../types';