From c7c2c7c95a259e4b136723161ed00a87680b388b Mon Sep 17 00:00:00 2001 From: Lukas Jarosch Date: Tue, 5 Mar 2024 22:27:07 +0100 Subject: [PATCH] refactor: `RemoveReferenceVertex` removes it's edges automatically --- reference/value.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/reference/value.go b/reference/value.go index ba9d102..355b4c2 100644 --- a/reference/value.go +++ b/reference/value.go @@ -278,11 +278,27 @@ func AddReferenceVertex(dependencyGraph graph.Graph[string, ValueReference], ref return nil } -func RemoveReferenceVertex(dependencyGraph graph.Graph[string, ValueReference], reference ValueReference, dependencies []ValueReference) error { - for _, dependency := range dependencies { - err := dependencyGraph.RemoveEdge(reference.Hash(), dependency.Hash()) - if err != nil { - return fmt.Errorf("failed to remove dependency %s: %w", dependency.Name(), err) +func RemoveReferenceVertex(dependencyGraph graph.Graph[string, ValueReference], reference ValueReference) error { + edges, err := dependencyGraph.Edges() + if err != nil { + return err + } + + // Find all edges with either originate from or point to the reference and remove them. + for _, edge := range edges { + if edge.Source == reference.Hash() { + err = dependencyGraph.RemoveEdge(edge.Source, edge.Target) + if err != nil { + return err + } + continue + } + if edge.Target == reference.Hash() { + err = dependencyGraph.RemoveEdge(edge.Source, edge.Target) + if err != nil { + return err + } + continue } }