github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/testutil/tuples.go (about) 1 package testutil 2 3 import ( 4 "fmt" 5 6 "golang.org/x/exp/slices" 7 8 core "github.com/authzed/spicedb/pkg/proto/core/v1" 9 "github.com/authzed/spicedb/pkg/tuple" 10 ) 11 12 var ONR = tuple.ObjectAndRelation 13 14 func JoinTuples(first []*core.RelationTuple, others ...[]*core.RelationTuple) []*core.RelationTuple { 15 combined := slices.Clone(first) 16 for _, other := range others { 17 combined = append(combined, other...) 18 } 19 return combined 20 } 21 22 func GenTuplesWithOffset(resourceName string, relation string, subjectName string, subjectID string, offset int, number int) []*core.RelationTuple { 23 return GenTuplesWithCaveat(resourceName, relation, subjectName, subjectID, "", nil, offset, number) 24 } 25 26 func GenTuples(resourceName string, relation string, subjectName string, subjectID string, number int) []*core.RelationTuple { 27 return GenTuplesWithOffset(resourceName, relation, subjectName, subjectID, 0, number) 28 } 29 30 func GenResourceTuples(resourceName string, resourceID string, relation string, subjectName string, subjectRelation string, number int) []*core.RelationTuple { 31 return GenResourceTuplesWithOffset(resourceName, resourceID, relation, subjectName, subjectRelation, 0, number) 32 } 33 34 func GenResourceTuplesWithOffset(resourceName string, resourceID string, relation string, subjectName string, subjectRelation string, offset int, number int) []*core.RelationTuple { 35 tuples := make([]*core.RelationTuple, 0, number) 36 for i := 0; i < number; i++ { 37 tpl := &core.RelationTuple{ 38 ResourceAndRelation: ONR(resourceName, resourceID, relation), 39 Subject: ONR(subjectName, fmt.Sprintf("%s-%d", subjectName, i+offset), subjectRelation), 40 } 41 tuples = append(tuples, tpl) 42 } 43 return tuples 44 } 45 46 func GenResourceTuplesWithCaveat(resourceName string, resourceID string, relation string, subjectName string, subjectRelation string, caveatName string, context map[string]any, number int) []*core.RelationTuple { 47 tuples := make([]*core.RelationTuple, 0, number) 48 for i := 0; i < number; i++ { 49 tpl := &core.RelationTuple{ 50 ResourceAndRelation: ONR(resourceName, resourceID, relation), 51 Subject: ONR(subjectName, fmt.Sprintf("%s-%d", subjectName, i), subjectRelation), 52 } 53 if caveatName != "" { 54 tpl = tuple.MustWithCaveat(tpl, caveatName, context) 55 } 56 tuples = append(tuples, tpl) 57 } 58 return tuples 59 } 60 61 func GenSubjectTuples(resourceName string, relation string, subjectName string, subjectRelation string, number int) []*core.RelationTuple { 62 tuples := make([]*core.RelationTuple, 0, number) 63 for i := 0; i < number; i++ { 64 tpl := &core.RelationTuple{ 65 ResourceAndRelation: ONR(resourceName, fmt.Sprintf("%s-%d", resourceName, i), relation), 66 Subject: ONR(subjectName, fmt.Sprintf("%s-%d", subjectName, i), subjectRelation), 67 } 68 tuples = append(tuples, tpl) 69 } 70 return tuples 71 } 72 73 func GenSubjectIdsWithOffset(subjectName string, offset int, number int) []string { 74 subjectIDs := make([]string, 0, number) 75 for i := 0; i < number; i++ { 76 subjectIDs = append(subjectIDs, fmt.Sprintf("%s-%d", subjectName, i+offset)) 77 } 78 return subjectIDs 79 } 80 81 func GenSubjectIds(subjectName string, number int) []string { 82 return GenSubjectIdsWithOffset(subjectName, 0, number) 83 } 84 85 func GenTuplesWithCaveat(resourceName string, relation string, subjectName string, subjectID string, caveatName string, context map[string]any, offset int, number int) []*core.RelationTuple { 86 tuples := make([]*core.RelationTuple, 0, number) 87 for i := 0; i < number; i++ { 88 tpl := &core.RelationTuple{ 89 ResourceAndRelation: ONR(resourceName, fmt.Sprintf("%s-%d", resourceName, i+offset), relation), 90 Subject: ONR(subjectName, subjectID, "..."), 91 } 92 if caveatName != "" { 93 tpl = tuple.MustWithCaveat(tpl, caveatName, context) 94 } 95 tuples = append(tuples, tpl) 96 } 97 return tuples 98 } 99 100 func GenResourceIds(resourceName string, number int) []string { 101 resourceIDs := make([]string, 0, number) 102 for i := 0; i < number; i++ { 103 resourceIDs = append(resourceIDs, fmt.Sprintf("%s-%d", resourceName, i)) 104 } 105 return resourceIDs 106 }