Skip to content

Commit

Permalink
Merge branch '192606-generic-otel-agent-name' into 192606-move-source…
Browse files Browse the repository at this point in the history
…-to-fields
  • Loading branch information
miloszmarcinkowski committed Oct 8, 2024
2 parents ae4633c + f3f0ca8 commit a5db1e7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export interface MappedTypes {
mappedTypeWithOneInlineProp: {
[key in 'prop3']: number;
};
mappedTypeWithLiteralTemplates: {
[key in MappedTypeProps | `templated_prop/${string}`]: number;
};
}

export type RecordWithKnownProps = Record<MappedTypeProps, number>;
Expand Down
7 changes: 7 additions & 0 deletions packages/kbn-telemetry-tools/src/tools/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ describe('getDescriptor', () => {
mappedTypeWithOneInlineProp: {
prop3: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' },
},
mappedTypeWithLiteralTemplates: {
prop1: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' },
prop2: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' },
// ideally, it'd be `templated_prop/@@INDEX@@` to be more explicit. But we're going with the fuzzier approach
// for now as it may require more changes downstream that are not worth it.
'@@INDEX@@': { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' },
},
});
});

Expand Down
11 changes: 8 additions & 3 deletions packages/kbn-telemetry-tools/src/tools/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,15 @@ export function getConstraints(node: ts.Node, program: ts.Program): any {
return node.literal.text;
}

if (ts.isStringLiteral(node)) {
if (ts.isStringLiteral(node) || ts.isStringLiteralLike(node)) {
return node.text;
}

// template literals such as `smth/${string}`
if (ts.isTemplateLiteralTypeNode(node) || ts.isTemplateExpression(node)) {
return '@@INDEX@@'; // just map it to any kind of string. We can enforce it further in the future if we see fit.
}

if (ts.isImportSpecifier(node) || ts.isExportSpecifier(node)) {
const source = node.getSourceFile();
const importedModuleName = getModuleSpecifier(node);
Expand Down Expand Up @@ -180,9 +185,9 @@ export function getDescriptor(node: ts.Node, program: ts.Program): Descriptor |
const constraintsArray = Array.isArray(constraints) ? constraints : [constraints];
if (typeof constraintsArray[0] === 'string') {
return constraintsArray.reduce((acc, c) => {
(acc as Record<string, unknown>)[c] = descriptor;
acc[c] = descriptor;
return acc;
}, {});
}, {} as Record<string, unknown>);
}
}
return { '@@INDEX@@': descriptor };
Expand Down

0 comments on commit a5db1e7

Please sign in to comment.