Skip to content

Commit

Permalink
[FieldFormatters] Remove aggs format decorator from `fieldFormats.des…
Browse files Browse the repository at this point in the history
…erialize()` (elastic#106755)
  • Loading branch information
Dosant authored and kibanamachine committed Jul 28, 2021
1 parent d0c0fcd commit d140bd5
Show file tree
Hide file tree
Showing 31 changed files with 219 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import helpers from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import helpers from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
Filter helpers namespace:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import helpers from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import helpers from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import helpers from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import helpers from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [FieldFormat](./kibana-plugin-plugins-data-public.fieldformat.md) &gt; [hidden](./kibana-plugin-plugins-data-public.fieldformat.hidden.md)

## FieldFormat.hidden property

Hidden field formats can only be accessed directly by id, They won't appear in field format editor UI, But they can be accessed and used from code internally.

{<!-- -->boolean<!-- -->} - Is this a hidden field format

<b>Signature:</b>

```typescript
static hidden: boolean;
```
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export declare abstract class FieldFormat
| [convertObject](./kibana-plugin-plugins-data-public.fieldformat.convertobject.md) | | <code>FieldFormatConvert &#124; undefined</code> | {<!-- -->FieldFormatConvert<!-- -->} have to remove the private because of https://github.com/Microsoft/TypeScript/issues/17293 |
| [fieldType](./kibana-plugin-plugins-data-public.fieldformat.fieldtype.md) | <code>static</code> | <code>string &#124; string[]</code> | {<!-- -->string<!-- -->} - Field Format Type |
| [getConfig](./kibana-plugin-plugins-data-public.fieldformat.getconfig.md) | | <code>FieldFormatsGetConfigFn &#124; undefined</code> | |
| [hidden](./kibana-plugin-plugins-data-public.fieldformat.hidden.md) | <code>static</code> | <code>boolean</code> | Hidden field formats can only be accessed directly by id, They won't appear in field format editor UI, But they can be accessed and used from code internally. {<!-- -->boolean<!-- -->} - Is this a hidden field format |
| [htmlConvert](./kibana-plugin-plugins-data-public.fieldformat.htmlconvert.md) | | <code>HtmlContextTypeConvert &#124; undefined</code> | {<!-- -->htmlConvert<!-- -->} have to remove the protected because of https://github.com/Microsoft/TypeScript/issues/17293 |
| [id](./kibana-plugin-plugins-data-public.fieldformat.id.md) | <code>static</code> | <code>string</code> | {<!-- -->string<!-- -->} - Field Format Id |
| [textConvert](./kibana-plugin-plugins-data-public.fieldformat.textconvert.md) | | <code>TextContextTypeConvert &#124; undefined</code> | {<!-- -->textConvert<!-- -->} have to remove the protected because of https://github.com/Microsoft/TypeScript/issues/17293 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> Warning: This API is now obsolete.
>
> Please import from the package kbn/es-query directly. This import will be deprecated in v8.0.0.
> Please import from the package kbn/es-query directly. This import will be removed in v8.0.0.
>
<b>Signature:</b>
Expand Down
12 changes: 12 additions & 0 deletions src/plugins/data/common/field_formats/field_format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ export abstract class FieldFormat {
* @public
*/
static id: string;

/**
* Hidden field formats can only be accessed directly by id,
* They won't appear in field format editor UI,
* But they can be accessed and used from code internally.
*
* @property {boolean} - Is this a hidden field format
* @static
* @public
*/
static hidden: boolean;

/**
* @property {string} - Field Format Title
* @static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class FieldFormatsRegistry {
protected defaultMap: Record<string, FieldFormatConfig> = {};
protected metaParamsOptions: Record<string, any> = {};
protected getConfig?: FieldFormatsGetConfigFn;
// overriden on the public contract

public deserialize: FormatFactory = (mapping?: SerializedFieldFormat) => {
if (!mapping) {
return new (FieldFormat.from(identity))();
Expand Down Expand Up @@ -215,15 +215,17 @@ export class FieldFormatsRegistry {
}

/**
* Get filtered list of field formats by format type
* Get filtered list of field formats by format type,
* Skips hidden field formats
*
* @param {KBN_FIELD_TYPES} fieldType
* @return {FieldFormatInstanceType[]}
*/
getByFieldType(fieldType: KBN_FIELD_TYPES): FieldFormatInstanceType[] {
return [...this.fieldFormats.values()]
.filter(
(format: FieldFormatInstanceType) => format && format.fieldType.indexOf(fieldType) !== -1
(format: FieldFormatInstanceType) =>
format && !format.hidden && format.fieldType.indexOf(fieldType) !== -1
)
.map(
(format: FieldFormatInstanceType) =>
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/field_formats/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export type FieldFormatInstanceType = (new (
// Static properties:
id: FieldFormatId;
title: string;
hidden?: boolean;
fieldType: string | string[];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@ import { identity } from 'lodash';

import { SerializedFieldFormat } from 'src/plugins/expressions/common/types';
import { FieldFormat, IFieldFormat } from '../../../../common';
import { getAggsFormats } from './get_aggs_formats';

import { getFormatWithAggs } from './get_format_with_aggs';
const getAggFormat = (
mapping: SerializedFieldFormat,
getFormat: (mapping: SerializedFieldFormat) => IFieldFormat
) => {
const aggsFormats = getAggsFormats(getFormat);
const AggFormat = aggsFormats.find((format) => format.id === mapping.id);
if (!AggFormat) throw new Error(`No agg format with id: ${mapping.id}`);

describe('getFormatWithAggs', () => {
return new AggFormat(mapping.params);
};

describe('getAggsFormats', () => {
let getFormat: jest.MockedFunction<(mapping: SerializedFieldFormat) => IFieldFormat>;

beforeEach(() => {
Expand All @@ -23,19 +33,9 @@ describe('getFormatWithAggs', () => {
});
});

test('calls provided getFormat if no matching aggs exist', () => {
const mapping = { id: 'foo', params: {} };
const getFieldFormat = getFormatWithAggs(getFormat);
getFieldFormat(mapping);

expect(getFormat).toHaveBeenCalledTimes(1);
expect(getFormat).toHaveBeenCalledWith(mapping);
});

test('creates custom format for date_range', () => {
const mapping = { id: 'date_range', params: {} };
const getFieldFormat = getFormatWithAggs(getFormat);
const format = getFieldFormat(mapping);
const format = getAggFormat(mapping, getFormat);

expect(format.convert({ from: '2020-05-01', to: '2020-06-01' })).toBe(
'2020-05-01 to 2020-06-01'
Expand All @@ -47,8 +47,7 @@ describe('getFormatWithAggs', () => {

test('creates custom format for ip_range', () => {
const mapping = { id: 'ip_range', params: {} };
const getFieldFormat = getFormatWithAggs(getFormat);
const format = getFieldFormat(mapping);
const format = getAggFormat(mapping, getFormat);

expect(format.convert({ type: 'range', from: '10.0.0.1', to: '10.0.0.10' })).toBe(
'10.0.0.1 to 10.0.0.10'
Expand All @@ -61,44 +60,40 @@ describe('getFormatWithAggs', () => {

test('creates custom format for range', () => {
const mapping = { id: 'range', params: {} };
const getFieldFormat = getFormatWithAggs(getFormat);
const format = getFieldFormat(mapping);
const format = getAggFormat(mapping, getFormat);

expect(format.convert({ gte: 1, lt: 20 })).toBe('≥ 1 and < 20');
expect(getFormat).toHaveBeenCalledTimes(1);
});

test('creates alternative format for range using the template parameter', () => {
const mapping = { id: 'range', params: { template: 'arrow_right' } };
const getFieldFormat = getFormatWithAggs(getFormat);
const format = getFieldFormat(mapping);
const format = getAggFormat(mapping, getFormat);

expect(format.convert({ gte: 1, lt: 20 })).toBe('1 → 20');
expect(getFormat).toHaveBeenCalledTimes(1);
});

test('handles Infinity values internally when no nestedFormatter is passed', () => {
const mapping = { id: 'range', params: { replaceInfinity: true } };
const getFieldFormat = getFormatWithAggs(getFormat);
const format = getFieldFormat(mapping);
const format = getAggFormat(mapping, getFormat);

expect(format.convert({ gte: -Infinity, lt: Infinity })).toBe('≥ −∞ and < +∞');
expect(getFormat).toHaveBeenCalledTimes(1);
});

test('lets Infinity values handling to nestedFormatter even when flag is on', () => {
const mapping = { id: 'range', params: { replaceInfinity: true, id: 'any' } };
const getFieldFormat = getFormatWithAggs(getFormat);
const format = getFieldFormat(mapping);
const format = getAggFormat(mapping, getFormat);

expect(format.convert({ gte: -Infinity, lt: Infinity })).toBe('≥ -Infinity and < Infinity');
expect(getFormat).toHaveBeenCalledTimes(1);
});

test('returns custom label for range if provided', () => {
const mapping = { id: 'range', params: {} };
const getFieldFormat = getFormatWithAggs(getFormat);
const format = getFieldFormat(mapping);

const format = getAggFormat(mapping, getFormat);

expect(format.convert({ gte: 1, lt: 20, label: 'custom' })).toBe('custom');
// underlying formatter is not called because custom label can be used directly
Expand All @@ -113,8 +108,8 @@ describe('getFormatWithAggs', () => {
missingBucketLabel: 'missing bucket',
},
};
const getFieldFormat = getFormatWithAggs(getFormat);
const format = getFieldFormat(mapping);

const format = getAggFormat(mapping, getFormat);

expect(format.convert('machine.os.keyword')).toBe('machine.os.keyword');
expect(format.convert('__other__')).toBe(mapping.params.otherBucketLabel);
Expand Down
Loading

0 comments on commit d140bd5

Please sign in to comment.