Skip to content

Commit ff6de3a

Browse files
VirtueMeclaude
andauthored
refactor(naming): introduce generateLogicalId factory method (#762)
- Add generateLogicalId(name, suffix) to centralise the repeated getNormalizedFunctionName(name) + suffix pattern - Refactor 9 get*LogicalId methods to delegate to the factory - Add 2 tests for the new factory method (19/19 passing) Part of #708 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 193d723 commit ff6de3a

2 files changed

Lines changed: 27 additions & 27 deletions

File tree

lib/naming.js

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
'use strict';
22

33
module.exports = {
4+
generateLogicalId(name, suffix = '') {
5+
return `${this.provider.naming.getNormalizedFunctionName(name)}${suffix}`;
6+
},
7+
48
getStateMachineLogicalId(stateMachineName, stateMachine) {
59
const custom = stateMachine ? stateMachine.id || stateMachine.name : null;
6-
710
if (custom) {
8-
return `${this.provider.naming.getNormalizedFunctionName(custom)}`;
11+
return this.generateLogicalId(custom);
912
}
10-
11-
return `${this.provider.naming
12-
.getNormalizedFunctionName(stateMachineName)}StepFunctionsStateMachine`;
13+
return this.generateLogicalId(stateMachineName, 'StepFunctionsStateMachine');
1314
},
1415

1516
getStateMachineOutputLogicalId(stateMachineName, stateMachine) {
1617
const custom = stateMachine ? stateMachine.id || stateMachine.name : null;
17-
1818
if (custom) {
19-
return `${this.provider.naming.getNormalizedFunctionName(custom)}Arn`;
19+
return this.generateLogicalId(custom, 'Arn');
2020
}
21-
22-
return `${this.provider.naming
23-
.getNormalizedFunctionName(stateMachineName)}StepFunctionsStateMachineArn`;
21+
return this.generateLogicalId(stateMachineName, 'StepFunctionsStateMachineArn');
2422
},
2523

2624
getActivityLogicalId(activityName) {
27-
return `${this.provider.naming
28-
.getNormalizedFunctionName(activityName)}StepFunctionsActivity`;
25+
return this.generateLogicalId(activityName, 'StepFunctionsActivity');
2926
},
3027

3128
getActivityOutputLogicalId(activityName) {
32-
return `${this.provider.naming
33-
.getNormalizedFunctionName(activityName)}StepFunctionsActivityArn`;
29+
return this.generateLogicalId(activityName, 'StepFunctionsActivityArn');
3430
},
3531

3632
getStateMachinePolicyName() {
@@ -59,20 +55,15 @@ module.exports = {
5955
},
6056

6157
getScheduleLogicalId(stateMachineName, scheduleIndex) {
62-
return `${this.provider.naming
63-
.getNormalizedFunctionName(stateMachineName)}StepFunctionsEventsRuleSchedule${scheduleIndex}`;
58+
return this.generateLogicalId(stateMachineName, `StepFunctionsEventsRuleSchedule${scheduleIndex}`);
6459
},
6560

6661
getSchedulerScheduleLogicalId(stateMachineName, scheduleIndex) {
67-
return `${this.provider.naming.getNormalizedFunctionName(
68-
stateMachineName,
69-
)}StepFunctionsSchedulerSchedule${scheduleIndex}`;
62+
return this.generateLogicalId(stateMachineName, `StepFunctionsSchedulerSchedule${scheduleIndex}`);
7063
},
7164

7265
getScheduleToStepFunctionsIamRoleLogicalId(stateMachineName) {
73-
return `${this.provider.naming.getNormalizedFunctionName(
74-
stateMachineName,
75-
)}ScheduleToStepFunctionsRole`;
66+
return this.generateLogicalId(stateMachineName, 'ScheduleToStepFunctionsRole');
7667
},
7768

7869
getSchedulePolicyName(stateMachineName) {
@@ -91,8 +82,7 @@ module.exports = {
9182
},
9283

9384
getCloudWatchEventLogicalId(stateMachineName, cloudWatchIndex) {
94-
return `${this.provider.naming
95-
.getNormalizedFunctionName(stateMachineName)}EventsRuleCloudWatchEvent${cloudWatchIndex}`;
85+
return this.generateLogicalId(stateMachineName, `EventsRuleCloudWatchEvent${cloudWatchIndex}`);
9686
},
9787

9888
getCloudWatchEventPolicyName(stateMachineName) {
@@ -106,8 +96,6 @@ module.exports = {
10696
},
10797

10898
getCloudWatchEventToStepFunctionsIamRoleLogicalId(stateMachineName) {
109-
return `${this.provider.naming.getNormalizedFunctionName(
110-
stateMachineName,
111-
)}EventToStepFunctionsRole`;
99+
return this.generateLogicalId(stateMachineName, 'EventToStepFunctionsRole');
112100
},
113101
};

lib/naming.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ describe('#naming', () => {
1616
serverlessStepFunctions = new ServerlessStepFunctions(serverless);
1717
});
1818

19+
describe('#generateLogicalId()', () => {
20+
it('should normalize the name and append the suffix', () => {
21+
expect(serverlessStepFunctions.generateLogicalId('myResource', 'StepFunctionsFoo')).to
22+
.equal('MyResourceStepFunctionsFoo');
23+
});
24+
25+
it('should normalize the name with no suffix', () => {
26+
expect(serverlessStepFunctions.generateLogicalId('myResource')).to
27+
.equal('MyResource');
28+
});
29+
});
30+
1931
describe('#getStateMachineLogicalId()', () => {
2032
it('should normalize the stateMachine name and add the standard suffix', () => {
2133
expect(serverlessStepFunctions.getStateMachineLogicalId('stateMachine')).to

0 commit comments

Comments
 (0)