diff --git a/kleros-sdk/src/dataMappings/actions/jsonAction.ts b/kleros-sdk/src/dataMappings/actions/jsonAction.ts index 3857b4006..87e787131 100644 --- a/kleros-sdk/src/dataMappings/actions/jsonAction.ts +++ b/kleros-sdk/src/dataMappings/actions/jsonAction.ts @@ -2,6 +2,10 @@ import { JsonMapping } from "../utils/actionTypes"; import { createResultObject } from "src/dataMappings/utils/createResultObject"; export const jsonAction = (mapping: JsonMapping) => { - const { value: source, seek, populate } = mapping; - return createResultObject(source, seek, populate); + const { value, seek, populate } = mapping; + + // Parse the source if it's a JSON string + const parsedValue = typeof value === "string" ? JSON.parse(value) : value; + + return createResultObject(parsedValue, seek, populate); }; diff --git a/kleros-sdk/src/dataMappings/executeActions.ts b/kleros-sdk/src/dataMappings/executeActions.ts index 049c31a87..2ea43cacd 100644 --- a/kleros-sdk/src/dataMappings/executeActions.ts +++ b/kleros-sdk/src/dataMappings/executeActions.ts @@ -42,6 +42,7 @@ export const executeActions = async (mappings, initialContext = {}) => { for (const mapping of mappings) { const actionResult = await executeAction(mapping, context); + if (actionResult) { Object.keys(actionResult).forEach((key) => { context[key] = actionResult[key]; diff --git a/kleros-sdk/src/dataMappings/utils/createResultObject.ts b/kleros-sdk/src/dataMappings/utils/createResultObject.ts index 184965e4f..4c4ed2155 100644 --- a/kleros-sdk/src/dataMappings/utils/createResultObject.ts +++ b/kleros-sdk/src/dataMappings/utils/createResultObject.ts @@ -1,34 +1,22 @@ -// Can this be replaced by Mustache ? -export const createResultObject = (sourceData, seek, populate) => { +export const createResultObject = (sourceData, seek: string[], populate: string[]) => { const result = {}; - seek.forEach((key, idx) => { - let foundValue = sourceData; - - if (key.includes(".")) { - const keyParts = key.split("."); - for (const part of keyParts) { - if (foundValue[part] !== undefined) { - foundValue = foundValue[part]; - } else { - foundValue = undefined; - break; - } - } - } else { - if (typeof sourceData !== "object" || key === "0") { - foundValue = sourceData; - } else { - foundValue = sourceData[key]; + const getNestedValue = (obj: any, path: string) => { + return path.split(".").reduce((acc, part) => { + if (acc && part.includes("[")) { + const [key, index] = part.replace(/\]/g, "").split("["); + return acc[key]?.[index]; } - } + return acc ? acc[part] : undefined; + }, obj); + }; - console.log(`Seek key: ${key}, Found value:`, foundValue); + seek.forEach((key, idx) => { + const foundValue = getNestedValue(sourceData, key); if (foundValue !== undefined) { result[populate[idx]] = foundValue; - console.log(`Populate key: ${populate[idx]}, Value to add:`, foundValue); } }); - console.log("Result object:", result); + return result; }; diff --git a/kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts b/kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts index 1d8e16439..a6d25add5 100644 --- a/kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts +++ b/kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts @@ -1,13 +1,17 @@ -export const replacePlaceholdersWithValues = (mapping: any, context: any) => { - let mappingAsString = JSON.stringify(mapping); +import mustache from "mustache"; - const replacedMapping = mappingAsString.replace(/\{\{(\w+)\}\}/g, (_, variableName) => { - if (context.hasOwnProperty(variableName)) { - return context[variableName]; +export const replacePlaceholdersWithValues = (mapping: any, context: any) => { + const replace = (obj) => { + if (typeof obj === "string") { + return mustache.render(obj, context); + } else if (Array.isArray(obj)) { + return obj.map(replace); + } else if (typeof obj === "object" && obj !== null) { + return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, replace(value)])); } else { - throw new Error(`Variable '${variableName}' not found in context.`); + return obj; } - }); + }; - return JSON.parse(replacedMapping); + return replace(mapping); };