From 4b1a2d3b11117ef785fe5bcb4190cca1dd68f626 Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Fri, 12 Oct 2018 14:15:44 +0200 Subject: [PATCH] make sure to add all variable nodes to dag before linking variables --- public/app/core/utils/dag.test.ts | 12 +++++++++++ public/app/core/utils/dag.ts | 20 +++++++++++++++++-- .../app/features/templating/variable_srv.ts | 6 ++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/public/app/core/utils/dag.test.ts b/public/app/core/utils/dag.test.ts index 064da13806b..4ee0dd7134b 100644 --- a/public/app/core/utils/dag.test.ts +++ b/public/app/core/utils/dag.test.ts @@ -104,5 +104,17 @@ describe('Directed acyclic graph', () => { const actual = nodeH.getOptimizedInputEdges(); expect(actual).toHaveLength(0); }); + + it('when linking non-existing input node with existing output node should throw error', () => { + expect(() => { + dag.link('non-existing', 'A'); + }).toThrowError("cannot link input node named non-existing since it doesn't exist in graph"); + }); + + it('when linking existing input node with non-existing output node should throw error', () => { + expect(() => { + dag.link('A', 'non-existing'); + }).toThrowError("cannot link output node named non-existing since it doesn't exist in graph"); + }); }); }); diff --git a/public/app/core/utils/dag.ts b/public/app/core/utils/dag.ts index eb7ff1c3b1a..48c00a4c8c3 100644 --- a/public/app/core/utils/dag.ts +++ b/public/app/core/utils/dag.ts @@ -15,6 +15,14 @@ export class Edge { } link(inputNode: Node, outputNode: Node) { + if (!inputNode) { + throw Error('inputNode is required'); + } + + if (!outputNode) { + throw Error('outputNode is required'); + } + this.unlink(); this.inputNode = inputNode; this.outputNode = outputNode; @@ -152,7 +160,11 @@ export class Graph { for (let n = 0; n < inputArr.length; n++) { const i = inputArr[n]; if (typeof i === 'string') { - inputNodes.push(this.getNode(i)); + const n = this.getNode(i); + if (!n) { + throw Error(`cannot link input node named ${i} since it doesn't exist in graph`); + } + inputNodes.push(n); } else { inputNodes.push(i); } @@ -161,7 +173,11 @@ export class Graph { for (let n = 0; n < outputArr.length; n++) { const i = outputArr[n]; if (typeof i === 'string') { - outputNodes.push(this.getNode(i)); + const n = this.getNode(i); + if (!n) { + throw Error(`cannot link output node named ${i} since it doesn't exist in graph`); + } + outputNodes.push(n); } else { outputNodes.push(i); } diff --git a/public/app/features/templating/variable_srv.ts b/public/app/features/templating/variable_srv.ts index 8c0f1f11f77..75e2ca35ec7 100644 --- a/public/app/features/templating/variable_srv.ts +++ b/public/app/features/templating/variable_srv.ts @@ -291,9 +291,11 @@ export class VariableSrv { createGraph() { const g = new Graph(); - this.variables.forEach(v1 => { - g.createNode(v1.name); + this.variables.forEach(v => { + g.createNode(v.name); + }); + this.variables.forEach(v1 => { this.variables.forEach(v2 => { if (v1 === v2) { return;