Skip to content

Commit

Permalink
feat(core): new APIs for Aspects and Tags (#9558)
Browse files Browse the repository at this point in the history
In [CDK 2.0], the `applyAspect` API will be removed and instead will be accessible through a "trait" pattern:

    Aspects.of(scope).add(aspect)

Similarly, we are normalizing the tagging API to use the same pattern:

    Tags.of(scope).add(x, y)
    Tags.of(scope).remove(x)
    
The existing APIs are still supported but marked as @deprecated.

Related: aws/aws-cdk-rfcs#192, See [Design].

[CDK 2.0]: https://github.com/aws/aws-cdk-rfcs/blob/master/text/0192-remove-constructs-compat.md
[Design]: https://github.com/aws/aws-cdk-rfcs/blob/master/text/0192-remove-constructs-compat.md#02-aspects

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Elad Ben-Israel committed Aug 10, 2020
1 parent 79d4a75 commit a7a772f
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 457 deletions.
20 changes: 11 additions & 9 deletions packages/@aws-cdk/core/lib/aspect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { IConstruct } from 'constructs';

const ASPECTS_SYMBOL = Symbol('cdk-aspects');

/**
* Represents an Aspect
*/
Expand All @@ -10,8 +12,6 @@ export interface IAspect {
visit(node: IConstruct): void;
}

const ASPECTS_SYMBOL = Symbol('cdk-aspects');

/**
* Aspects can be applied to CDK tree scopes and can operate on the tree before
* synthesis.
Expand All @@ -25,8 +25,10 @@ export class Aspects {
public static of(scope: IConstruct): Aspects {
let aspects = (scope as any)[ASPECTS_SYMBOL];
if (!aspects) {
aspects = Object.defineProperty(scope, ASPECTS_SYMBOL, {
value: new Aspects(scope),
aspects = new Aspects(scope);

Object.defineProperty(scope, ASPECTS_SYMBOL, {
value: aspects,
configurable: false,
enumerable: false,
});
Expand All @@ -36,13 +38,13 @@ export class Aspects {

private readonly _aspects = new Array<IAspect>();

private constructor(_construct: IConstruct) { }
private constructor(private readonly scope: IConstruct) { }

/**
* Apply an aspect on this scope.
* @param aspect The aspect to apply.
* Adds an aspect to apply this scope before synthesis.
* @param aspect The aspect to add.
*/
public apply(aspect: IAspect) {
public add(aspect: IAspect) {
this._aspects.push(aspect);
}

Expand All @@ -52,4 +54,4 @@ export class Aspects {
public get aspects(): IAspect[] {
return [ ...this._aspects ];
}
}
}
Loading

0 comments on commit a7a772f

Please sign in to comment.