Skip to content

Commit

Permalink
chore: add PathExists to graph
Browse files Browse the repository at this point in the history
  • Loading branch information
miparnisari committed Jan 20, 2025
1 parent fda1cab commit 8b182b4
Show file tree
Hide file tree
Showing 2 changed files with 541 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/go/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (g *AuthorizationModelGraph) GetDrawingDirection() DrawingDirection {
return g.drawingDirection
}

// GetNodeByLabel provides O(1) access to a node.
// GetNodeByLabel provides O(1) access to a node. If the node doesn't exist, it returns ErrQueryingGraph.
func (g *AuthorizationModelGraph) GetNodeByLabel(label string) (*AuthorizationModelNode, error) {
id, ok := g.ids[label]
if !ok {
Expand Down Expand Up @@ -101,6 +101,22 @@ func (g *AuthorizationModelGraph) Reversed() (*AuthorizationModelGraph, error) {
return nil, fmt.Errorf("%w: could not cast to directed graph", ErrBuildingGraph)
}

// PathExists returns true if both nodes exist and there is a path starting at 'fromLabel' extending to 'toLabel'.
// If either node doesn't exist, it returns false and ErrQueryingGraph.
func (g *AuthorizationModelGraph) PathExists(fromLabel, toLabel string) (bool, error) {
fromNode, err := g.GetNodeByLabel(fromLabel)
if err != nil {
return false, err
}

toNode, err := g.GetNodeByLabel(toLabel)
if err != nil {
return false, err
}

return topo.PathExistsIn(g.DirectedGraph, fromNode, toNode), nil
}

var _ dot.Attributers = (*AuthorizationModelGraph)(nil)

func (g *AuthorizationModelGraph) DOTAttributers() (encoding.Attributer, encoding.Attributer, encoding.Attributer) {
Expand Down
Loading

0 comments on commit 8b182b4

Please sign in to comment.