Skip to content

Commit

Permalink
Merge pull request #1619 from kleros/feat(sdk)/improve-mapping-json-a…
Browse files Browse the repository at this point in the history
…ction

chore(sdk): making the curate v2 mappings work
  • Loading branch information
alcercu authored Jun 25, 2024
2 parents ee63a5d + 78dbbf6 commit 6ac5035
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
8 changes: 6 additions & 2 deletions kleros-sdk/src/dataMappings/actions/jsonAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
1 change: 1 addition & 0 deletions kleros-sdk/src/dataMappings/executeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
36 changes: 12 additions & 24 deletions kleros-sdk/src/dataMappings/utils/createResultObject.ts
Original file line number Diff line number Diff line change
@@ -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;
};
20 changes: 12 additions & 8 deletions kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts
Original file line number Diff line number Diff line change
@@ -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);
};

0 comments on commit 6ac5035

Please sign in to comment.