Skip to content

Commit

Permalink
credit Sergey Sergeev - no change propagation through function variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey Hendrey committed Oct 25, 2023
1 parent 84cc688 commit 67c6f8a
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/TemplateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,33 @@ export default class TemplateProcessor {
/** Allows caller to set a callback to propagate initialization into their framework */
public onInit: () => void;

public static fromString(template:string, context = {}, options={} ):TemplateProcessor{
let inferredType: "JSON" | "YAML" | "UNKNOWN" = "UNKNOWN";

// Check for JSON
if (template.trim().startsWith('{') || template.trim().startsWith('[')) {
inferredType = "JSON";
}
// Check for YAML
else if (template.includes('---') || /[^":]\s*:\s*[^"]/g.test(template)) {
inferredType = "YAML";
}

let parsedObject;

// Based on the inferred type, parse the string
if (inferredType === "JSON") {
parsedObject = JSON.parse(template);
} else if (inferredType === "YAML") {
parsedObject = yaml.safeLoad(template);
} else {
throw new Error("Unknown format");
}

// Return an instance of TemplateProcessor with the parsed object
return new TemplateProcessor(parsedObject, context, options);
}

constructor(template={}, context = {}, options={}) {
this.uniqueId = Math.random()*1e6;
this.setData = this.setData.bind(this); // Bind template-accessible functions like setData and import
Expand Down Expand Up @@ -946,6 +973,7 @@ export default class TemplateProcessor {
const dependents:MetaInfo[] = [];
const queue:JsonPointerString[] = [jsonPtr];
const visited:Set<JsonPointerString> = new Set();
const origin = jsonPtr;

//----------------- utility functions ----------------//
const queueParent = (jsonPtr)=>{
Expand Down Expand Up @@ -1000,7 +1028,11 @@ export default class TemplateProcessor {
continue;
}
const metaInf = jp.get(this.templateMeta, currentPtr);
if(metaInf.expr__ !== undefined){
const isFunction = metaInf.data__?._jsonata_lambda;
if(isFunction){
continue; //function never gets re-evaluated
}
if(currentPtr !== origin && metaInf.expr__ !== undefined){
dependents.push(metaInf);
}
queueDependees(metaInf); //these are EXPLICIT dependees
Expand Down

0 comments on commit 67c6f8a

Please sign in to comment.