Preparation for move core_plugins\kibana\common\field_formats into data plugin (#46921) (#47040)

* Preparation for move core_plugins\kibana\common\field_formats into data plugin

Related to: #44973

* Fix PR Comments
This commit is contained in:
Alexey Antonov 2019-10-01 19:03:20 +03:00 committed by GitHub
parent 610661980b
commit 259655c460
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 58 deletions

View file

@ -17,11 +17,19 @@
* under the License.
*/
import { asPrettyString } from '../../../../../../plugins/data/common/field_formats';
import {
FieldFormat,
asPrettyString,
KBN_FIELD_TYPES,
} from '../../../../../../plugins/data/common';
export function createBoolFormat(FieldFormat) {
export function createBoolFormat() {
return class BoolFormat extends FieldFormat {
_convert(value) {
static id = 'boolean';
static title = 'Boolean';
static fieldType = [KBN_FIELD_TYPES.BOOLEAN, KBN_FIELD_TYPES.NUMBER, KBN_FIELD_TYPES.STRING];
_convert(value: any): string {
if (typeof value === 'string') {
value = value.trim().toLowerCase();
}
@ -41,9 +49,5 @@ export function createBoolFormat(FieldFormat) {
return asPrettyString(value);
}
}
static id = 'boolean';
static title = 'Boolean';
static fieldType = ['boolean', 'number', 'string'];
};
}

View file

@ -17,17 +17,17 @@
* under the License.
*/
import { escape, isFunction } from 'lodash';
import { FieldFormatConvert, IFieldFormat, HtmlConventTypeConvert } from '../types';
import { FieldFormatConvert, IFieldFormat, HtmlContextTypeConvert } from '../types';
import { asPrettyString, getHighlightHtml } from '../utils';
const CONTEXT_TYPE = 'html';
export const HTML_CONTEXT_TYPE = 'html';
const getConvertFn = (
format: IFieldFormat,
fieldFormatConvert: FieldFormatConvert
): HtmlConventTypeConvert => {
const fallbackHtml: HtmlConventTypeConvert = (value, field, hit) => {
fieldFormatConvert: Partial<FieldFormatConvert>
): HtmlContextTypeConvert => {
const fallbackHtml: HtmlContextTypeConvert = (value, field, hit) => {
const formatted = escape(format.convert(value, 'text'));
return !field || !hit || !hit.highlight || !hit.highlight[field.name]
@ -35,16 +35,16 @@ const getConvertFn = (
: getHighlightHtml(formatted, hit.highlight[field.name]);
};
return (fieldFormatConvert[CONTEXT_TYPE] || fallbackHtml) as HtmlConventTypeConvert;
return (fieldFormatConvert[HTML_CONTEXT_TYPE] || fallbackHtml) as HtmlContextTypeConvert;
};
export const setup = (
format: IFieldFormat,
fieldFormatConvert: FieldFormatConvert
): FieldFormatConvert => {
fieldFormatConvert: Partial<FieldFormatConvert>
): HtmlContextTypeConvert => {
const convert = getConvertFn(format, fieldFormatConvert);
const recurse: HtmlConventTypeConvert = (value, field, hit, meta) => {
const recurse: HtmlContextTypeConvert = (value, field, hit, meta) => {
if (value == null) {
return asPrettyString(value);
}
@ -63,11 +63,9 @@ export const setup = (
return subValues.join(',' + (useMultiLine ? '\n' : ' '));
};
const wrap: HtmlConventTypeConvert = (value, field, hit, meta) => {
const wrap: HtmlContextTypeConvert = (value, field, hit, meta) => {
return `<span ng-non-bindable>${recurse(value, field, hit, meta)}</span>`;
};
return {
[CONTEXT_TYPE]: wrap,
};
return wrap;
};

View file

@ -17,5 +17,5 @@
* under the License.
*/
export { setup as textContentTypeSetup } from './text_content_type';
export { setup as htmlContentTypeSetup } from './html_content_type';
export { setup as textContentTypeSetup, TEXT_CONTEXT_TYPE } from './text_content_type';
export { setup as htmlContentTypeSetup, HTML_CONTEXT_TYPE } from './html_content_type';

View file

@ -22,15 +22,15 @@ import { IFieldFormat, FieldFormatConvert, TextContextTypeConvert } from '../typ
import { asPrettyString } from '../utils';
const CONTEXT_TYPE = 'text';
export const TEXT_CONTEXT_TYPE = 'text';
const getConvertFn = (fieldFormatConvert: FieldFormatConvert): TextContextTypeConvert =>
(fieldFormatConvert[CONTEXT_TYPE] || asPrettyString) as TextContextTypeConvert;
const getConvertFn = (fieldFormatConvert: Partial<FieldFormatConvert>): TextContextTypeConvert =>
(fieldFormatConvert[TEXT_CONTEXT_TYPE] || asPrettyString) as TextContextTypeConvert;
export const setup = (
format: IFieldFormat,
fieldFormatConvert: FieldFormatConvert
): FieldFormatConvert => {
fieldFormatConvert: Partial<FieldFormatConvert>
): TextContextTypeConvert => {
const convert = getConvertFn(fieldFormatConvert);
const recurse: TextContextTypeConvert = value => {
@ -42,5 +42,5 @@ export const setup = (
return JSON.stringify(value.map(recurse));
};
return { [CONTEXT_TYPE]: recurse };
return recurse;
};

View file

@ -18,15 +18,13 @@
*/
import { FieldFormat } from '../field_format';
import { FieldFormatConvert } from '../types';
import { FieldFormatConvertFunction } from '../types';
const ID = 'custom';
export const createCustomFieldFormat = (convert: FieldFormatConvert) =>
export const createCustomFieldFormat = (convert: FieldFormatConvertFunction) =>
class CustomFieldFormat extends FieldFormat {
static id = ID;
public get _convert() {
return convert;
}
_convert = convert;
};

View file

@ -23,7 +23,7 @@ import { FieldFormatConvert } from './types';
import { asPrettyString } from './utils/as_pretty_string';
const getTestFormat = (
_convert: FieldFormatConvert = {
_convert: Partial<FieldFormatConvert> = {
text: (val: string) => asPrettyString(val),
},
_params?: any
@ -32,9 +32,7 @@ const getTestFormat = (
static id = 'test-format';
static title = 'Test Format';
public get _convert() {
return _convert;
}
_convert = _convert;
})(_params);
describe('FieldFormat class', () => {

View file

@ -21,10 +21,18 @@ import { isFunction, transform, size, cloneDeep, get, defaults } from 'lodash';
import { createCustomFieldFormat } from './converters/custom';
import { ContentType, FieldFormatConvert, FieldFormatConvertFunction } from './types';
import { htmlContentTypeSetup, textContentTypeSetup } from './content_types';
import {
htmlContentTypeSetup,
textContentTypeSetup,
TEXT_CONTEXT_TYPE,
HTML_CONTEXT_TYPE,
} from './content_types';
const DEFAULT_CONTEXT_TYPE = 'text';
export const isFieldFormatConvertFn = (convert: any): convert is FieldFormatConvertFunction =>
isFunction(convert);
export abstract class FieldFormat {
/**
* @property {string} - Field Format Id
@ -43,19 +51,19 @@ export abstract class FieldFormat {
* @property {string} - Field Format Type
* @private
*/
static fieldType: string;
static fieldType: string | string[];
/**
* @property {FieldFormatConvert}
* @private
*/
_convert: FieldFormatConvert = FieldFormat.setupContentType(this, get(this, '_convert', {}));
private convertObject: FieldFormatConvert | undefined;
/**
* @property {Function} - ref to child class
* @private
*/
type: any = this.constructor;
public type: any = this.constructor;
constructor(public _params: any = {}) {}
@ -88,11 +96,11 @@ export abstract class FieldFormat {
getConverterFor(
contentType: ContentType = DEFAULT_CONTEXT_TYPE
): FieldFormatConvertFunction | null {
if (this._convert) {
return this._convert[contentType];
if (!this.convertObject) {
this.convertObject = FieldFormat.setupContentType(this, get(this, '_convert'));
}
return null;
return this.convertObject[contentType] || null;
}
/**
@ -160,32 +168,31 @@ export abstract class FieldFormat {
}
static from(convertFn: FieldFormatConvertFunction) {
return createCustomFieldFormat(FieldFormat.toConvertObject(convertFn));
return createCustomFieldFormat(convertFn);
}
private static setupContentType(
fieldFormat: IFieldFormat,
convert: FieldFormatConvert | FieldFormatConvertFunction
convert: Partial<FieldFormatConvert> | FieldFormatConvertFunction = {}
): FieldFormatConvert {
const convertObject = FieldFormat.toConvertObject(convert);
const convertObject = isFieldFormatConvertFn(convert)
? FieldFormat.toConvertObject(convert)
: convert;
return {
...textContentTypeSetup(fieldFormat, convertObject),
...htmlContentTypeSetup(fieldFormat, convertObject),
[TEXT_CONTEXT_TYPE]: textContentTypeSetup(fieldFormat, convertObject),
[HTML_CONTEXT_TYPE]: htmlContentTypeSetup(fieldFormat, convertObject),
};
}
private static toConvertObject(
convert: FieldFormatConvert | FieldFormatConvertFunction
): FieldFormatConvert {
if (isFunction(convert)) {
private static toConvertObject(convert: FieldFormatConvertFunction): Partial<FieldFormatConvert> {
if (isFieldFormatConvertFn(convert)) {
return {
[DEFAULT_CONTEXT_TYPE]: convert,
[TEXT_CONTEXT_TYPE]: convert,
};
}
return convert;
}
}
export type FieldFormatConvert = { [key: string]: Function } | FieldFormatConvertFunction;
export type IFieldFormat = PublicMethodsOf<FieldFormat>;

View file

@ -24,7 +24,7 @@ export type ContentType = 'html' | 'text';
export { IFieldFormat } from './field_format';
/** @internal **/
export type HtmlConventTypeConvert = (
export type HtmlContextTypeConvert = (
value: any,
field?: any,
hit?: Record<string, any>,
@ -35,9 +35,10 @@ export type HtmlConventTypeConvert = (
export type TextContextTypeConvert = (value: any) => string;
/** @internal **/
export type FieldFormatConvertFunction = HtmlConventTypeConvert | TextContextTypeConvert;
export type FieldFormatConvertFunction = HtmlContextTypeConvert | TextContextTypeConvert;
/** @internal **/
export interface FieldFormatConvert {
[key: string]: FieldFormatConvertFunction;
text: TextContextTypeConvert;
html: HtmlContextTypeConvert;
}