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] fix register on start contract #106828

Merged
merged 5 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -8,7 +8,7 @@
<b>Signature:</b>

```typescript
export declare type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' & 'register'> & {
export declare type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' | 'register'> & {
deserialize: FormatFactory;
};
```
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ describe('FieldFormatsRegistry', () => {
expect(registeredFieldFormatters.get(StringFormat.id)).toBe(StringFormat);
expect(registeredFieldFormatters.get(PercentFormat.id)).toBeUndefined();
});

test('should throw if registering a formatter with existing id ', () => {
fieldFormatsRegistry.register([BoolFormat]);

expect(() => fieldFormatsRegistry.register([BoolFormat])).toThrowErrorMatchingInlineSnapshot(
`"Failed to register field format with id \\"boolean\\" as it already has been registered"`
);
});
});

describe('has', () => {
test('should provide an public "has" method', () => {
expect(fieldFormatsRegistry.has).toBeDefined();
expect(typeof fieldFormatsRegistry.has).toBe('function');
});

test('should check if field format registered', () => {
fieldFormatsRegistry.register([StringFormat]);
expect(fieldFormatsRegistry.has(StringFormat.id)).toBe(true);
expect(fieldFormatsRegistry.has(BoolFormat.id)).toBe(false);
});
});

describe('getType', () => {
Expand Down
16 changes: 15 additions & 1 deletion src/plugins/data/common/field_formats/field_formats_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,21 @@ export class FieldFormatsRegistry {
}

register(fieldFormats: FieldFormatInstanceType[]) {
fieldFormats.forEach((fieldFormat) => this.fieldFormats.set(fieldFormat.id, fieldFormat));
fieldFormats.forEach((fieldFormat) => {
if (this.fieldFormats.has(fieldFormat.id))
throw new Error(
`Failed to register field format with id "${fieldFormat.id}" as it already has been registered`
);
this.fieldFormats.set(fieldFormat.id, fieldFormat);
});
}

/**
* Checks if field format with id already registered
* @param id
*/
has(id: string): boolean {
return this.fieldFormats.has(id);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/field_formats/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const fieldFormatsMock: IFieldFormatsRegistry = {
getTypeNameByEsTypes: jest.fn(),
init: jest.fn(),
register: jest.fn(),
has: jest.fn(),
parseDefaultTypeMap: jest.fn(),
deserialize: jest.fn().mockImplementation(() => {
const DefaultFieldFormat = FieldFormat.from(identity);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/field_formats/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ export interface IFieldFormatMetaParams {
};
}

export type FieldFormatsStartCommon = Omit<FieldFormatsRegistry, 'init' & 'register'>;
export type FieldFormatsStartCommon = Omit<FieldFormatsRegistry, 'init' | 'register'>;
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export class FieldFormatsService {
}

/** @public */
export type FieldFormatsSetup = Pick<FieldFormatsRegistry, 'register'>;
export type FieldFormatsSetup = Pick<FieldFormatsRegistry, 'register' | 'has'>;

/** @public */
export type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' & 'register'> & {
export type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' | 'register'> & {
deserialize: FormatFactory;
};
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 @@ -1069,7 +1069,7 @@ export type FieldFormatsContentType = 'html' | 'text';
export type FieldFormatsGetConfigFn = GetConfigFn;

// @public (undocumented)
export type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' & 'register'> & {
export type FieldFormatsStart = Omit<FieldFormatsRegistry, 'init' | 'register'> & {
deserialize: FormatFactory;
};

Expand Down
9 changes: 6 additions & 3 deletions x-pack/plugins/lens/public/indexpattern_datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class IndexPatternDatasource {

setup(
core: CoreSetup<IndexPatternDatasourceStartPlugins>,
{ expressions, editorFrame, charts }: IndexPatternDatasourceSetupPlugins
{ expressions, editorFrame, charts, data: dataSetup }: IndexPatternDatasourceSetupPlugins
) {
editorFrame.registerDatasource(async () => {
const {
Expand All @@ -48,8 +48,11 @@ export class IndexPatternDatasource {
} = await import('../async_services');
return core
.getStartServices()
.then(([coreStart, { data, indexPatternFieldEditor, uiActions }]) => {
data.fieldFormats.register([getSuffixFormatter(data.fieldFormats.deserialize)]);
.then(([coreStart, { indexPatternFieldEditor, uiActions, data }]) => {
const suffixFormatter = getSuffixFormatter(data.fieldFormats.deserialize);
if (!dataSetup.fieldFormats.has(suffixFormatter.id)) {
dataSetup.fieldFormats.register([suffixFormatter]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you are registering this to the upper data plugin rather than the inner one? Can you explain with a comment?

Copy link
Contributor Author

@Dosant Dosant Jul 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this pr, I fixed the type definition of field formats registry so that you don't have register() function on the start contract anymore. It was available prior to this pr because of an issue in type definitions. The only consumer who used register on a start contract was the lens.

Screen Shot 2021-07-28 at 13 12 04

I don't think this needs a code comment because there is no other option to call register() anymore.


Longer-term we plan to make all registries allow new items to be registered only in a setup contract and only during the setup lifecycle. It will likely be forbidden to call methods on the setup contract after the setup cycle has finished (just like the lens does now). This current long-long term plan. #106838 #106510 (comment)

}
expressions.registerFunction(timeScale);
expressions.registerFunction(counterRate);
expressions.registerFunction(renameColumns);
Expand Down