Skip to content

Commit

Permalink
don't fanout from functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey Hendrey committed Jan 22, 2024
1 parent b93f8fe commit 6c70764
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/TemplateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,11 +1123,20 @@ export default class TemplateProcessor {
const origin = jsonPtr;

//----------------- utility functions ----------------//

const isFunction = (jsonPointer)=>{
if(!jp.has(this.templateMeta, jsonPointer)){
return false;
}
const metaInf = jp.get(this.templateMeta, jsonPointer);
return !!metaInf.isFunction__;
}

const queueParent = (jsonPtr)=>{
//search "up" from this currentPtr to find any dependees of the ancestors of currentPtr
const parentPointer = jp.parent(jsonPtr);//jp.compile(parts.slice(0, parts.length - 1));
if (parentPointer !== '' && !visited.has(parentPointer)) {
queue.push(parentPointer);
!isFunction(parentPointer) && queue.push(parentPointer);
visited.add(parentPointer);
}
}
Expand All @@ -1136,7 +1145,7 @@ export default class TemplateProcessor {
if (metaInf.dependees__) {
metaInf.dependees__.forEach(dependee => {
if (!visited.has(dependee)) {
queue.push(dependee);
!isFunction(dependee) && queue.push(dependee);
visited.add(dependee);
}
});
Expand All @@ -1153,7 +1162,7 @@ export default class TemplateProcessor {
// Generate json pointer for child
let childPtr = `${currentPtr}/${key}`;
if (!visited.has(childPtr)) {
queue.push(childPtr);
!isFunction(childPtr) && queue.push(childPtr);
visited.add(childPtr);
}
}
Expand Down
57 changes: 57 additions & 0 deletions src/test/TemplateProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,63 @@ test("root / vs absolute root // inside various rooted expressions", async () =>
expect(tp.output.b.c.i.b).toBe("Global A");
});

// This test ensures that functions do not have dependencies
test("functions are immutable and have no 'from'", async () => {
const templateYaml = `
# producer will be sending some test data
produceParams:
type: "my-topic"
data: ['luke', 'han', 'leia']
client:
type: test
# the subscriber's 'to' function will be called on each received event
subscribeParams: #parameters for subscribing to an event
source: cloudEvent
type: /\${produceParams.type} # subscribe to the same topic as we are publishing to test events
to: /\${joinResistance}
subscriberId: rebelArmy
initialPosition: latest
client:
type: test
joinResistance: /\${function ($rebel) {$set('/rebelForces', rebelForces ~> $append($rebel))}}
# starts producer function
send$: $publish(produceParams)
# starts consumer function
recv$: $subscribe(subscribeParams)
# the subscriber's \`to\` function will write the received data here
rebelForces: [ ]`

const template = yaml.load(templateYaml);
const tp = new TemplateProcessor(template, {publish: ()=>{'NoOp'}, subscribe: ()=>{'NoOp'}}); //just stub subscribe with a noop
await tp.initialize();
expect(tp.from("/rebelForces")).toEqual(["/rebelForces"]); // /rebelForces has no fan-out
expect(tp.to("/rebelForces")).toEqual(["/rebelForces"]); // /rebelForces has no fan-in
expect(tp.from("/subscribeParams/to")).toEqual([
"/subscribeParams/to",
"/subscribeParams/type", //<--- this is somewhat dubious
"/recv$"
]);
expect(tp.to("/subscribeParams/to")).toEqual( [
"/rebelForces",
"/joinResistance",
"/subscribeParams/to"
]);

expect(tp.to("/subscribeParams/to")).toEqual([
"/rebelForces",
"/joinResistance",
"/subscribeParams/to"
]);

expect(await tp.getEvaluationPlan()).toEqual([
"/joinResistance",
"/subscribeParams/to",
"/subscribeParams/type",
"/recv$",
"/send$"
]
);
});



Expand Down

0 comments on commit 6c70764

Please sign in to comment.