Skip to content
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

Variables: Fix support for "${self:}" #8343

Merged
merged 3 commits into from Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/classes/Service.js
Expand Up @@ -20,7 +20,7 @@ class Service {
this.serviceObject = null;
this.provider = {
stage: 'dev',
variableSyntax: '\\${([^{}:]+?(?:\\(|:)[^:{}][^{}]*?)}',
variableSyntax: '\\${([^{}:]+?(?:\\(|:)(?:[^:{}][^{}]*?)?)}',
};
this.custom = {};
this.plugins = [];
Expand Down
50 changes: 24 additions & 26 deletions lib/classes/Variables.js
Expand Up @@ -241,40 +241,38 @@ class Variables {
* Generate an array of objects noting the terminal properties of the given root object and their
* paths
* @param root The object to generate a terminal property path/value set for
* @param current The current part of the given root that terminal properties are being sought
* @param initCurrent The current part of the given root that terminal properties are being sought
* within
* @param [context] An array containing the path to the current object root (intended for internal
* use)
* @param [results] An array of current results (intended for internal use)
* @returns {TerminalProperty[]} The terminal properties of the given root object, with the path
* and value of each
*/
getProperties(root, atRoot, current, cntxt, rslts) {
let context = cntxt;
if (!context) {
context = [];
}
let results = rslts;
if (!results) {
results = [];
}
const addContext = (value, key) =>
this.getProperties(root, false, value, context.concat(key), results);
if (Array.isArray(current)) {
current.map(addContext);
} else if (
_.isObject(current) &&
!_.isDate(current) &&
!_.isRegExp(current) &&
typeof current !== 'function'
) {
if (atRoot || current !== root) {
_.mapValues(current, addContext);
getProperties(root, initAtRoot, initCurrent, initContext, initResults) {
const processedMap = new WeakMap();
return (function self(atRoot, current, context, results) {
if (processedMap.has(current)) return processedMap.get(current);
if (!context) context = [];
if (!results) results = [];
if (_.isObject(current)) processedMap.set(current, results);
const addContext = (value, key) => self(false, value, context.concat(key), results);
if (Array.isArray(current)) {
current.map(addContext);
} else if (
_.isObject(current) &&
!_.isDate(current) &&
!_.isRegExp(current) &&
typeof current !== 'function'
) {
if (atRoot || current !== root) {
_.mapValues(current, addContext);
}
} else {
results.push({ path: context, value: current });
}
} else {
results.push({ path: context, value: current });
}
return results;
return results;
})(initAtRoot, initCurrent, initContext, initResults);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/classes/Variables.test.js
Expand Up @@ -2769,5 +2769,8 @@ module.exports = {
it('should support ${self:key} syntax', () => {
expect(processedConfig.custom.selfReference).to.equal('bar');
});
it('should support ${self:} syntax', () => {
expect(processedConfig.custom.serviceReference).to.equal(processedConfig);
});
});
});
2 changes: 1 addition & 1 deletion lib/plugins/aws/provider/awsProvider.test.js
Expand Up @@ -59,7 +59,7 @@ describe('AwsProvider', () => {
});

it('should have no AWS logger', () => {
expect(awsProvider.sdk.config.logger).to.be.null;
expect(awsProvider.sdk.config.logger == null).to.be.true;
});

it('should set AWS logger', () => {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/variables/serverless.yml
Expand Up @@ -10,3 +10,4 @@ custom:
awsVariable: ${AWS::Region}
cloudFormationReference: ${AnotherResource}
selfReference: ${self:custom.importedFileWithKey}
serviceReference: ${self:}