Skip to content

Commit

Permalink
bugfix, cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey Hendrey committed Jan 27, 2024
1 parent 60bc523 commit 12728d8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
7 changes: 7 additions & 0 deletions example/concurrent-homeworlds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"people": ["luke", "han"],
"personDetails": "!${ people.$fetch('https://swapi.dev/api/people/?search='& $).json().results[0]}",
"homeworldURLs": "${ personDetails.homeworld }",
"homeworldDetails": "!${ homeworldURLs.$fetch($).json() }",
"homeworldName": "${ homeworldDetails.name }"
}
6 changes: 6 additions & 0 deletions example/homeworlds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"lukePersonDetails": "${ $fetch('https://swapi.dev/api/people/?search=luke').json().results[0]}",
"lukeHomeworldURL": "${ lukePersonDetails.homeworld }",
"homeworldDetails": "${ $fetch(lukeHomeworldURL).json() }",
"homeworldName": "${ homeworldDetails.name }"
}
51 changes: 31 additions & 20 deletions src/DependencyFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,29 +300,19 @@ export default class DependencyFinder {
}

emitPaths() {
if(this.currentSteps.length === 0){
return;
if(this.currentSteps?.length === 0){
return;
}
const emitted: string[] = [];
const steps: StepRecord[] = this.currentSteps.flat(); //[[],["a","b"], ["c"]] -> ["a","b","c"]
const lastStepsArray = last(this.currentSteps);
this.currentSteps.pop();

function everyStepShouldBeEmitted() {
return lastStepsArray.length > 0 && lastStepsArray.every(s => s.emit);
}

if (everyStepShouldBeEmitted()) {
if (lastStepsArray[0].value === "$") { //corresponding to '$$' variable
//in this case the chain of steps must be broken as '$$' is an absolute reference to root document
lastStepsArray.forEach(s => emitted.push(s.value));
} else {
steps.forEach(s => s.emit && emitted.push(s.value));
}
const lastStepsArray:StepRecord[] = last(this.currentSteps);
let emitted;
if(DependencyFinder.isNoOpSteps(lastStepsArray)){
emitted = [];
}else if(DependencyFinder.stepsRootedIn$(lastStepsArray)){
emitted = DependencyFinder.emit(lastStepsArray);
}else{
return;
emitted = DependencyFinder.emit(this.currentSteps.flat()); //[[],["a","b"], ["c"]] -> ["a","b","c"]
}

this.currentSteps.pop();
if(emitted[emitted.length-1]===""){
emitted.pop(); //foo.{$} would result in [foo, ""] as dependency. Trailing "" must be removed.
}
Expand All @@ -335,6 +325,27 @@ export default class DependencyFinder {

}

static isNoOpSteps(stepArray:StepRecord[]){
return stepArray.length===0 || !stepArray[0].emit; //if a step array starts with a not emitted step, the entire thing is NoOp
}

static stepsRootedIn$(stepArray:StepRecord[]){
return stepArray.length > 0 && stepArray[0].value === "$"; // in the actual expression this is $$ which means 'the variable named $'
}

static emit(stepArray:StepRecord[]){
const emitted = [];
for(let i=0;i<stepArray.length;i++){
const {emit, value} = stepArray[i];
if(emit){
emitted.push(value)
}else{
break;
}
}
return emitted;
}


}

Expand Down
2 changes: 1 addition & 1 deletion src/VizGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class VizGraph {
data = jp.get(tp.output, metaInfo.jsonPointer__);
dataPreview = VizGraph.escapeSpecialCharacters(StatedREPL.stringify(data));
}else{
dataPreview = "--WARNING: data not found--";
dataPreview = "";
}

// Color and style logic
Expand Down
8 changes: 8 additions & 0 deletions src/test/DependencyFinder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,14 @@ test("navigate into function's return II", () => {
});


test("homeworldURLs.$fetch($).json()", () => {
const program = "homeworldURLs.$fetch($).json()";
const df = new DependencyFinder(program);
const deps = df.findDependencies();
expect(deps).toEqual([["homeworldURLs"]]);
});





Expand Down
2 changes: 1 addition & 1 deletion src/test/VizGraph.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Expression: \${b3+b2}", style="filled", fillcolor="#87c095", fontcolor="#f8f8f2"
Data: null
Expression: \${b4}", style="filled", fillcolor="#87c095", fontcolor="#f8f8f2" ];
"/b4" [label="JSONPointer: /b4
Data: --WARNING: data not found--", style="filled,dashed", fillcolor="#ffb86c", fontcolor="#44475a" ];
Data: ", style="filled,dashed", fillcolor="#ffb86c", fontcolor="#44475a" ];
"/b/b1" -> "/b" [label="parent", color="#8be9fd", fontcolor="#8be9fd"];
"/b/b2" -> "/b/b1" [label="depends on", color="#bd93f9", fontcolor="#bd93f9" ];
"/b/b2" -> "/b" [label="parent", color="#8be9fd", fontcolor="#8be9fd"];
Expand Down

0 comments on commit 12728d8

Please sign in to comment.