-
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 4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { UnscopedValidationError } from '../errors'; | ||
|
|
||
| /** | ||
| * Utility class for parsing template strings with variables. | ||
| */ | ||
| export class TemplateStringParser { | ||
otaviomacedo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /** | ||
| * 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 template the template string containing variables | ||
| * @param input the input string to parse | ||
| * @throws UnscopedValidationError if the input does not match the template | ||
| */ | ||
| public static parse(template: string, input: string): Record<string, string> { | ||
| const templateParts = 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 ${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 ${template}`); | ||
| } | ||
| inputIndex += part.length; | ||
| } | ||
| } | ||
|
|
||
| if (inputIndex !== input.length) { | ||
| throw new UnscopedValidationError(`Input ${input} does not match template ${template}`); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public static interpolate(template: string, variables: Record<string, string>): string { | ||
| return template.replace(/\${([^{}]+)}/g, (_, varName) => { | ||
| if (variables[varName] === undefined) { | ||
| throw new UnscopedValidationError(`Variable ${varName} not provided for template interpolation`); | ||
| } | ||
| return variables[varName]; | ||
| }); | ||
| } | ||
| } | ||
106 changes: 106 additions & 0 deletions
106
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,106 @@ | ||
| import { UnscopedValidationError } from '../../lib'; | ||
| import { TemplateStringParser } from '../../lib/helpers-internal/strings'; | ||
|
|
||
| describe('TemplateStringParser', () => { | ||
| describe('parse', () => { | ||
| it('parses template with single variable correctly', () => { | ||
| const result = TemplateStringParser.parse('Hello, ${name}!', 'Hello, John!'); | ||
| expect(result).toEqual({ name: 'John' }); | ||
| }); | ||
|
|
||
| it('parses template with multiple variables correctly', () => { | ||
| const result = TemplateStringParser.parse('My name is ${firstName} ${lastName}.', 'My name is Jane Doe.'); | ||
| expect(result).toEqual({ firstName: 'Jane', lastName: 'Doe' }); | ||
| }); | ||
|
|
||
| it('throws error when input does not match template', () => { | ||
| expect(() => { | ||
| TemplateStringParser.parse('Hello, ${name}!', 'Hi, John!'); | ||
| }).toThrow(UnscopedValidationError); | ||
| }); | ||
|
|
||
| it('parses template with no variables correctly', () => { | ||
| const result = TemplateStringParser.parse('Hello, world!', 'Hello, world!'); | ||
| expect(result).toEqual({}); | ||
| }); | ||
|
|
||
| it('parses template with trailing variable correctly', () => { | ||
| const result = TemplateStringParser.parse('Path: ${path}', 'Path: /home/user'); | ||
| expect(result).toEqual({ path: '/home/user' }); | ||
| }); | ||
|
|
||
| it('throws error when input has extra characters', () => { | ||
| expect(() => { | ||
| TemplateStringParser.parse('Hello, ${name}!', 'Hello, John!!'); | ||
| }).toThrow(UnscopedValidationError); | ||
| }); | ||
|
|
||
| it('parses template with adjacent variables correctly', () => { | ||
| const result = TemplateStringParser.parse('${greeting}, ${name}!', 'Hi, John!'); | ||
| expect(result).toEqual({ greeting: 'Hi', name: 'John' }); | ||
| }); | ||
|
|
||
| it('throws error when input is shorter than template', () => { | ||
| expect(() => { | ||
| TemplateStringParser.parse('Hello, ${name}!', 'Hello, '); | ||
| }).toThrow(UnscopedValidationError); | ||
| }); | ||
|
|
||
| it('parses template with empty variable value correctly', () => { | ||
| const result = TemplateStringParser.parse('Hello, ${name}!', 'Hello, !'); | ||
| expect(result).toEqual({ name: '' }); | ||
| }); | ||
|
|
||
| it('parses template with variable at the start correctly', () => { | ||
| const result = TemplateStringParser.parse('${greeting}, world!', 'Hi, world!'); | ||
| expect(result).toEqual({ greeting: 'Hi' }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('interpolate', () => { | ||
| it('interpolates template with single variable correctly', () => { | ||
| const result = TemplateStringParser.interpolate('Hello, ${name}!', { name: 'John' }); | ||
| expect(result).toBe('Hello, John!'); | ||
| }); | ||
|
|
||
| it('interpolates template with multiple variables correctly', () => { | ||
| const result = TemplateStringParser.interpolate('My name is ${firstName} ${lastName}.', { | ||
| firstName: 'Jane', | ||
| lastName: 'Doe', | ||
| }); | ||
| expect(result).toBe('My name is Jane Doe.'); | ||
| }); | ||
|
|
||
| it('throws error when variable is missing in interpolation', () => { | ||
| expect(() => { | ||
| TemplateStringParser.interpolate('Hello, ${name}!', {}); | ||
| }).toThrow(UnscopedValidationError); | ||
| }); | ||
|
|
||
| it('interpolates template with no variables correctly', () => { | ||
| const result = TemplateStringParser.interpolate('Hello, world!', {}); | ||
| expect(result).toBe('Hello, world!'); | ||
| }); | ||
|
|
||
| it('throws error when template contains undefined variable', () => { | ||
| expect(() => { | ||
| TemplateStringParser.interpolate('Hello, ${name}!', { greeting: 'Hi' }); | ||
| }).toThrow(UnscopedValidationError); | ||
| }); | ||
|
|
||
| it('interpolates template with adjacent variables correctly', () => { | ||
| const result = TemplateStringParser.interpolate('${greeting}, ${name}!', { greeting: 'Hi', name: 'John' }); | ||
| expect(result).toBe('Hi, John!'); | ||
| }); | ||
|
|
||
| it('interpolates template with empty variable value correctly', () => { | ||
| const result = TemplateStringParser.interpolate('Hello, ${name}!', { name: '' }); | ||
| expect(result).toBe('Hello, !'); | ||
| }); | ||
|
|
||
| it('interpolates template with variable at the start correctly', () => { | ||
| const result = TemplateStringParser.interpolate('${greeting}, world!', { 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.