Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sdk): making the curate v2 mappings work #1619

Merged
merged 8 commits into from
Jun 25, 2024
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);
};
kemuru marked this conversation as resolved.
Show resolved Hide resolved
Loading