Skip to content

Commit 4afad32

Browse files
authored
feat: Allow variable nesting (#294)
1 parent 9d15f46 commit 4afad32

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/modules/variables.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,16 @@ export class VariableCollection {
162162
}
163163

164164
substitute(str: string): string {
165-
for (const kv of this._variables.entries()) {
166-
str = str.replaceAll(`\$\{\{ ${kv[0]} \}\}`, kv[1]);
165+
// Number of variable nestings allowed.
166+
// E.g. a variable can include a reference to another variable value
167+
const ALLOWED_VARIABLE_REFERENCE_LEVELS = 1;
168+
169+
for (let i = 0; i <= ALLOWED_VARIABLE_REFERENCE_LEVELS; ++i) {
170+
for (const kv of this._variables.entries()) {
171+
str = str.replaceAll(`\$\{\{ ${kv[0]} \}\}`, kv[1]);
172+
}
167173
}
174+
168175
return str;
169176
}
170177

test/unit/variables.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ describe('variables - module', () => {
8686
description: 'A bool with a default value',
8787
default: false,
8888
},
89+
{
90+
name: 'testLevelTwoNesting',
91+
type: 'string',
92+
description: 'A variable with a variable reference',
93+
default: 'I am unable to hold another level of reference ${{ testWithDefault }}',
94+
},
95+
{
96+
name: 'testVariableReference',
97+
type: 'string',
98+
description: 'A variable with a variable reference',
99+
default: 'I reference the value of ${{ testNumber }} and ${{ testLevelTwoNesting }}',
100+
},
89101
],
90102
};
91103
pkg2Comp1Manifest = {
@@ -241,6 +253,12 @@ describe('variables - module', () => {
241253
expect(envVars['builtin_package_github_ref']).to.equal('v1.1.1');
242254
expect(envVars['builtin_component_id']).to.equal('test-component');
243255
});
256+
it('should allow one level of variable references', () => {
257+
const vars = VariableCollection.build([componentContext], variablesMapProjCfg, componentContext);
258+
expect(vars.substitute('${{ testVariableReference }}')).to.equal(
259+
'I reference the value of 1 and I am unable to hold another level of reference ${{ testWithDefault }}',
260+
);
261+
});
244262
it('should not throw an error when trying to build VariableCollection with identical VariableDefinition names', () => {
245263
const buildVariableCollection = () => {
246264
const alreadyExistingVariableDefName = {

0 commit comments

Comments
 (0)