Skip to content

Commit

Permalink
refactor(Variables): Seclude internal logic for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Dec 2, 2021
1 parent c0f8459 commit 9c75044
Showing 1 changed file with 37 additions and 41 deletions.
78 changes: 37 additions & 41 deletions lib/configuration/variables/resolve.js
Expand Up @@ -27,6 +27,33 @@ const variableProcessingErrorNames = new Set(['ServerlessError', 'VariableSource

let lastResolutionBatchId = 0;

const resolveSourceValuesVariables = (sourceValues) => {
// Value is a result of concatenation of string values coming from multiple sources.
// Parse variables in each string part individually - it's to avoid accidental
// resolution of variable-like notation which may surface after joining two values.
// Also that way we do not accidentally re-parse an escaped variables notation
let baseIndex = 0;
let resolvedValueVariables = null;
for (const sourceValue of sourceValues) {
const sourceValueVariables = parse(sourceValue);
if (sourceValueVariables) {
for (const sourceValueVariable of sourceValueVariables) {
if (sourceValueVariable.end) {
sourceValueVariable.start += baseIndex;
sourceValueVariable.end += baseIndex;
} else {
sourceValueVariable.start = baseIndex;
sourceValueVariable.end = baseIndex + sourceValue.length;
}
}
if (!resolvedValueVariables) resolvedValueVariables = [];
resolvedValueVariables.push(...sourceValueVariables);
}
baseIndex += sourceValue.length;
}
return resolvedValueVariables;
};

class VariablesResolver {
constructor({
serviceDir,
Expand Down Expand Up @@ -389,48 +416,17 @@ Object.defineProperties(
let propertyVariablesMeta;
if (typeof value === 'string') {
const valueVariables = (() => {
const silentParse = (sourceValue) => {
try {
return parse(sourceValue);
} catch (error) {
error.message = `Cannot resolve variable at "${humanizePropertyPath(
propertyPathKeys
)}": Approached variable syntax error in resolved value "${value}": ${
error.message
}`;
delete valueMeta.value;
valueMeta.error = error;
return null;
}
};
if (sourceValues) {
// Value is a result of concatenation of string values coming from multiple sources.
// Parse variables in each string part individually - it's to avoid accidental
// resolution of variable-like notation which may surface after joining two values.
// Also that way we do not accidentally re-parse an escaped variables notation
let baseIndex = 0;
let resolvedValueVariables = null;
for (const sourceValue of sourceValues) {
const sourceValueVariables = silentParse(sourceValue);
if (valueMeta.error) return null;
if (sourceValueVariables) {
for (const sourceValueVariable of sourceValueVariables) {
if (sourceValueVariable.end) {
sourceValueVariable.start += baseIndex;
sourceValueVariable.end += baseIndex;
} else {
sourceValueVariable.start = baseIndex;
sourceValueVariable.end = baseIndex + sourceValue.length;
}
}
if (!resolvedValueVariables) resolvedValueVariables = [];
resolvedValueVariables.push(...sourceValueVariables);
}
baseIndex += sourceValue.length;
}
return resolvedValueVariables;
try {
if (sourceValues) return resolveSourceValuesVariables(sourceValues);
return parse(value);
} catch (error) {
error.message = `Cannot resolve variable at "${humanizePropertyPath(
propertyPathKeys
)}": Approached variable syntax error in resolved value "${value}": ${error.message}`;
delete valueMeta.value;
valueMeta.error = error;
return null;
}
return silentParse(value);
})();

if (valueVariables) {
Expand Down

0 comments on commit 9c75044

Please sign in to comment.