Skip to content

Commit

Permalink
[Mappings Editor] Support unknown types (#62149)
Browse files Browse the repository at this point in the history
* First iteration of supporting unknown types e2e

* Add missing files

* Fix types issues

* When creating a new field, we check if we actually know the type

If we do know the type, convert the new field to it and throw away
the customTypeJson.

* Fix i18n

* Updated naming to be more consistent

customType -> otherType

* Clean up of custom type in comments and validation feedback

* Codre review suggestions

* Add missing serializer

* Add Array validator to json

* Fix types issues

Do not use otherTypeName in call to getConfig rather wrap it
in it's own component also add some comments.

* Remove otherTypeJson from parameters

* Move fieldConfig to variable outside of the UseField

* Copy update

Change the instruction from "Manually specify" to something more
declarative. Also, manually may sound misleading (suggests there
is an automatic alternative).

Also change the JSON parameter label to something more accurate.

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Co-authored-by: Sébastien Loix <sabee77@gmail.com>
  • Loading branch information
3 people committed Apr 3, 2020
1 parent 4b05ac2 commit 020e573
Show file tree
Hide file tree
Showing 16 changed files with 257 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,6 @@
* under the License.
*/

/*
* 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 { ValidationFunc } from '../../hook_form_lib';
import { isJSON } from '../../../validators/string';
import { ERROR_CODE } from './types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export * from './max_shingle_size_parameter';

export * from './relations_parameter';

export * from './other_type_name_parameter';

export * from './other_type_json_parameter';

export const PARAMETER_SERIALIZERS = [relationsSerializer, dynamicSerializer];

export const PARAMETER_DESERIALIZERS = [relationsDeserializer, dynamicDeserializer];
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import { i18n } from '@kbn/i18n';

import {
UseField,
JsonEditorField,
ValidationFuncArg,
fieldValidators,
FieldConfig,
} from '../../../shared_imports';

const { isJsonField } = fieldValidators;

/**
* This is a special component that does not have an explicit entry in {@link PARAMETERS_DEFINITION}.
*
* We use it to store custom defined parameters in a field called "otherTypeJson".
*/

const fieldConfig: FieldConfig = {
label: i18n.translate('xpack.idxMgmt.mappingsEditor.otherTypeJsonFieldLabel', {
defaultMessage: 'Type Parameters JSON',
}),
defaultValue: {},
validations: [
{
validator: isJsonField(
i18n.translate(
'xpack.idxMgmt.mappingsEditor.parameters.validations.otherTypeJsonInvalidJSONErrorMessage',
{
defaultMessage: 'Invalid JSON.',
}
)
),
},
{
validator: ({ value }: ValidationFuncArg<any, any>) => {
const json = JSON.parse(value);
if (Array.isArray(json)) {
return {
message: i18n.translate(
'xpack.idxMgmt.mappingsEditor.parameters.validations.otherTypeJsonArrayNotAllowedErrorMessage',
{
defaultMessage: 'Arrays are not allowed.',
}
),
};
}
},
},
{
validator: ({ value }: ValidationFuncArg<any, any>) => {
const json = JSON.parse(value);
if (json.type) {
return {
code: 'ERR_CUSTOM_TYPE_OVERRIDDEN',
message: i18n.translate(
'xpack.idxMgmt.mappingsEditor.parameters.validations.otherTypeJsonTypeFieldErrorMessage',
{
defaultMessage: 'Cannot override the "type" field.',
}
),
};
}
},
},
],
deserializer: (value: any) => {
if (value === '') {
return value;
}
return JSON.stringify(value, null, 2);
},
serializer: (value: string) => {
try {
return JSON.parse(value);
} catch (error) {
// swallow error and return non-parsed value;
return value;
}
},
};

export const OtherTypeJsonParameter = () => (
<UseField path="otherTypeJson" config={fieldConfig} component={JsonEditorField} />
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';

import { i18n } from '@kbn/i18n';
import { UseField, TextField, FieldConfig } from '../../../shared_imports';
import { fieldValidators } from '../../../shared_imports';

const { emptyField } = fieldValidators;

/**
* This is a special component that does not have an explicit entry in {@link PARAMETERS_DEFINITION}.
*
* We use it to store the name of types unknown to the mappings editor in the "subType" path.
*/

const fieldConfig: FieldConfig = {
label: i18n.translate('xpack.idxMgmt.mappingsEditor.otherTypeNameFieldLabel', {
defaultMessage: 'Type Name',
}),
defaultValue: '',
validations: [
{
validator: emptyField(
i18n.translate(
'xpack.idxMgmt.mappingsEditor.parameters.validations.otherTypeNameIsRequiredErrorMessage',
{
defaultMessage: 'The type name is required.',
}
)
),
},
],
};

export const OtherTypeNameParameter = () => (
<UseField path="subType" config={fieldConfig} component={TextField} />
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import React, { useEffect, useCallback } from 'react';
import classNames from 'classnames';
import * as _ from 'lodash';

import { i18n } from '@kbn/i18n';

Expand All @@ -31,7 +32,7 @@ import {
filterTypesForNonRootFields,
} from '../../../../lib';
import { Field, MainType, SubType, NormalizedFields, ComboBoxOption } from '../../../../types';
import { NameParameter, TypeParameter } from '../../field_parameters';
import { NameParameter, TypeParameter, OtherTypeNameParameter } from '../../field_parameters';
import { getParametersFormForType } from './required_parameters_forms';

const formWrapper = (props: any) => <form {...props} />;
Expand Down Expand Up @@ -155,9 +156,9 @@ export const CreateField = React.memo(function CreateFieldComponent({
},
[form, getSubTypeMeta]
);

const renderFormFields = useCallback(
({ type }) => {
const isOtherType = type === 'other';
const { subTypeOptions, subTypeLabel } = getSubTypeMeta(type);

const docLink = documentationService.getTypeDocLink(type) as string;
Expand All @@ -178,7 +179,13 @@ export const CreateField = React.memo(function CreateFieldComponent({
docLink={docLink}
/>
</EuiFlexItem>
{/* Field sub type (if any) */}
{/* Other type */}
{isOtherType && (
<EuiFlexItem>
<OtherTypeNameParameter />
</EuiFlexItem>
)}
{/* Field sub type (if any) - will never be the case if we have an "other" type */}
{subTypeOptions && (
<EuiFlexItem>
<UseField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,29 @@ export const EditField = React.memo(({ form, field, allFields, exitEdit }: Props
</EuiFlexItem>

{/* Documentation link */}
<EuiFlexItem grow={false}>
<EuiButtonEmpty
size="s"
flush="right"
href={linkDocumentation}
target="_blank"
iconType="help"
>
{i18n.translate(
'xpack.idxMgmt.mappingsEditor.editField.typeDocumentation',
{
defaultMessage: '{type} documentation',
values: {
type: subTypeDefinition
? subTypeDefinition.label
: typeDefinition.label,
},
}
)}
</EuiButtonEmpty>
</EuiFlexItem>
{linkDocumentation && (
<EuiFlexItem grow={false}>
<EuiButtonEmpty
size="s"
flush="right"
href={linkDocumentation}
target="_blank"
iconType="help"
>
{i18n.translate(
'xpack.idxMgmt.mappingsEditor.editField.typeDocumentation',
{
defaultMessage: '{type} documentation',
values: {
type: subTypeDefinition
? subTypeDefinition.label
: typeDefinition.label,
},
}
)}
</EuiButtonEmpty>
</EuiFlexItem>
)}
</EuiFlexGroup>

{/* Field path */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '../../../../lib';
import { TYPE_DEFINITION } from '../../../../constants';

import { NameParameter, TypeParameter } from '../../field_parameters';
import { NameParameter, TypeParameter, OtherTypeNameParameter } from '../../field_parameters';
import { FieldDescriptionSection } from './field_description_section';

interface Props {
Expand Down Expand Up @@ -80,9 +80,17 @@ export const EditFieldHeaderForm = React.memo(
/>
</EuiFlexItem>

{/* Field sub type (if any) */}
{/* Other type */}
{type === 'other' && (
<EuiFlexItem>
<OtherTypeNameParameter />
</EuiFlexItem>
)}

{/* Field sub type (if any) - will never be the case if we have an "other" type */}
{hasSubType && (
<EuiFlexItem>
{' '}
<UseField
path="subType"
config={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { FlattenedType } from './flattened_type';
import { ShapeType } from './shape_type';
import { DenseVectorType } from './dense_vector_type';
import { ObjectType } from './object_type';
import { OtherType } from './other_type';
import { NestedType } from './nested_type';
import { JoinType } from './join_type';

Expand All @@ -48,6 +49,7 @@ const typeToParametersFormMap: { [key in DataType]?: ComponentType<any> } = {
shape: ShapeType,
dense_vector: DenseVectorType,
object: ObjectType,
other: OtherType,
nested: NestedType,
join: JoinType,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React from 'react';

import { OtherTypeJsonParameter } from '../../field_parameters';
import { BasicParametersSection } from '../edit_field';

export const OtherType = () => {
return (
<BasicParametersSection>
<OtherTypeJsonParameter />
</BasicParametersSection>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import {
import { i18n } from '@kbn/i18n';

import { NormalizedField, NormalizedFields } from '../../../types';
import { getTypeLabelFromType } from '../../../lib';
import {
TYPE_DEFINITION,
CHILD_FIELD_INDENT_SIZE,
LEFT_PADDING_SIZE_FIELD_ITEM_WRAPPER,
} from '../../../constants';

import { FieldsList } from './fields_list';
import { CreateField } from './create_field';
import { DeleteFieldProvider } from './delete_field_provider';
Expand Down Expand Up @@ -265,7 +267,7 @@ function FieldListItemComponent(
dataType: TYPE_DEFINITION[source.type].label,
},
})
: TYPE_DEFINITION[source.type].label}
: getTypeLabelFromType(source.type)}
</EuiBadge>
</EuiFlexItem>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { i18n } from '@kbn/i18n';
import { SearchResult } from '../../../types';
import { TYPE_DEFINITION } from '../../../constants';
import { useDispatch } from '../../../mappings_state';
import { getTypeLabelFromType } from '../../../lib';
import { DeleteFieldProvider } from '../fields/delete_field_provider';

interface Props {
Expand Down Expand Up @@ -115,7 +116,7 @@ export const SearchResultItem = React.memo(function FieldListItemFlatComponent({
dataType: TYPE_DEFINITION[source.type].label,
},
})
: TYPE_DEFINITION[source.type].label}
: getTypeLabelFromType(source.type)}
</EuiBadge>
</EuiFlexItem>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,20 @@ export const TYPE_DEFINITION: { [key in DataType]: DataTypeDefinition } = {
</p>
),
},
other: {
label: i18n.translate('xpack.idxMgmt.mappingsEditor.dataType.otherDescription', {
defaultMessage: 'Other',
}),
value: 'other',
description: () => (
<p>
<FormattedMessage
id="xpack.idxMgmt.mappingsEditor.dataType.otherLongDescription"
defaultMessage="Specify type parameters in JSON."
/>
</p>
),
},
};

export const MAIN_TYPES: MainType[] = [
Expand Down Expand Up @@ -811,6 +825,7 @@ export const MAIN_TYPES: MainType[] = [
'shape',
'text',
'token_count',
'other',
];

export const MAIN_DATA_TYPE_DEFINITION: {
Expand Down
Loading

0 comments on commit 020e573

Please sign in to comment.