-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1619 from kleros/feat(sdk)/improve-mapping-json-a…
…ction chore(sdk): making the curate v2 mappings work
- Loading branch information
Showing
4 changed files
with
31 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
kleros-sdk/src/dataMappings/utils/replacePlaceholdersWithValues.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |