Skip to content

Commit f6f1e4e

Browse files
committed
feat: replaceplaceholders replaced with mustache, jsonaction json parses in case the input is a stri
1 parent 3ad99b1 commit f6f1e4e

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

kleros-sdk/src/dataMappings/actions/jsonAction.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { JsonMapping } from "../utils/actionTypes";
22
import { createResultObject } from "src/dataMappings/utils/createResultObject";
33

44
export const jsonAction = (mapping: JsonMapping) => {
5-
const { value: source, seek, populate } = mapping;
6-
return createResultObject(source, seek, populate);
5+
const { value, seek, populate } = mapping;
6+
7+
// Parse the source if it's a JSON string
8+
const parsedValue = typeof value === "string" ? JSON.parse(value) : value;
9+
10+
return createResultObject(parsedValue, seek, populate);
711
};

kleros-sdk/src/dataMappings/utils/createResultObject.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Can this be replaced by Mustache?
21
export const createResultObject = (sourceData, seek: string[], populate: string[]) => {
32
const result = {};
43

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
export const replacePlaceholdersWithValues = (mapping: any, context: any) => {
2-
let mappingAsString = JSON.stringify(mapping);
1+
import mustache from "mustache";
32

4-
const replacedMapping = mappingAsString.replace(/\{\{(\w+)\}\}/g, (_, variableName) => {
5-
if (context.hasOwnProperty(variableName)) {
6-
return context[variableName];
3+
export const replacePlaceholdersWithValues = (mapping: any, context: any) => {
4+
const replace = (obj) => {
5+
if (typeof obj === "string") {
6+
return mustache.render(obj, context);
7+
} else if (Array.isArray(obj)) {
8+
return obj.map(replace);
9+
} else if (typeof obj === "object" && obj !== null) {
10+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, replace(value)]));
711
} else {
8-
throw new Error(`Variable '${variableName}' not found in context.`);
12+
return obj;
913
}
10-
});
14+
};
1115

12-
const parsedReplacedMapping = JSON.parse(replacedMapping.replace(/"(\{.*?\})"/g, "$1"));
13-
return parsedReplacedMapping;
16+
return replace(mapping);
1417
};

0 commit comments

Comments
 (0)