-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(spec2cdk): generate from<Resource>Arn and from<Resource><Prop> in every L1
#35470
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bcc7b81
feat(spec2cdk): generate `from<Resource>Arn` in every L1
otaviomacedo fa02d2a
Test for L1 instead of L2
otaviomacedo 8e468d8
from<Resource><PropName>
otaviomacedo 1588cdd
Consume `arnTemplate` directly from the `Resource` type
otaviomacedo 92ee14b
Inner class inside the method
otaviomacedo 0bc7f6b
Merge branch 'main' into otaviom/from-arn-l1
otaviomacedo dbb025b
Update dependencies
otaviomacedo 0662605
Update snapshot
otaviomacedo bbb798e
Add missing scope-map.json and index.ts
otaviomacedo c6cc829
Merge branch 'main' into otaviom/from-arn-l1
otaviomacedo 8f6402b
Merge branch 'main' into otaviom/from-arn-l1
otaviomacedo 63a9a10
scope-map again
otaviomacedo ab76e11
- TemplateStringParser -> TemplateString
otaviomacedo eaaef87
Merge branch 'main' into otaviom/from-arn-l1
otaviomacedo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { UnscopedValidationError } from '../errors'; | ||
|
|
||
| /** | ||
| * A string with variables in the form `${name}`. | ||
| */ | ||
| export class TemplateString { | ||
| constructor(private readonly template: string) { | ||
| } | ||
|
|
||
| /** | ||
| * Parses a template string with variables in the form of `${var}` and extracts the values from the input string. | ||
| * Returns a record mapping variable names to their corresponding values. | ||
| * @param input the input string to parse | ||
| * @throws UnscopedValidationError if the input does not match the template | ||
| */ | ||
| public parse(input: string): Record<string, string> { | ||
| const templateParts = this.template.split(/(\$\{[^{}]+})/); | ||
| const result: Record<string, string> = {}; | ||
|
|
||
| let inputIndex = 0; | ||
|
|
||
| for (let i = 0; i < templateParts.length; i++) { | ||
| const part = templateParts[i]; | ||
| if (part.startsWith('${') && part.endsWith('}')) { | ||
| const varName = part.slice(2, -1); | ||
| const nextLiteral = templateParts[i + 1] || ''; | ||
|
|
||
| let value = ''; | ||
| if (nextLiteral) { | ||
| const endIndex = input.indexOf(nextLiteral, inputIndex); | ||
| if (endIndex === -1) { | ||
| throw new UnscopedValidationError(`Input ${input} does not match template ${this.template}`); | ||
| } | ||
| value = input.slice(inputIndex, endIndex); | ||
| inputIndex = endIndex; | ||
| } else { | ||
| value = input.slice(inputIndex); | ||
| inputIndex = input.length; | ||
| } | ||
|
|
||
| result[varName] = value; | ||
| } else { | ||
| if (input.slice(inputIndex, inputIndex + part.length) !== part) { | ||
| throw new UnscopedValidationError(`Input ${input} does not match template ${this.template}`); | ||
| } | ||
| inputIndex += part.length; | ||
| } | ||
| } | ||
|
|
||
| if (inputIndex !== input.length) { | ||
| throw new UnscopedValidationError(`Input ${input} does not match template ${this.template}`); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the template interpolated with the attributes of an object passed as input. | ||
| * Attributes that don't match any variable in the template are ignored, but all template | ||
| * variables must be replaced. | ||
| * @param variables an object where keys are the variable names, and values are the values to be replaced. | ||
| */ | ||
| public interpolate(variables: Record<string, string>): string { | ||
| return this.template.replace(/\${([^{}]+)}/g, (_, varName) => { | ||
| if (variables[varName] === undefined) { | ||
| throw new UnscopedValidationError(`Variable ${varName} not provided for template interpolation`); | ||
| } | ||
| return variables[varName]; | ||
| }); | ||
| } | ||
| } |
117 changes: 117 additions & 0 deletions
117
packages/aws-cdk-lib/core/test/helpers-internal/strings.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { UnscopedValidationError } from '../../lib'; | ||
| import { TemplateString } from '../../lib/helpers-internal'; | ||
|
|
||
| describe('new TemplateString', () => { | ||
| describe('parse', () => { | ||
| it('parses template with single variable correctly', () => { | ||
| const result = new TemplateString('Hello, ${name}!').parse('Hello, John!'); | ||
| expect(result).toEqual({ name: 'John' }); | ||
| }); | ||
|
|
||
| it('parses template with multiple variables correctly', () => { | ||
| const result = new TemplateString('My name is ${firstName} ${lastName}.').parse('My name is Jane Doe.'); | ||
| expect(result).toEqual({ firstName: 'Jane', lastName: 'Doe' }); | ||
| }); | ||
|
|
||
| it('throws error when input does not match template', () => { | ||
| expect(() => { | ||
| new TemplateString('Hello, ${name}!').parse('Hi, John!'); | ||
| }).toThrow(UnscopedValidationError); | ||
| }); | ||
|
|
||
| it('parses template with no variables correctly', () => { | ||
| const result = new TemplateString('Hello, world!').parse('Hello, world!'); | ||
| expect(result).toEqual({}); | ||
| }); | ||
|
|
||
| it('parses template with trailing variable correctly', () => { | ||
| const result = new TemplateString('Path: ${path}').parse('Path: /home/user'); | ||
| expect(result).toEqual({ path: '/home/user' }); | ||
| }); | ||
|
|
||
| it('throws error when input has extra characters', () => { | ||
| expect(() => { | ||
| new TemplateString('Hello, ${name}!').parse('Hello, John!!'); | ||
| }).toThrow(UnscopedValidationError); | ||
| }); | ||
|
|
||
| it('parses template with adjacent variables correctly', () => { | ||
| const result = new TemplateString('${greeting}, ${name}!').parse('Hi, John!'); | ||
| expect(result).toEqual({ greeting: 'Hi', name: 'John' }); | ||
| }); | ||
|
|
||
| it('throws error when input is shorter than template', () => { | ||
| expect(() => { | ||
| new TemplateString('Hello, ${name}!').parse('Hello, '); | ||
| }).toThrow(UnscopedValidationError); | ||
| }); | ||
|
|
||
| it('parses template with empty variable value correctly', () => { | ||
| const result = new TemplateString('Hello, ${name}!').parse('Hello, !'); | ||
| expect(result).toEqual({ name: '' }); | ||
| }); | ||
|
|
||
| it('parses template with variable at the start correctly', () => { | ||
| const result = new TemplateString('${greeting}, world!').parse('Hi, world!'); | ||
| expect(result).toEqual({ greeting: 'Hi' }); | ||
| }); | ||
|
|
||
| it('parses complex template correctly', () => { | ||
| const result = new TemplateString('arn:${Partition}:dynamodb:${Region}:${Account}:table/${TableName}') | ||
| .parse('arn:aws:dynamodb:us-east-1:12345:table/MyTable'); | ||
| expect(result).toEqual({ | ||
| Partition: 'aws', | ||
| Region: 'us-east-1', | ||
| Account: '12345', | ||
| TableName: 'MyTable', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('interpolate', () => { | ||
| it('interpolates template with single variable correctly', () => { | ||
| const result = new TemplateString('Hello, ${name}!').interpolate({ name: 'John' }); | ||
| expect(result).toBe('Hello, John!'); | ||
| }); | ||
|
|
||
| it('interpolates template with multiple variables correctly', () => { | ||
| const result = new TemplateString('My name is ${firstName} ${lastName}.').interpolate({ | ||
| firstName: 'Jane', | ||
| lastName: 'Doe', | ||
| }); | ||
| expect(result).toBe('My name is Jane Doe.'); | ||
| }); | ||
|
|
||
| it('throws error when variable is missing in interpolation', () => { | ||
| expect(() => { | ||
| new TemplateString('Hello, ${name}!').interpolate({}); | ||
| }).toThrow(UnscopedValidationError); | ||
| }); | ||
|
|
||
| it('interpolates template with no variables correctly', () => { | ||
| const result = new TemplateString('Hello, world!').interpolate({}); | ||
| expect(result).toBe('Hello, world!'); | ||
| }); | ||
|
|
||
| it('throws error when template contains undefined variable', () => { | ||
| expect(() => { | ||
| new TemplateString('Hello, ${name}!').interpolate({ greeting: 'Hi' }); | ||
| }).toThrow(UnscopedValidationError); | ||
| }); | ||
|
|
||
| it('interpolates template with adjacent variables correctly', () => { | ||
| const result = new TemplateString('${greeting}, ${name}!').interpolate({ greeting: 'Hi', name: 'John' }); | ||
| expect(result).toBe('Hi, John!'); | ||
| }); | ||
|
|
||
| it('interpolates template with empty variable value correctly', () => { | ||
| const result = new TemplateString('Hello, ${name}!').interpolate({ name: '' }); | ||
| expect(result).toBe('Hello, !'); | ||
| }); | ||
|
|
||
| it('interpolates template with variable at the start correctly', () => { | ||
| const result = new TemplateString('${greeting}, world!').interpolate({ greeting: 'Hi' }); | ||
| expect(result).toBe('Hi, world!'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choosing one representative to test that the generated methods actually return the right thing.