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 e48d26b
Show file tree
Hide file tree
Showing 2 changed files with 542 additions and 2 deletions.
19 changes: 17 additions & 2 deletions pkg/go/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package graph
import (
"errors"
"fmt"

"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/encoding"
"gonum.org/v1/gonum/graph/encoding/dot"
Expand Down Expand Up @@ -35,7 +34,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 +100,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 e48d26b

Please sign in to comment.