Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FieldFormats] Cleanup: rename IFieldFormatType -> FieldFormatInstanceType #64193

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
baseFormattersPublic: (import("../../common").IFieldFormatType | typeof DateFormat)[]
baseFormattersPublic: (import("../../common").FieldFormatInstanceType | typeof DateFormat)[]
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
```typescript
setup(core: CoreSetup, { usageCollection }: DataPluginSetupDependencies): {
fieldFormats: {
register: (customFieldFormat: import("../common").IFieldFormatType) => number;
register: (customFieldFormat: import("../common").FieldFormatInstanceType) => number;
};
search: ISearchSetup;
};
Expand All @@ -26,7 +26,7 @@ setup(core: CoreSetup, { usageCollection }: DataPluginSetupDependencies): {

`{
fieldFormats: {
register: (customFieldFormat: import("../common").IFieldFormatType) => number;
register: (customFieldFormat: import("../common").FieldFormatInstanceType) => number;
};
search: ISearchSetup;
}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { IFieldFormatType } from '../types';
import { FieldFormatInstanceType } from '../types';

import {
BoolFormat,
Expand All @@ -36,7 +36,7 @@ import {
UrlFormat,
} from '../converters';

export const baseFormatters: IFieldFormatType[] = [
export const baseFormatters: FieldFormatInstanceType[] = [
BoolFormat,
BytesFormat,
ColorFormat,
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/common/field_formats/converters/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/

import { FieldFormat } from '../field_format';
import { TextContextTypeConvert, FIELD_FORMAT_IDS, IFieldFormatType } from '../types';
import { TextContextTypeConvert, FIELD_FORMAT_IDS, FieldFormatInstanceType } from '../types';

export const createCustomFieldFormat = (convert: TextContextTypeConvert): IFieldFormatType =>
export const createCustomFieldFormat = (convert: TextContextTypeConvert): FieldFormatInstanceType =>
class CustomFieldFormat extends FieldFormat {
static id = FIELD_FORMAT_IDS.CUSTOM;

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/common/field_formats/field_format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { createCustomFieldFormat } from './converters/custom';
import {
FieldFormatsGetConfigFn,
FieldFormatsContentType,
IFieldFormatType,
FieldFormatInstanceType,
FieldFormatConvert,
FieldFormatConvertFunction,
HtmlContextTypeOptions,
Expand Down Expand Up @@ -199,7 +199,7 @@ export abstract class FieldFormat {
};
}

static from(convertFn: FieldFormatConvertFunction): IFieldFormatType {
static from(convertFn: FieldFormatConvertFunction): FieldFormatInstanceType {
return createCustomFieldFormat(convertFn);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import { FieldFormatsRegistry } from './field_formats_registry';
import { BoolFormat, PercentFormat, StringFormat } from './converters';
import { FieldFormatsGetConfigFn, IFieldFormatType } from './types';
import { FieldFormatsGetConfigFn, FieldFormatInstanceType } from './types';
import { KBN_FIELD_TYPES } from '../../common';

const getValueOfPrivateField = (instance: any, field: string) => instance[field];
Expand Down Expand Up @@ -75,10 +75,10 @@ describe('FieldFormatsRegistry', () => {
test('should register field formats', () => {
fieldFormatsRegistry.register([StringFormat, BoolFormat]);

const registeredFieldFormatters: Map<string, IFieldFormatType> = getValueOfPrivateField(
fieldFormatsRegistry,
'fieldFormats'
);
const registeredFieldFormatters: Map<
string,
FieldFormatInstanceType
> = getValueOfPrivateField(fieldFormatsRegistry, 'fieldFormats');

expect(registeredFieldFormatters.size).toBe(2);

Expand Down
40 changes: 21 additions & 19 deletions src/plugins/data/common/field_formats/field_formats_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
FieldFormatsGetConfigFn,
FieldFormatConfig,
FIELD_FORMAT_IDS,
IFieldFormatType,
FieldFormatInstanceType,
FieldFormatId,
IFieldFormatMetaParams,
IFieldFormat,
Expand All @@ -35,7 +35,7 @@ import { SerializedFieldFormat } from '../../../expressions/common/types';
import { ES_FIELD_TYPES, KBN_FIELD_TYPES } from '../types';

export class FieldFormatsRegistry {
protected fieldFormats: Map<FieldFormatId, IFieldFormatType> = new Map();
protected fieldFormats: Map<FieldFormatId, FieldFormatInstanceType> = new Map();
protected defaultMap: Record<string, FieldFormatConfig> = {};
protected metaParamsOptions: Record<string, any> = {};
protected getConfig?: FieldFormatsGetConfigFn;
Expand All @@ -47,7 +47,7 @@ export class FieldFormatsRegistry {
init(
getConfig: FieldFormatsGetConfigFn,
metaParamsOptions: Record<string, any> = {},
defaultFieldConverters: IFieldFormatType[] = baseFormatters
defaultFieldConverters: FieldFormatInstanceType[] = baseFormatters
) {
const defaultTypeMap = getConfig('format:defaultTypeMap');
this.register(defaultFieldConverters);
Expand Down Expand Up @@ -79,23 +79,23 @@ export class FieldFormatsRegistry {
* Get a derived FieldFormat class by its id.
*
* @param {FieldFormatId} formatId - the format id
* @return {IFieldFormatType | undefined}
* @return {FieldFormatInstanceType | undefined}
*/
getType = (formatId: FieldFormatId): IFieldFormatType | undefined => {
getType = (formatId: FieldFormatId): FieldFormatInstanceType | undefined => {
const fieldFormat = this.fieldFormats.get(formatId);

if (fieldFormat) {
const decoratedFieldFormat: any = this.fieldFormatMetaParamsDecorator(fieldFormat);

if (decoratedFieldFormat) {
return decoratedFieldFormat as IFieldFormatType;
return decoratedFieldFormat as FieldFormatInstanceType;
}
}

return undefined;
};

getTypeWithoutMetaParams = (formatId: FieldFormatId): IFieldFormatType | undefined => {
getTypeWithoutMetaParams = (formatId: FieldFormatId): FieldFormatInstanceType | undefined => {
return this.fieldFormats.get(formatId);
};

Expand All @@ -106,12 +106,12 @@ export class FieldFormatsRegistry {
*
* @param {KBN_FIELD_TYPES} fieldType
* @param {ES_FIELD_TYPES[]} esTypes - Array of ES data types
* @return {IFieldFormatType | undefined}
* @return {FieldFormatInstanceType | undefined}
*/
getDefaultType = (
fieldType: KBN_FIELD_TYPES,
esTypes: ES_FIELD_TYPES[]
): IFieldFormatType | undefined => {
): FieldFormatInstanceType | undefined => {
const config = this.getDefaultConfig(fieldType, esTypes);

return this.getType(config.id);
Expand Down Expand Up @@ -206,14 +206,16 @@ export class FieldFormatsRegistry {
* Get filtered list of field formats by format type
*
* @param {KBN_FIELD_TYPES} fieldType
* @return {IFieldFormatType[]}
* @return {FieldFormatInstanceType[]}
*/
getByFieldType(fieldType: KBN_FIELD_TYPES): IFieldFormatType[] {
getByFieldType(fieldType: KBN_FIELD_TYPES): FieldFormatInstanceType[] {
return [...this.fieldFormats.values()]
.filter((format: IFieldFormatType) => format && format.fieldType.indexOf(fieldType) !== -1)
.filter(
(format: FieldFormatInstanceType) => format && format.fieldType.indexOf(fieldType) !== -1
)
.map(
(format: IFieldFormatType) =>
this.fieldFormatMetaParamsDecorator(format) as IFieldFormatType
(format: FieldFormatInstanceType) =>
this.fieldFormatMetaParamsDecorator(format) as FieldFormatInstanceType
);
}

Expand All @@ -238,20 +240,20 @@ export class FieldFormatsRegistry {
});
}

register(fieldFormats: IFieldFormatType[]) {
register(fieldFormats: FieldFormatInstanceType[]) {
fieldFormats.forEach(fieldFormat => this.fieldFormats.set(fieldFormat.id, fieldFormat));
}

/**
* FieldFormat decorator - provide a one way to add meta-params for all field formatters
*
* @private
* @param {IFieldFormatType} fieldFormat - field format type
* @return {IFieldFormatType | undefined}
* @param {FieldFormatInstanceType} fieldFormat - field format type
* @return {FieldFormatInstanceType | undefined}
*/
private fieldFormatMetaParamsDecorator = (
fieldFormat: IFieldFormatType
): IFieldFormatType | undefined => {
fieldFormat: FieldFormatInstanceType
): FieldFormatInstanceType | undefined => {
const getMetaParams = (customParams: Record<string, any>) => this.buildMetaParams(customParams);

if (fieldFormat) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/field_formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ export {
FieldFormatConfig,
FieldFormatId,
// Used in data plugin only
IFieldFormatType,
FieldFormatInstanceType,
IFieldFormat,
} from './types';
6 changes: 3 additions & 3 deletions src/plugins/data/common/field_formats/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/

import { FieldFormat } from './field_format';
export { FieldFormat };

/** @public **/
export type FieldFormatsContentType = 'html' | 'text';
Expand Down Expand Up @@ -82,10 +80,12 @@ export type IFieldFormat = PublicMethodsOf<FieldFormat>;
*/
export type FieldFormatId = FIELD_FORMAT_IDS | string;

export type IFieldFormatType = (new (
/** @internal **/
export type FieldFormatInstanceType = (new (
params?: any,
getConfig?: FieldFormatsGetConfigFn
) => FieldFormat) & {
// Static properties:
id: FieldFormatId;
title: string;
fieldType: string | string[];
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class AggTypeFilters {
// Warning: (ae-missing-release-tag) "baseFormattersPublic" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export const baseFormattersPublic: (import("../../common").IFieldFormatType | typeof DateFormat)[];
export const baseFormattersPublic: (import("../../common").FieldFormatInstanceType | typeof DateFormat)[];

// Warning: (ae-missing-release-tag) "BUCKET_TYPES" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
Expand Down
10 changes: 7 additions & 3 deletions src/plugins/data/server/field_formats/field_formats_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@
* under the License.
*/
import { has } from 'lodash';
import { FieldFormatsRegistry, IFieldFormatType, baseFormatters } from '../../common/field_formats';
import {
FieldFormatsRegistry,
FieldFormatInstanceType,
baseFormatters,
} from '../../common/field_formats';
import { IUiSettingsClient } from '../../../../core/server';
import { DateFormat } from './converters';

export class FieldFormatsService {
private readonly fieldFormatClasses: IFieldFormatType[] = [DateFormat, ...baseFormatters];
private readonly fieldFormatClasses: FieldFormatInstanceType[] = [DateFormat, ...baseFormatters];

public setup() {
return {
register: (customFieldFormat: IFieldFormatType) =>
register: (customFieldFormat: FieldFormatInstanceType) =>
this.fieldFormatClasses.push(customFieldFormat),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ export class Plugin implements Plugin_2<PluginSetup, PluginStart> {
// (undocumented)
setup(core: CoreSetup, { usageCollection }: DataPluginSetupDependencies): {
fieldFormats: {
register: (customFieldFormat: import("../common").IFieldFormatType) => number;
register: (customFieldFormat: import("../common").FieldFormatInstanceType) => number;
};
search: ISearchSetup;
};
Expand Down