Skip to content

Commit

Permalink
fix(restquery): support non quoted attributes
Browse files Browse the repository at this point in the history
Non quoted but URI encoded attributes are now supported as well.
At the same time to reduce the likelyhood of name clashes
only ${attribute.*} will be replaced.
  • Loading branch information
mbehr1 committed Jan 2, 2021
1 parent bf1f605 commit 79dc2b4
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/webview/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export async function triggerRestQueryDetails(dataSourceObj, attributes) {
try {
const reqSource = dataSourceObj.source;

const replaceAttr = (match, p1, offset) => {
const replaceAttr = (match, p1, offset, wrapStringInQuotes) => {
//console.log(`replacing '${match}' '${p1}' at offset ${offset}`);
if (p1.startsWith("attributes.")) { // currently only attribute supported
let attrName = p1.slice(p1.indexOf('.') + 1);
Expand All @@ -97,22 +97,30 @@ export async function triggerRestQueryDetails(dataSourceObj, attributes) {
const attrKeyValue = Array.isArray(attrValue) ? attrValue.map(e => attrKey ? e[attrKey] : e) : attrKey ? attrValue[attrKey] : attrValue;
if (typeof attrKeyValue === 'string') {
// console.log(`attrKeyValue='${attrKeyValue}'`, attribute);
return `"${attrKeyValue}"`; // need to wrap the string in ""
return wrapStringInQuotes ? `"${attrKeyValue}"` : attrKeyValue;
} else {
// console.log(`attrKeyValue='${JSON.stringify(attrKeyValue)}'`, attribute);
return JSON.stringify(attrKeyValue);
}
}
return `<unknown attribute:${attrName}>`;
}
return `<unknown ${p1}>`;
return wrapStringInQuotes ? `"${p1}"` : p1;
};

let requestStr = '';
if (typeof reqSource === 'string') {
requestStr = reqSource.replace(/"\$\{(.*?)\}"/g, (match, p1, offset) => replaceAttr(match, p1, offset));
// rules are:
// "${attributes.name}"
// -> "<value of attr.name" (with brackets if .name is of type string)
// -> JSON representation otherwise (e.g. for arrays [...])
// problem is that arrays should not be "" quoted.

requestStr = reqSource.replace(/"\$\{(.*?)\}"/g, (match, p1, offset) => replaceAttr(match, p1, offset, true));
// replace the URI encoded ones as well, but uri encode them then:
requestStr = requestStr.replace(/%22%24%7B(.*?)%7D%22/g, (match, p1, offset) => encodeURIComponent(replaceAttr(match, p1, offset, true)));
// support uri encoded attrs like ${attributes.*} (w.o. being double enquoted) as well:
requestStr = requestStr.replace(/%24%7B(.*?)%7D/g, (match, p1, offset) => encodeURIComponent(replaceAttr(match, p1, offset, false)));
}

const res = await triggerRestQuery(requestStr);
Expand Down

0 comments on commit 79dc2b4

Please sign in to comment.