chore: pr #632 polish

This commit is contained in:
Anoop M D 2023-10-18 10:54:51 +05:30
parent d5a72b1a5d
commit b6da0b2113
2 changed files with 19 additions and 13 deletions

View File

@ -17,7 +17,7 @@ const interpolateEnvVars = (str, processEnvVars) => {
}); });
}; };
const interpolateString = (str, { envVars, collectionVariables, processEnvVars }) => { const interpolateString = (str, { envVariables, collectionVariables, processEnvVars }) => {
if (!str || !str.length || typeof str !== 'string') { if (!str || !str.length || typeof str !== 'string') {
return str; return str;
} }
@ -25,20 +25,20 @@ const interpolateString = (str, { envVars, collectionVariables, processEnvVars }
processEnvVars = processEnvVars || {}; processEnvVars = processEnvVars || {};
collectionVariables = collectionVariables || {}; collectionVariables = collectionVariables || {};
// we clone envVars because we don't want to modify the original object // we clone envVariables because we don't want to modify the original object
envVars = envVars ? cloneDeep(envVars) : {}; envVariables = envVariables ? cloneDeep(envVariables) : {};
// envVars can inturn have values as {{process.env.VAR_NAME}} // envVariables can inturn have values as {{process.env.VAR_NAME}}
// so we need to interpolate envVars first with processEnvVars // so we need to interpolate envVariables first with processEnvVars
forOwn(envVars, (value, key) => { forOwn(envVariables, (value, key) => {
envVars[key] = interpolateEnvVars(value, processEnvVars); envVariables[key] = interpolateEnvVars(value, processEnvVars);
}); });
const template = Handlebars.compile(str, { noEscape: true }); const template = Handlebars.compile(str, { noEscape: true });
// collectionVariables take precedence over envVars // collectionVariables take precedence over envVariables
const combinedVars = { const combinedVars = {
...envVars, ...envVariables,
...collectionVariables, ...collectionVariables,
process: { process: {
env: { env: {

View File

@ -162,6 +162,12 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
return; return;
} }
const interpolationContext = {
collectionVariables: context.bru.collectionVariables,
envVariables: context.bru.envVariables,
processEnvVars: context.bu.processEnvVars
};
// gracefully allow both a,b as well as [a, b] // gracefully allow both a,b as well as [a, b]
if (operator === 'in' || operator === 'notIn') { if (operator === 'in' || operator === 'notIn') {
if (rhsOperand.startsWith('[') && rhsOperand.endsWith(']')) { if (rhsOperand.startsWith('[') && rhsOperand.endsWith(']')) {
@ -170,13 +176,13 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
return rhsOperand return rhsOperand
.split(',') .split(',')
.map((v) => evaluateJsTemplateLiteral(interpolateString(v.trim(), context.bru), context)); .map((v) => evaluateJsTemplateLiteral(interpolateString(v.trim(), interpolationContext), context));
} }
if (operator === 'between') { if (operator === 'between') {
const [lhs, rhs] = rhsOperand const [lhs, rhs] = rhsOperand
.split(',') .split(',')
.map((v) => evaluateJsTemplateLiteral(interpolateString(v.trim(), context.bru), context)); .map((v) => evaluateJsTemplateLiteral(interpolateString(v.trim(), interpolationContext), context));
return [lhs, rhs]; return [lhs, rhs];
} }
@ -186,10 +192,10 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
rhsOperand = rhsOperand.substring(1, rhsOperand.length - 1); rhsOperand = rhsOperand.substring(1, rhsOperand.length - 1);
} }
return interpolateString(rhsOperand, context.bru); return interpolateString(rhsOperand, interpolationContext);
} }
return evaluateJsTemplateLiteral(interpolateString(rhsOperand, context.bru), context); return evaluateJsTemplateLiteral(interpolateString(rhsOperand, interpolationContext), context);
}; };
class AssertRuntime { class AssertRuntime {