Skip to content

Commit

Permalink
feat: Allow variable nesting (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
doosuu authored Jul 8, 2024
1 parent 9d15f46 commit 4afad32
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/modules/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,16 @@ export class VariableCollection {
}

substitute(str: string): string {
for (const kv of this._variables.entries()) {
str = str.replaceAll(`\$\{\{ ${kv[0]} \}\}`, kv[1]);
// Number of variable nestings allowed.
// E.g. a variable can include a reference to another variable value
const ALLOWED_VARIABLE_REFERENCE_LEVELS = 1;

for (let i = 0; i <= ALLOWED_VARIABLE_REFERENCE_LEVELS; ++i) {
for (const kv of this._variables.entries()) {
str = str.replaceAll(`\$\{\{ ${kv[0]} \}\}`, kv[1]);
}
}

return str;
}

Expand Down
18 changes: 18 additions & 0 deletions test/unit/variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ describe('variables - module', () => {
description: 'A bool with a default value',
default: false,
},
{
name: 'testLevelTwoNesting',
type: 'string',
description: 'A variable with a variable reference',
default: 'I am unable to hold another level of reference ${{ testWithDefault }}',
},
{
name: 'testVariableReference',
type: 'string',
description: 'A variable with a variable reference',
default: 'I reference the value of ${{ testNumber }} and ${{ testLevelTwoNesting }}',
},
],
};
pkg2Comp1Manifest = {
Expand Down Expand Up @@ -241,6 +253,12 @@ describe('variables - module', () => {
expect(envVars['builtin_package_github_ref']).to.equal('v1.1.1');
expect(envVars['builtin_component_id']).to.equal('test-component');
});
it('should allow one level of variable references', () => {
const vars = VariableCollection.build([componentContext], variablesMapProjCfg, componentContext);
expect(vars.substitute('${{ testVariableReference }}')).to.equal(
'I reference the value of 1 and I am unable to hold another level of reference ${{ testWithDefault }}',
);
});
it('should not throw an error when trying to build VariableCollection with identical VariableDefinition names', () => {
const buildVariableCollection = () => {
const alreadyExistingVariableDefName = {
Expand Down

0 comments on commit 4afad32

Please sign in to comment.