Skip to content

Framework: Tokens can be converted to strings #518

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

Merged
merged 20 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Combine string literals while resolving fragments, so we never have n…
…on-intrinsics
  • Loading branch information
Rico Huijbers committed Aug 13, 2018
commit 4206b85da3627ba586e8d773edbda076f2c41bf7
5 changes: 2 additions & 3 deletions packages/@aws-cdk/cdk/lib/cloudformation/intrinsics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DEFAULT_ENGINE_NAME, ProvisioningEngine, StringFragment } from "../core/engine-strings";
import { ProvisioningEngine, StringFragment } from "../core/engine-strings";
import { IntrinsicToken } from "../core/tokens";

/**
Expand All @@ -24,5 +24,4 @@ const cloudFormationEngine = {
}
};

ProvisioningEngine.register(CLOUDFORMATION_ENGINE, cloudFormationEngine);
ProvisioningEngine.register(DEFAULT_ENGINE_NAME, cloudFormationEngine);
ProvisioningEngine.register(CLOUDFORMATION_ENGINE, cloudFormationEngine);
29 changes: 23 additions & 6 deletions packages/@aws-cdk/cdk/lib/core/engine-strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ export class ProvisioningEngine {
* Resolves the result.
*/
public static combineStringFragments(fragments: StringFragment[]): any {
simplifyFragments(fragments);

if (fragments.length === 0) { return ''; }
if (fragments.length === 1) { return fragments[0].value; }

const engines = Array.from(new Set<string>(fragments.filter(f => f.intrinsicEngine !== undefined).map(f => f.intrinsicEngine!)));
if (engines.length === 0) {
throw new Error('Should not happen; no intrinsics found in StringFragment list.');
}
if (engines.length > 1) {
throw new Error(`Combining different engines in one string fragment: ${engines.join(', ')}`);
}

const engine = engines.length > 0 ? engines[0] : DEFAULT_ENGINE_NAME;
const engine = engines[0];
if (!(engine in HANDLERS)) {
throw new Error(`No Token handler registered for engine: ${engine}`);
}
Expand Down Expand Up @@ -63,7 +68,7 @@ export interface StringFragment {
*/
export enum FragmentSource {
Literal = 'Literal',
Token = 'Token'
Intrinsic = 'Intrinsic'
}

/**
Expand All @@ -77,11 +82,23 @@ export interface IProvisioningEngine {
}

/**
* The engine that will be used if no Tokens are found
* Global handler map
*/
export const DEFAULT_ENGINE_NAME = 'default';
const HANDLERS: {[engine: string]: IProvisioningEngine} = {};

/**
* Global handler map
* Combine adjacent string literals in the fragment list
*
* List is modified in-place.
*/
const HANDLERS: {[engine: string]: IProvisioningEngine} = {};
function simplifyFragments(fragments: StringFragment[]) {
let i = 0;
while (i < fragments.length - 1) {
if (fragments[i].source === FragmentSource.Literal && fragments[i + 1].source === FragmentSource.Literal) {
fragments[i].value += fragments[i + 1].value;
fragments.splice(i + 1, 1);
} else {
i++;
}
}
}
8 changes: 6 additions & 2 deletions packages/@aws-cdk/cdk/lib/core/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { intrinsicEngine } from './engine-intrinsics';
import { intrinsicEngine, isIntrinsic } from './engine-intrinsics';
import { FragmentSource, StringFragment } from './engine-strings';
import { resolve } from './tokens';

Expand Down Expand Up @@ -101,7 +101,11 @@ export function resolveMarkerSpans(spans: MarkerSpan[], lookup: (id: string) =>
return { source: FragmentSource.Literal, value: span.value };
case 'marker':
const value = lookup(span.id);
return { source: FragmentSource.Token, value, intrinsicEngine: intrinsicEngine(value) };
if (isIntrinsic(value)) {
return { source: FragmentSource.Intrinsic, value, intrinsicEngine: intrinsicEngine(value) };
} else {
return { source: FragmentSource.Literal, value: `${value}` };
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/cdk/test/core/test.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export = {
const resolved = resolve(stringified);

// THEN
test.deepEqual(resolved, {'Fn::Join': ['', ['The dog says: ', 'woof woof']]});
test.deepEqual(evaluateCFN(resolved), 'The dog says: woof woof');
test.done();
},

Expand Down