github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/datasets/subjectset.go (about) 1 package datasets 2 3 import ( 4 core "github.com/authzed/spicedb/pkg/proto/core/v1" 5 v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1" 6 ) 7 8 // SubjectSet defines a set that tracks accessible subjects. 9 // 10 // NOTE: Unlike a traditional set, unions between wildcards and a concrete subject will result 11 // in *both* being present in the set, to maintain the proper set semantics around wildcards. 12 type SubjectSet struct { 13 BaseSubjectSet[*v1.FoundSubject] 14 } 15 16 // NewSubjectSet creates and returns a new subject set. 17 func NewSubjectSet() SubjectSet { 18 return SubjectSet{ 19 BaseSubjectSet: NewBaseSubjectSet(subjectSetConstructor), 20 } 21 } 22 23 func (ss SubjectSet) SubtractAll(other SubjectSet) { 24 ss.BaseSubjectSet.SubtractAll(other.BaseSubjectSet) 25 } 26 27 func (ss SubjectSet) MustIntersectionDifference(other SubjectSet) { 28 ss.BaseSubjectSet.MustIntersectionDifference(other.BaseSubjectSet) 29 } 30 31 func (ss SubjectSet) IntersectionDifference(other SubjectSet) error { 32 return ss.BaseSubjectSet.IntersectionDifference(other.BaseSubjectSet) 33 } 34 35 func (ss SubjectSet) MustUnionWithSet(other SubjectSet) { 36 ss.BaseSubjectSet.MustUnionWithSet(other.BaseSubjectSet) 37 } 38 39 func (ss SubjectSet) Clone() SubjectSet { 40 return SubjectSet{ss.BaseSubjectSet.Clone()} 41 } 42 43 func (ss SubjectSet) UnionWithSet(other SubjectSet) error { 44 return ss.BaseSubjectSet.UnionWithSet(other.BaseSubjectSet) 45 } 46 47 // WithParentCaveatExpression returns a copy of the subject set with the parent caveat expression applied 48 // to all members of this set. 49 func (ss SubjectSet) WithParentCaveatExpression(parentCaveatExpr *core.CaveatExpression) SubjectSet { 50 return SubjectSet{ss.BaseSubjectSet.WithParentCaveatExpression(parentCaveatExpr)} 51 } 52 53 func (ss SubjectSet) AsFoundSubjects() *v1.FoundSubjects { 54 return &v1.FoundSubjects{ 55 FoundSubjects: ss.AsSlice(), 56 } 57 } 58 59 func subjectSetConstructor(subjectID string, caveatExpression *core.CaveatExpression, excludedSubjects []*v1.FoundSubject, _ ...*v1.FoundSubject) *v1.FoundSubject { 60 return &v1.FoundSubject{ 61 SubjectId: subjectID, 62 CaveatExpression: caveatExpression, 63 ExcludedSubjects: excludedSubjects, 64 } 65 }