Skip to content

Commit 5c6cd20

Browse files
authored
Support default value for merge tags in governance rules. (#93)
* support default value in merge tags * bump version
1 parent 8f15c89 commit 5c6cd20

File tree

3 files changed

+67
-44
lines changed

3 files changed

+67
-44
lines changed

lib/governanceRulesManager.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,56 @@ function doesRegexConfigMatch(regexConfig, requestFields, requestBody, requestHe
9494
}, false);
9595
}
9696

97+
function replaceVariableNameWithValueWithDefault(inputString, variables) {
98+
// This regular expression matches patterns like {{VARIABLE_NAME}} or {{VARIABLE_NAME|DEFAULT_VALUE}}
99+
const variablePattern = /\{\{([^\{\}|]+)(\|([^}]+))?\}\}/g;
100+
101+
return inputString.replace(variablePattern, (match, variableName, _, defaultValue) => {
102+
// Check if the variableName exists in the variables object and is not null/undefined
103+
if (variables.hasOwnProperty(variableName) && variables[variableName] !== null && variables[variableName] !== undefined) {
104+
// Replace with the variable value from the variables object
105+
return variables[variableName];
106+
} else {
107+
// If the variable is not provided, use the default value (if available)
108+
// If defaultValue is undefined (i.e., not provided in the template), this will return an empty string
109+
return defaultValue || 'UNKNOWN';
110+
}
111+
});
112+
}
113+
// // Example usage:
114+
// const inputString = "This string has {{VARIABLE_NAME}} and {{MISSING_VARIABLE|default value}} to be {{foo.bar|default name}} replaced {{no_default.field}}.";
115+
116+
// // Suppose VARIABLE_NAME is provided, but MISSING_VARIABLE is not
117+
// const variables = {
118+
// VARIABLE_NAME: "some value",
119+
// 'foo.bar': 'nihao'
120+
// // MISSING_VARIABLE is not provided, so its default value from the template should be used
121+
// // no_default.field is not provided, the template have no default either, so it becomes "UNKNOWN"
122+
// };
123+
124+
// const result = replaceVariableNameWithValueWithDefault(inputString, variables);
125+
// console.log(result):
126+
// // This string has some value and default value to be nihao replaced UNKNOWN.
127+
97128
function recursivelyReplaceValues(tempObjectOrVal, mergeTagValues, ruleVariables) {
98129
if (!ruleVariables || ruleVariables.length <= 0) {
99130
return tempObjectOrVal;
100131
}
101132

102133
if (typeof tempObjectOrVal === 'string') {
103134
let tempString = tempObjectOrVal;
135+
const variablesAndValues = {};
104136
ruleVariables.forEach(function (ruleVar) {
105137
const varName = ruleVar.name;
106-
const replacementValue = safeGet(mergeTagValues, varName) || 'UNKNOWN';
138+
const replacementValue = safeGet(mergeTagValues, varName);
107139

108-
tempString = tempString.replace('{{' + varName + '}}', replacementValue);
140+
if (replacementValue) {
141+
variablesAndValues[varName] = replacementValue;
142+
}
109143
});
110144

111-
return tempString;
145+
const replacedString = replaceVariableNameWithValueWithDefault(tempString, variablesAndValues);
146+
return replacedString;
112147
}
113148

114149
if (isNil(tempObjectOrVal)) {

package-lock.json

Lines changed: 27 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "moesif-nodejs",
3-
"version": "3.6.1",
3+
"version": "3.6.2",
44
"description": "Monitoring agent to log API calls to Moesif for deep API analytics",
55
"main": "lib/index.js",
66
"typings": "dist/index.d.ts",
@@ -48,7 +48,7 @@
4848
"assert": "^2.0.0",
4949
"blanket": "^1.2.3",
5050
"chai": "^4.3.10",
51-
"express": "^4.17.2",
51+
"express": "^4.19.2",
5252
"express-unless": "^1.0.0",
5353
"mocha": "^9.2.1",
5454
"node-mocks-http": "^1.11.0",

0 commit comments

Comments
 (0)