Skip to content

Commit

Permalink
[Security Solutions] Removes deprecated types in kbn-securitysolution…
Browse files Browse the repository at this point in the history
…-* for newer kbn-es-query types (elastic#106801)

## Summary

Fixes elastic#105731, by replacing these `any` types:

```json
type IFieldType = any;
type IIndexPattern = any;
type Filter = any;
```

With the types from `es-query` which are:
* IndexPatternFieldBase
* IndexPatternBase
* Filter

Note: I had to do a few creative casting to avoid having to use `FieldSpec` since that is not within the package `es-query` and is not planned to be within that package or another package for at least a while if ever.

### Checklist

- [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
  • Loading branch information
FrankHassanabad authored and vadimkibana committed Aug 8, 2021
1 parent 40bafcc commit 6e319c8
Show file tree
Hide file tree
Showing 39 changed files with 399 additions and 399 deletions.
1 change: 1 addition & 0 deletions packages/kbn-securitysolution-autocomplete/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ SRC_DEPS = [
"//packages/kbn-i18n",
"//packages/kbn-securitysolution-io-ts-list-types",
"//packages/kbn-securitysolution-list-hooks",
"//packages/kbn-es-query",
"@npm//@babel/core",
"@npm//babel-loader",
"@npm//@elastic/eui",
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-securitysolution-autocomplete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This hook uses the kibana `services.data.autocomplete.getValueSuggestions()` ser

This component can be used to display available indexPattern fields. It requires an indexPattern to be passed in and will show an error state if value is not one of the available indexPattern fields. Users will be able to select only one option.

The `onChange` handler is passed `IFieldType[]`.
The `onChange` handler is passed `IndexPatternFieldBase[]`.

```js
<FieldComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
* Side Public License, v 1.
*/

import { IndexPatternFieldBase } from '@kbn/es-query';
import * as i18n from '../translations';

// TODO: I have to use any here for now, but once this is available below, we should use the correct types, https://github.com/elastic/kibana/issues/105731
// import { IFieldType } from '../../../../../../../src/plugins/data/common';
type IFieldType = any;

/**
* Determines if empty value is ok
*/
export const checkEmptyValue = (
param: string | undefined,
field: IFieldType | undefined,
field: IndexPatternFieldBase | undefined,
isRequired: boolean,
touched: boolean
): string | undefined | null => {
Expand Down
38 changes: 18 additions & 20 deletions packages/kbn-securitysolution-autocomplete/src/field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

import React, { useCallback, useMemo, useState } from 'react';
import { EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui';

// TODO: I have to use any here for now, but once this is available below, we should use the correct types, https://github.com/elastic/kibana/issues/105731
// import { IFieldType, IIndexPattern } from '../../../../../../../../src/plugins/data/common';
type IFieldType = any;
type IIndexPattern = any;
import { IndexPatternBase, IndexPatternFieldBase } from '@kbn/es-query';

import {
getGenericComboBoxProps,
Expand All @@ -24,14 +20,14 @@ const AS_PLAIN_TEXT = { asPlainText: true };
interface OperatorProps {
fieldInputWidth?: number;
fieldTypeFilter?: string[];
indexPattern: IIndexPattern | undefined;
indexPattern: IndexPatternBase | undefined;
isClearable: boolean;
isDisabled: boolean;
isLoading: boolean;
isRequired?: boolean;
onChange: (a: IFieldType[]) => void;
onChange: (a: IndexPatternFieldBase[]) => void;
placeholder: string;
selectedField: IFieldType | undefined;
selectedField: IndexPatternFieldBase | undefined;
}

export const FieldComponent: React.FC<OperatorProps> = ({
Expand Down Expand Up @@ -60,7 +56,7 @@ export const FieldComponent: React.FC<OperatorProps> = ({

const handleValuesChange = useCallback(
(newOptions: EuiComboBoxOptionOption[]): void => {
const newValues: IFieldType[] = newOptions.map(
const newValues: IndexPatternFieldBase[] = newOptions.map(
({ label }) => availableFields[labels.indexOf(label)]
);
onChange(newValues);
Expand Down Expand Up @@ -98,13 +94,13 @@ export const FieldComponent: React.FC<OperatorProps> = ({
FieldComponent.displayName = 'Field';

interface ComboBoxFields {
availableFields: IFieldType[];
selectedFields: IFieldType[];
availableFields: IndexPatternFieldBase[];
selectedFields: IndexPatternFieldBase[];
}

const getComboBoxFields = (
indexPattern: IIndexPattern | undefined,
selectedField: IFieldType | undefined,
indexPattern: IndexPatternBase | undefined,
selectedField: IndexPatternFieldBase | undefined,
fieldTypeFilter: string[]
): ComboBoxFields => {
const existingFields = getExistingFields(indexPattern);
Expand All @@ -117,27 +113,29 @@ const getComboBoxFields = (
const getComboBoxProps = (fields: ComboBoxFields): GetGenericComboBoxPropsReturn => {
const { availableFields, selectedFields } = fields;

return getGenericComboBoxProps<IFieldType>({
return getGenericComboBoxProps<IndexPatternFieldBase>({
getLabel: (field) => field.name,
options: availableFields,
selectedOptions: selectedFields,
});
};

const getExistingFields = (indexPattern: IIndexPattern | undefined): IFieldType[] => {
const getExistingFields = (indexPattern: IndexPatternBase | undefined): IndexPatternFieldBase[] => {
return indexPattern != null ? indexPattern.fields : [];
};

const getSelectedFields = (selectedField: IFieldType | undefined): IFieldType[] => {
const getSelectedFields = (
selectedField: IndexPatternFieldBase | undefined
): IndexPatternFieldBase[] => {
return selectedField ? [selectedField] : [];
};

const getAvailableFields = (
existingFields: IFieldType[],
selectedFields: IFieldType[],
existingFields: IndexPatternFieldBase[],
selectedFields: IndexPatternFieldBase[],
fieldTypeFilter: string[]
): IFieldType[] => {
const fieldsByName = new Map<string, IFieldType>();
): IndexPatternFieldBase[] => {
const fieldsByName = new Map<string, IndexPatternFieldBase>();

existingFields.forEach((f) => fieldsByName.set(f.name, f));
selectedFields.forEach((f) => fieldsByName.set(f.name, f));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { EuiComboBox, EuiComboBoxOptionOption, EuiFormRow } from '@elastic/eui';
import type { ListSchema } from '@kbn/securitysolution-io-ts-list-types';
import { useFindLists } from '@kbn/securitysolution-list-hooks';
import { IndexPatternFieldBase } from '@kbn/es-query';

import { filterFieldToList } from '../filter_field_to_list';
import { getGenericComboBoxProps } from '../get_generic_combo_box_props';

// TODO: I have to use any here for now, but once this is available below, we should use the correct types, https://github.com/elastic/kibana/issues/105731
// import { IFieldType } from '../../../../../../../src/plugins/data/common';
type IFieldType = any;

// TODO: I have to use any here for now, but once this is available below, we should use the correct types, https://github.com/elastic/kibana/issues/100715
// import { HttpStart } from 'kibana/public';
type HttpStart = any;
Expand All @@ -34,7 +31,7 @@ interface AutocompleteFieldListsProps {
onChange: (arg: ListSchema) => void;
placeholder: string;
rowLabel?: string;
selectedField: IFieldType | undefined;
selectedField: IndexPatternFieldBase | undefined;
selectedValue: string | undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('AutocompleteFieldMatchComponent', () => {
placeholder="Placeholder text"
rowLabel={'Row Label'}
selectedField={getField('ip')}
selectedValue="126.45.211.34"
selectedValue="127.0.0.1"
/>
);

Expand All @@ -79,7 +79,7 @@ describe('AutocompleteFieldMatchComponent', () => {
onError={jest.fn()}
placeholder="Placeholder text"
selectedField={getField('ip')}
selectedValue="126.45.211.34"
selectedValue="127.0.0.1"
/>
);

Expand All @@ -104,7 +104,7 @@ describe('AutocompleteFieldMatchComponent', () => {
onError={jest.fn()}
placeholder="Placeholder text"
selectedField={getField('ip')}
selectedValue="126.45.211.34"
selectedValue="127.0.0.1"
/>
);
wrapper.find('[data-test-subj="valuesAutocompleteMatch"] button').at(0).simulate('click');
Expand All @@ -131,7 +131,7 @@ describe('AutocompleteFieldMatchComponent', () => {
onError={jest.fn()}
placeholder="Placeholder text"
selectedField={getField('ip')}
selectedValue="126.45.211.34"
selectedValue="127.0.0.1"
/>
);

Expand All @@ -158,13 +158,13 @@ describe('AutocompleteFieldMatchComponent', () => {
onError={jest.fn()}
placeholder="Placeholder text"
selectedField={getField('ip')}
selectedValue="126.45.211.34"
selectedValue="127.0.0.1"
/>
);

expect(
wrapper.find('[data-test-subj="valuesAutocompleteMatch"] EuiComboBoxPill').at(0).text()
).toEqual('126.45.211.34');
).toEqual('127.0.0.1');
});

test('it invokes "onChange" when new value created', async () => {
Expand All @@ -190,9 +190,9 @@ describe('AutocompleteFieldMatchComponent', () => {

((wrapper.find(EuiComboBox).props() as unknown) as {
onCreateOption: (a: string) => void;
}).onCreateOption('126.45.211.34');
}).onCreateOption('127.0.0.1');

expect(mockOnChange).toHaveBeenCalledWith('126.45.211.34');
expect(mockOnChange).toHaveBeenCalledWith('127.0.0.1');
});

test('it invokes "onChange" when new value selected', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
EuiComboBoxOptionOption,
EuiComboBox,
} from '@elastic/eui';
import { IndexPatternBase, IndexPatternFieldBase } from '@kbn/es-query';

import { uniq } from 'lodash';

import { ListOperatorTypeEnum as OperatorTypeEnum } from '@kbn/securitysolution-io-ts-list-types';
Expand All @@ -22,11 +24,6 @@ import { ListOperatorTypeEnum as OperatorTypeEnum } from '@kbn/securitysolution-
// import { AutocompleteStart } from '../../../../../../../src/plugins/data/public';
type AutocompleteStart = any;

// TODO: I have to use any here for now, but once this is available below, we should use the correct types, https://github.com/elastic/kibana/issues/105731
// import { IFieldType, IIndexPattern } from '../../../../../../../../src/plugins/data/common';
type IFieldType = any;
type IIndexPattern = any;

import * as i18n from '../translations';
import { useFieldValueAutocomplete } from '../hooks/use_field_value_autocomplete';
import {
Expand All @@ -44,9 +41,9 @@ const SINGLE_SELECTION = { asPlainText: true };

interface AutocompleteFieldMatchProps {
placeholder: string;
selectedField: IFieldType | undefined;
selectedField: IndexPatternFieldBase | undefined;
selectedValue: string | undefined;
indexPattern: IIndexPattern | undefined;
indexPattern: IndexPatternBase | undefined;
isLoading: boolean;
isDisabled: boolean;
isClearable: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('AutocompleteFieldMatchAnyComponent', () => {
placeholder="Placeholder text"
rowLabel={'Row Label'}
selectedField={getField('ip')}
selectedValue={['126.45.211.34']}
selectedValue={['127.0.0.1']}
/>
);

Expand Down Expand Up @@ -124,7 +124,7 @@ describe('AutocompleteFieldMatchAnyComponent', () => {
placeholder="Placeholder text"
rowLabel={'Row Label'}
selectedField={getField('ip')}
selectedValue={['126.45.211.34']}
selectedValue={['127.0.0.1']}
/>
);

Expand Down Expand Up @@ -155,13 +155,13 @@ describe('AutocompleteFieldMatchAnyComponent', () => {
placeholder="Placeholder text"
rowLabel={'Row Label'}
selectedField={getField('ip')}
selectedValue={['126.45.211.34']}
selectedValue={['127.0.0.1']}
/>
);

expect(
wrapper.find(`[data-test-subj="valuesAutocompleteMatchAny"] EuiComboBoxPill`).at(0).text()
).toEqual('126.45.211.34');
).toEqual('127.0.0.1');
});

test('it invokes "onChange" when new value created', async () => {
Expand Down Expand Up @@ -191,9 +191,9 @@ describe('AutocompleteFieldMatchAnyComponent', () => {

((wrapper.find(EuiComboBox).props() as unknown) as {
onCreateOption: (a: string) => void;
}).onCreateOption('126.45.211.34');
}).onCreateOption('127.0.0.1');

expect(mockOnChange).toHaveBeenCalledWith(['126.45.211.34']);
expect(mockOnChange).toHaveBeenCalledWith(['127.0.0.1']);
});

test('it invokes "onChange" when new value selected', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ import React, { useCallback, useMemo, useState } from 'react';
import { EuiComboBox, EuiComboBoxOptionOption, EuiFormRow } from '@elastic/eui';
import { uniq } from 'lodash';
import { ListOperatorTypeEnum as OperatorTypeEnum } from '@kbn/securitysolution-io-ts-list-types';
import { IndexPatternBase, IndexPatternFieldBase } from '@kbn/es-query';

// TODO: I have to use any here for now, but once this is available below, we should use the correct types, https://github.com/elastic/kibana/issues/100715
// import { AutocompleteStart } from '../../../../../../../src/plugins/data/public';
type AutocompleteStart = any;

// TODO: I have to use any here for now, but once this is available below, we should use the correct types, https://github.com/elastic/kibana/issues/105731
// import { IFieldType, IIndexPattern } from '../../../../../../../../src/plugins/data/common';
type IFieldType = any;
type IIndexPattern = any;

import * as i18n from '../translations';
import {
getGenericComboBoxProps,
Expand All @@ -30,9 +26,9 @@ import { paramIsValid } from '../param_is_valid';

interface AutocompleteFieldMatchAnyProps {
placeholder: string;
selectedField: IFieldType | undefined;
selectedField: IndexPatternFieldBase | undefined;
selectedValue: string[];
indexPattern: IIndexPattern | undefined;
indexPattern: IndexPatternBase | undefined;
isLoading: boolean;
isDisabled: boolean;
isClearable: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* Side Public License, v 1.
*/

// Copied from "src/plugins/data/common/index_patterns/fields/fields.mocks.ts"
// but without types.
import { IndexPatternFieldBase } from '@kbn/es-query';

// Copied from "src/plugins/data/common/index_patterns/fields/fields.mocks.ts" but with the types changed to "IndexPatternFieldBase" since that type is compatible.
// TODO: This should move out once those mocks are directly useable or in their own package, https://github.com/elastic/kibana/issues/100715

export const fields = [
export const fields: IndexPatternFieldBase[] = ([
{
name: 'bytes',
type: 'number',
Expand Down Expand Up @@ -308,6 +309,6 @@ export const fields = [
readFromDocValues: false,
subType: { nested: { path: 'nestedField.nestedChild' } },
},
];
] as unknown) as IndexPatternFieldBase[];

export const getField = (name: string) => fields.find((field) => field.name === name);

0 comments on commit 6e319c8

Please sign in to comment.