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

[7.x] [Usage Collection] [schema] apm (#79000) #79355

Merged
merged 1 commit into from
Oct 2, 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
18 changes: 18 additions & 0 deletions packages/kbn-telemetry-tools/src/tools/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,22 @@ describe('getDescriptor', () => {
},
});
});

it('serializes RecordWithKnownProps', () => {
const usageInterface = usageInterfaces.get('RecordWithKnownProps')!;
const descriptor = getDescriptor(usageInterface, tsProgram);
expect(descriptor).toEqual({
prop1: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' },
prop2: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' },
});
});

it('serializes IndexedAccessType', () => {
const usageInterface = usageInterfaces.get('IndexedAccessType')!;
const descriptor = getDescriptor(usageInterface, tsProgram);
expect(descriptor).toEqual({
prop1: { kind: ts.SyntaxKind.StringKeyword, type: 'StringKeyword' },
prop2: { kind: ts.SyntaxKind.StringKeyword, type: 'StringKeyword' },
});
});
});
33 changes: 29 additions & 4 deletions packages/kbn-telemetry-tools/src/tools/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import * as ts from 'typescript';
import { uniqBy } from 'lodash';
import { uniqBy, pick } from 'lodash';
import {
getResolvedModuleSourceFile,
getIdentifierDeclarationFromSource,
Expand Down Expand Up @@ -95,7 +95,16 @@ export function getConstraints(node: ts.Node, program: ts.Program): any {
return node.literal.text;
}

throw Error(`Unsupported constraint`);
if (ts.isImportSpecifier(node)) {
const source = node.getSourceFile();
const importedModuleName = getModuleSpecifier(node);

const declarationSource = getResolvedModuleSourceFile(source, program, importedModuleName);
const declarationNode = getIdentifierDeclarationFromSource(node.name, declarationSource);
return getConstraints(declarationNode, program);
}

throw Error(`Unsupported constraint of kind ${node.kind} [${ts.SyntaxKind[node.kind]}]`);
}

export function getDescriptor(node: ts.Node, program: ts.Program): Descriptor | DescriptorValue {
Expand Down Expand Up @@ -157,9 +166,25 @@ export function getDescriptor(node: ts.Node, program: ts.Program): Descriptor |
return { kind: TelemetryKinds.Date, type: 'Date' };
}
// Support `Record<string, SOMETHING>`
if (symbolName === 'Record' && node.typeArguments![0].kind === ts.SyntaxKind.StringKeyword) {
return { '@@INDEX@@': getDescriptor(node.typeArguments![1], program) };
if (symbolName === 'Record') {
const descriptor = getDescriptor(node.typeArguments![1], program);
if (node.typeArguments![0].kind === ts.SyntaxKind.StringKeyword) {
return { '@@INDEX@@': descriptor };
}
const constraints = getConstraints(node.typeArguments![0], program);
const constraintsArray = Array.isArray(constraints) ? constraints : [constraints];
if (typeof constraintsArray[0] === 'string') {
return constraintsArray.reduce((acc, c) => ({ ...acc, [c]: descriptor }), {});
}
}

// Support `Pick<SOMETHING, 'prop1' | 'prop2'>`
if (symbolName === 'Pick') {
const parentDescriptor = getDescriptor(node.typeArguments![0], program);
const pickPropNames = getConstraints(node.typeArguments![1], program);
return pick(parentDescriptor, pickPropNames);
}

const declaration = (symbol?.getDeclarations() || [])[0];
if (declaration) {
return getDescriptor(declaration, program);
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-telemetry-tools/src/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ export function getIdentifierDeclarationFromSource(node: ts.Node, source: ts.Sou
const identifierName = node.getText();
const identifierDefinition: ts.Node = (source as any).locals.get(identifierName);
if (!identifierDefinition) {
throw new Error(`Unable to fine identifier in source ${identifierName}`);
throw new Error(`Unable to find identifier in source ${identifierName}`);
}
const declarations = (identifierDefinition as any).declarations as ts.Node[];

const latestDeclaration: ts.Node | false | undefined =
Array.isArray(declarations) && declarations[declarations.length - 1];
if (!latestDeclaration) {
throw new Error(`Unable to fine declaration for identifier ${identifierName}`);
throw new Error(`Unable to find declaration for identifier ${identifierName}`);
}

return latestDeclaration;
Expand Down
4 changes: 4 additions & 0 deletions src/fixtures/telemetry_collectors/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ export interface MappedTypes {
[key in 'prop3']: number;
};
}

export type RecordWithKnownProps = Record<MappedTypeProps, number>;

export type IndexedAccessType = Pick<WithUnion, 'prop1' | 'prop2'>;
4 changes: 1 addition & 3 deletions x-pack/.telemetryrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"output": "plugins/telemetry_collection_xpack/schema/xpack_plugins.json",
"root": "plugins/",
"exclude": [
"plugins/apm/server/lib/apm_telemetry/index.ts"
]
"exclude": []
}
Loading