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
4 changes: 3 additions & 1 deletion kleros-sdk/src/dataMappings/actions/jsonAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ import { createResultObject } from "src/dataMappings/utils/createResultObject";

export const jsonAction = (mapping: JsonMapping) => {
const { value: source, seek, populate } = mapping;
return createResultObject(source, seek, populate);
const sourceData = JSON.parse(source);

return createResultObject(sourceData, seek, populate);
kemuru marked this conversation as resolved.
Show resolved Hide resolved
};
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
37 changes: 13 additions & 24 deletions kleros-sdk/src/dataMappings/utils/createResultObject.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
// Can this be replaced by Mustache ?
export const createResultObject = (sourceData, seek, populate) => {
// Can this be replaced by Mustache?
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("]", "").split("[");
Fixed Show fixed Hide fixed
kemuru marked this conversation as resolved.
Show resolved Hide resolved
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);

kemuru marked this conversation as resolved.
Show resolved Hide resolved
return result;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ export const replacePlaceholdersWithValues = (mapping: any, context: any) => {

const replacedMapping = mappingAsString.replace(/\{\{(\w+)\}\}/g, (_, variableName) => {
if (context.hasOwnProperty(variableName)) {
return context[variableName];
const value = context[variableName];

if (typeof value === "string" && value.startsWith("{") && value.endsWith("}")) {
return JSON.stringify(value);
} else {
return typeof value === "bigint" ? value.toString() : value;
}
kemuru marked this conversation as resolved.
Show resolved Hide resolved
} else {
throw new Error(`Variable '${variableName}' not found in context.`);
}
});

return JSON.parse(replacedMapping);
const parsedMapping = JSON.parse(replacedMapping.replace(/"(\{.*\})"/, "$1"));
return parsedMapping;
kemuru marked this conversation as resolved.
Show resolved Hide resolved
};
Loading