Skip to content

Commit

Permalink
fix: auto place empty verticies when placing edges to unknown targets
Browse files Browse the repository at this point in the history
  • Loading branch information
tinyzimmer committed Nov 5, 2023
1 parent ea451f7 commit 5dba873
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/storage/types/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,33 @@ func (e MeshEdge) PutInto(ctx context.Context, g PeerGraph) error {
if !errors.IsEdgeNotFound(err) {
return fmt.Errorf("get edge: %w", err)
}
// If either vertex does not exist, create an empty one.
_, err = g.Vertex(e.SourceID())
if err != nil {
if errors.Is(err, graph.ErrVertexNotFound) {
err = g.AddVertex(MeshNode{
MeshNode: &v1.MeshNode{Id: e.SourceID().String()},
})
if err != nil {
return fmt.Errorf("add source vertex: %w", err)
}
} else {
return fmt.Errorf("get source vertex: %w", err)
}
}
_, err = g.Vertex(e.TargetID())
if err != nil {
if errors.Is(err, graph.ErrVertexNotFound) {
err = g.AddVertex(MeshNode{
MeshNode: &v1.MeshNode{Id: e.TargetID().String()},
})
if err != nil {
return fmt.Errorf("add target vertex: %w", err)
}
} else {
return fmt.Errorf("get target vertex: %w", err)
}
}
err = g.AddEdge(e.SourceID(), e.TargetID(), opts...)
if err != nil && !errors.Is(err, graph.ErrEdgeAlreadyExists) {
return fmt.Errorf("add edge: %w", err)
Expand Down

0 comments on commit 5dba873

Please sign in to comment.