Skip to content

Commit

Permalink
fix: updated concatenation handling in form payload (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: fedepini <[email protected]>
  • Loading branch information
fedepini and fedepini authored Feb 5, 2025
1 parent 959a015 commit f1c8585
Showing 1 changed file with 20 additions and 31 deletions.
51 changes: 20 additions & 31 deletions src/components/Widgets/FormGenerator/FormGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,38 +321,27 @@ const FormGenerator = ({title, description, descriptionTooltip = false, fieldsEn
return result;
}

const updateJson = (values, keyPath, valuePath) => {
const getObjectByPath = (obj, path) => path
.split('.')
.reduce((acc, part) => acc && acc[part], obj);
// const key = getObjectByPath(values, keyPath);

const substr = valuePath.replace("${", "").replace("}", "")
const arr = substr.split("+").map(el => el.trim())
let append = ""
let jsonpath = ""
arr.forEach(el => {
if ((el.indexOf("\"") > -1) || (el.indexOf("'") > -1)) {
append = el.replace(/"/g, '');
} else {
jsonpath = el
}
});
const value = getObjectByPath(values, jsonpath) || ""; // value: "${ lorem.ipsum + \"-ns\" + \"-xy\" }"

/*
if (key !== undefined && value !== undefined) {
// update value
values[key] = `${value}${append}`;
}
if (key === undefined && value !== undefined) {
// add key
values = _.merge({}, values, convertStringToObject(keyPath, `${value}${append}`))
}
*/
values = _.merge({}, values, convertStringToObject(keyPath, `${value}${append}`))
// Example input: valuePath = "${ git.toRepo.name + \"-\" + git.toRepo.org }"
// Example parts: ["git.toRepo.name", "\"-\"", "git.toRepo.org"]
// Example transformation:
// getObjectByPath(values, "git.toRepo.name") → "name"
// getObjectByPath(values, "git.toRepo.org") → "org"
// Result: value = "name-org"

return values;
const updateJson = (values, keyPath, valuePath) => {
const getObjectByPath = (obj, path) =>
path.split('.').reduce((acc, part) => acc && acc[part], obj)

const substr = valuePath.replace("${", "").replace("}", "")
const parts = substr.split("+").map(el => el.trim())

const value = parts.map(el =>
el.startsWith("\"") || el.startsWith("'")
? el.replace(/"/g, '')
: getObjectByPath(values, el) || ""
).join("")

return _.merge({}, values, convertStringToObject(keyPath, value))
}

const updateNameNamespace = (path, name, namespace) => {
Expand Down

0 comments on commit f1c8585

Please sign in to comment.