Skip to content

Commit e7f032b

Browse files
committed
chore(spec2cdk): refactor for mixins
1 parent 457aa99 commit e7f032b

File tree

17 files changed

+162
-75
lines changed

17 files changed

+162
-75
lines changed

packages/@aws-cdk/custom-resource-handlers/lib/custom-resources-framework/classes.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ interface ConstructorBuildProps {
5959
readonly optionalConstructorProps?: boolean;
6060

6161
/**
62-
* Visbility for the constructor.
62+
* Visibility for the constructor.
6363
*
64-
* @default MemberVisbility.Public
64+
* @default MemberVisibility.Public
6565
*/
66-
readonly constructorVisbility?: MemberVisibility;
66+
readonly constructorVisibility?: MemberVisibility;
6767

6868
/**
6969
* These statements are added to the constructor body in the order they appear in this property.
@@ -98,6 +98,13 @@ export interface HandlerFrameworkClassProps {
9898
* @default - the latest Lambda runtime available in the region.
9999
*/
100100
readonly runtime?: Runtime;
101+
102+
/**
103+
* Visibility for the constructor.
104+
*
105+
* @default MemberVisibility.Public
106+
*/
107+
readonly constructorVisibility?: MemberVisibility;
101108
}
102109

103110
interface BuildRuntimePropertyOptions {
@@ -134,13 +141,13 @@ export abstract class HandlerFrameworkClass extends ClassType {
134141
['runtime', this.buildRuntimeProperty(scope, { runtime: props.runtime })],
135142
]);
136143
const metadataStatements: Statement[] = [
137-
expr.directCode(`this.node.addMetadata('${CUSTOM_RESOURCE_RUNTIME_FAMILY}', this.runtime.family)`),
144+
stmt.directCode(`this.node.addMetadata('${CUSTOM_RESOURCE_RUNTIME_FAMILY}', this.runtime.family)`),
138145
];
139146
this.buildConstructor({
140147
constructorPropsType: LAMBDA_MODULE.FunctionOptions,
141148
superProps,
142149
optionalConstructorProps: true,
143-
constructorVisbility: MemberVisibility.Public,
150+
constructorVisibility: MemberVisibility.Public,
144151
statements: metadataStatements,
145152
});
146153
}
@@ -226,16 +233,16 @@ export abstract class HandlerFrameworkClass extends ClassType {
226233
['runtime', this.buildRuntimeProperty(scope, { runtime: props.runtime, isEvalNodejsProvider })],
227234
]);
228235
const metadataStatements: Statement[] = [
229-
expr.directCode(`this.addMetadata('${CUSTOM_RESOURCE_SINGLETON}', true)`),
230-
expr.directCode(`this.addMetadata('${CUSTOM_RESOURCE_RUNTIME_FAMILY}', this.runtime.family)`),
231-
expr.directCode(`if (props?.logGroup) { this.logGroup.node.addMetadata('${CUSTOM_RESOURCE_SINGLETON_LOG_GROUP}', true) }`),
236+
stmt.directCode(`this.addMetadata('${CUSTOM_RESOURCE_SINGLETON}', true)`),
237+
stmt.directCode(`this.addMetadata('${CUSTOM_RESOURCE_RUNTIME_FAMILY}', this.runtime.family)`),
238+
stmt.directCode(`if (props?.logGroup) { this.logGroup.node.addMetadata('${CUSTOM_RESOURCE_SINGLETON_LOG_GROUP}', true) }`),
232239
// We need to access the private `_logRetention` custom resource, the only public property - `logGroup` - provides an ARN reference to the resource, instead of the resource itself.
233-
expr.directCode(`if (props?.logRetention) { ((this as any).lambdaFunction as lambda.Function)._logRetention?.node.addMetadata('${CUSTOM_RESOURCE_SINGLETON_LOG_RETENTION}', true) }`),
240+
stmt.directCode(`if (props?.logRetention) { ((this as any).lambdaFunction as lambda.Function)._logRetention?.node.addMetadata('${CUSTOM_RESOURCE_SINGLETON_LOG_RETENTION}', true) }`),
234241
];
235242
this.buildConstructor({
236243
constructorPropsType: _interface.type,
237244
superProps,
238-
constructorVisbility: MemberVisibility.Public,
245+
constructorVisibility: MemberVisibility.Public,
239246
statements: metadataStatements,
240247
});
241248
}
@@ -337,11 +344,11 @@ export abstract class HandlerFrameworkClass extends ClassType {
337344
isCustomResourceProvider: true,
338345
})],
339346
]);
340-
const metadataStatements: Statement[] = [expr.directCode(`this.node.addMetadata('${CUSTOM_RESOURCE_PROVIDER}', true)`)];
347+
const metadataStatements: Statement[] = [stmt.directCode(`this.node.addMetadata('${CUSTOM_RESOURCE_PROVIDER}', true)`)];
341348
this.buildConstructor({
342349
constructorPropsType: CORE_MODULE.CustomResourceProviderOptions,
343350
superProps,
344-
constructorVisbility: MemberVisibility.Private,
351+
constructorVisibility: props.constructorVisibility ?? MemberVisibility.Private,
345352
optionalConstructorProps: true,
346353
statements: metadataStatements,
347354
});
@@ -370,7 +377,7 @@ export abstract class HandlerFrameworkClass extends ClassType {
370377

371378
private buildConstructor(props: ConstructorBuildProps) {
372379
const init = this.addInitializer({
373-
visibility: props.constructorVisbility,
380+
visibility: props.constructorVisibility,
374381
});
375382
const scope = init.addParameter({
376383
name: 'scope',

packages/@aws-cdk/custom-resource-handlers/lib/custom-resources-framework/config.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/* eslint-disable import/no-extraneous-dependencies */
21
import * as path from 'path';
2+
/* eslint-disable import/no-extraneous-dependencies */
3+
import { MemberVisibility } from '@cdklabs/typewriter';
34

45
/**
56
* Handler framework runtimes used for code generation.
@@ -97,6 +98,13 @@ export interface ComponentProps {
9798
* @default true
9899
*/
99100
readonly minifyAndBundle?: boolean;
101+
102+
/**
103+
* Visibility for the constructor.
104+
*
105+
* @default MemberVisibility.Public
106+
*/
107+
readonly constructorVisibility?: MemberVisibility;
100108
}
101109

102110
export type HandlerFrameworkConfig = { [module: string]: { [identifier: string]: ComponentProps[] } };
@@ -341,6 +349,7 @@ export const config: HandlerFrameworkConfig = {
341349
{
342350
type: ComponentType.CUSTOM_RESOURCE_PROVIDER,
343351
sourceCode: path.resolve(__dirname, '..', 'core', 'cross-region-ssm-writer-handler', 'index.ts'),
352+
constructorVisibility: MemberVisibility.Public,
344353
},
345354
],
346355
'cross-region-ssm-reader-provider': [

packages/@aws-cdk/custom-resource-handlers/lib/custom-resources-framework/framework.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class HandlerFrameworkModule extends Module {
5454
handler,
5555
codeDirectory,
5656
runtime: component.runtime,
57+
constructorVisibility: component.constructorVisibility,
5758
};
5859

5960
switch (component.type) {

packages/@aws-cdk/custom-resource-handlers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"@types/jest": "^29.5.14",
4949
"aws-sdk-client-mock": "4.1.0",
5050
"aws-sdk-client-mock-jest": "4.1.0",
51-
"@cdklabs/typewriter": "^0.0.6",
51+
"@cdklabs/typewriter": "^0.0.10",
5252
"jest": "^29.7.0",
5353
"sinon": "^9.2.4",
5454
"nock": "^13.5.6",

tools/@aws-cdk/cdk-build-tools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"glob": "^7.2.3",
6262
"jest": "^29.7.0",
6363
"jest-junit": "^13.2.0",
64-
"jsii": "~5.9.8",
64+
"jsii": "~5.9.13",
6565
"jsii-rosetta": "~5.9.9",
6666
"jsii-pacmak": "1.118.0",
6767
"jsii-reflect": "1.118.0",

tools/@aws-cdk/spec2cdk/lib/cdk/ast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export class AstBuilder {
212212
return ret;
213213
}
214214

215-
private createResourceModule(moduleName: string, serviceName: string, importLocations?: ModuleImportLocations) {
215+
protected createResourceModule(moduleName: string, serviceName: string, importLocations?: ModuleImportLocations) {
216216
const resourceModule = new Module(`@aws-cdk/${moduleName}/${serviceName}`);
217217
CDK_CORE.import(resourceModule, 'cdk', { fromLocation: importLocations?.core });
218218
CONSTRUCTS.import(resourceModule, 'constructs');

tools/@aws-cdk/spec2cdk/lib/cdk/relationship-decider.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Property, RelationshipRef, Resource, RichProperty, SpecDatabase } from '@aws-cdk/service-spec-types';
22
import { namespaceFromResource, referenceInterfaceName, referenceInterfaceAttributeName, referencePropertyName, typeAliasPrefixFromResource } from '../naming';
33
import { getReferenceProps } from './reference-props';
4-
import { createModuleDefinitionFromCfnNamespace } from '../cfn2ts/pkglint';
5-
import { log } from '../util';
4+
import { log, pkglint } from '../util';
65

76
// For now we want relationships to be applied only for these services
87
export const RELATIONSHIP_SERVICES: string[] = [];
@@ -55,7 +54,7 @@ export class RelationshipDecider {
5554
originalType: string;
5655
aliasedType: string;
5756
}) {
58-
const moduleName = createModuleDefinitionFromCfnNamespace(namespace).moduleName;
57+
const moduleName = pkglint.createModuleDefinitionFromCfnNamespace(namespace).moduleName;
5958
const moduleImport = this.imports.find(i => i.moduleName === moduleName);
6059
if (!moduleImport) {
6160
this.imports.push({

tools/@aws-cdk/spec2cdk/lib/cdk/resource-class.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
TruthyOr,
2525
Type,
2626
TypeDeclarationStatement,
27-
DocsSpec,
2827
} from '@cdklabs/typewriter';
2928
import { CDK_CORE, CONSTRUCTS } from './cdk';
3029
import { CloudFormationMapping } from './cloudformation-mapping';
@@ -39,7 +38,7 @@ import {
3938
staticRequiredTransform,
4039
staticResourceTypeName,
4140
} from '../naming';
42-
import { splitDocumentation } from '../util';
41+
import { isDefined, splitDocumentation, maybeDeprecated } from '../util';
4342
import { findArnProperty } from './reference-props';
4443
import { SelectiveImport, RelationshipDecider } from './relationship-decider';
4544

@@ -640,37 +639,6 @@ export class ResourceClass extends ClassType {
640639
}
641640
}
642641

643-
/**
644-
* Type guard to filter out undefined values.
645-
*/
646-
function isDefined<T>(x: T | undefined): x is T {
647-
return x !== undefined;
648-
}
649-
650-
/**
651-
* Compute stability taking into account deprecation status.
652-
*/
653-
function stability(isDeprecated: boolean = false, defaultStability: Stability = Stability.External): Stability {
654-
if (isDeprecated) {
655-
return Stability.Deprecated;
656-
}
657-
return defaultStability;
658-
}
659-
660-
/**
661-
* Returns deprecation props if deprecated.
662-
*/
663-
function maybeDeprecated(deprecationNotice?: string, defaultStability: Stability = Stability.External): Pick<DocsSpec, 'deprecated' | 'stability'> {
664-
if (deprecationNotice) {
665-
return {
666-
deprecated: deprecationNotice,
667-
stability: stability(Boolean(deprecationNotice), defaultStability),
668-
};
669-
}
670-
671-
return {};
672-
}
673-
674642
/**
675643
* Given a template like "arn:${Partition}:ec2:${Region}:${Account}:fleet/${FleetId}",
676644
* and a list of property names, like ["partition", "region", "account", "fleetId"],

tools/@aws-cdk/spec2cdk/lib/cdk/type-converter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @cdklabs/no-throw-default-error */
12
import {
23
SpecDatabase,
34
PropertyType,

tools/@aws-cdk/spec2cdk/lib/cdk/typedefinition-struct.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Resource, TypeDefinition } from '@aws-cdk/service-spec-types';
2-
import { ClassType, expr, FreeFunction, Module, Stability, stmt, StructType, Type } from '@cdklabs/typewriter';
2+
import { ClassType, expr, FreeFunction, Module, PropertySpec, Stability, stmt, StructType, Type } from '@cdklabs/typewriter';
33
import { CloudFormationMapping } from './cloudformation-mapping';
44
import { RelationshipDecider } from './relationship-decider';
55
import { TypeConverter } from './type-converter';
@@ -14,6 +14,18 @@ export interface TypeDefinitionStructOptions {
1414
readonly resource: Resource;
1515
readonly resourceClass: ClassType;
1616
readonly relationshipDecider: RelationshipDecider;
17+
/**
18+
* Add the cfn producer helpers.
19+
* Validator + ToCloudFormation
20+
* @default true
21+
*/
22+
readonly cfnProducer?: boolean;
23+
/**
24+
* Add the cfn parser helpers.
25+
* fromCloudFormation
26+
* @default true
27+
*/
28+
readonly cfnParser?: boolean;
1729
}
1830

1931
/**
@@ -27,6 +39,7 @@ export class TypeDefinitionStruct extends StructType {
2739
private readonly resource: Resource;
2840
private readonly module: Module;
2941
private readonly relationshipDecider: RelationshipDecider;
42+
private readonly options: TypeDefinitionStructOptions;
3043

3144
constructor(options: TypeDefinitionStructOptions) {
3245
super(options.resourceClass, {
@@ -46,6 +59,7 @@ export class TypeDefinitionStruct extends StructType {
4659
this.converter = options.converter;
4760
this.resource = options.resource;
4861
this.relationshipDecider = options.relationshipDecider;
62+
this.options = options;
4963

5064
this.module = Module.of(this);
5165
}
@@ -90,7 +104,26 @@ export class TypeDefinitionStruct extends StructType {
90104
);
91105
}
92106

93-
cfnMapping.makeCfnProducer(this.module, this);
94-
cfnMapping.makeCfnParser(this.module, this);
107+
if (this.options.cfnProducer ?? true) {
108+
cfnMapping.makeCfnProducer(this.module, this);
109+
}
110+
if (this.options.cfnParser ?? true) {
111+
cfnMapping.makeCfnParser(this.module, this);
112+
}
113+
}
114+
}
115+
116+
/**
117+
* Same as TypeDefinitionStruct, but all props are optional.
118+
*/
119+
export class PartialTypeDefinitionStruct extends TypeDefinitionStruct {
120+
/**
121+
* Change property spec to make every prop optional.
122+
*/
123+
public addProperty(prop: PropertySpec) {
124+
return super.addProperty({
125+
...prop,
126+
optional: true,
127+
});
95128
}
96129
}

0 commit comments

Comments
 (0)