github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/graph/tree.go (about)

     1  package graph
     2  
     3  import (
     4  	core "github.com/authzed/spicedb/pkg/proto/core/v1"
     5  )
     6  
     7  // Leaf constructs a RelationTupleTreeNode leaf.
     8  func Leaf(start *core.ObjectAndRelation, subjects ...*core.DirectSubject) *core.RelationTupleTreeNode {
     9  	return &core.RelationTupleTreeNode{
    10  		NodeType: &core.RelationTupleTreeNode_LeafNode{
    11  			LeafNode: &core.DirectSubjects{
    12  				Subjects: subjects,
    13  			},
    14  		},
    15  		Expanded:         start,
    16  		CaveatExpression: nil, // Set by caller if necessary
    17  	}
    18  }
    19  
    20  func setResult(
    21  	op core.SetOperationUserset_Operation,
    22  	start *core.ObjectAndRelation,
    23  	children []*core.RelationTupleTreeNode,
    24  ) *core.RelationTupleTreeNode {
    25  	return &core.RelationTupleTreeNode{
    26  		NodeType: &core.RelationTupleTreeNode_IntermediateNode{
    27  			IntermediateNode: &core.SetOperationUserset{
    28  				Operation:  op,
    29  				ChildNodes: children,
    30  			},
    31  		},
    32  		Expanded:         start,
    33  		CaveatExpression: nil, // Set by caller if necessary
    34  	}
    35  }
    36  
    37  // Union constructs a RelationTupleTreeNode union operation.
    38  func Union(start *core.ObjectAndRelation, children ...*core.RelationTupleTreeNode) *core.RelationTupleTreeNode {
    39  	return setResult(core.SetOperationUserset_UNION, start, children)
    40  }
    41  
    42  // Intersection constructs a RelationTupleTreeNode intersection operation.
    43  func Intersection(start *core.ObjectAndRelation, children ...*core.RelationTupleTreeNode) *core.RelationTupleTreeNode {
    44  	return setResult(core.SetOperationUserset_INTERSECTION, start, children)
    45  }
    46  
    47  // Exclusion constructs a RelationTupleTreeNode exclusion operation.
    48  func Exclusion(start *core.ObjectAndRelation, children ...*core.RelationTupleTreeNode) *core.RelationTupleTreeNode {
    49  	return setResult(core.SetOperationUserset_EXCLUSION, start, children)
    50  }