github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/proto/core/v1/util.go (about) 1 package corev1 2 3 import ( 4 v0 "github.com/authzed/authzed-go/proto/authzed/api/v0" 5 ) 6 7 // ToCoreRelationTuple converts the input to a core RelationTuple. 8 func ToCoreRelationTuple(tuple *v0.RelationTuple) *RelationTuple { 9 return &RelationTuple{ 10 ResourceAndRelation: ToCoreObjectAndRelation(tuple.ObjectAndRelation), 11 Subject: ToCoreObjectAndRelation(tuple.User.GetUserset()), 12 } 13 } 14 15 // ToCoreRelationTuples converts the input slice elements to core RelationTuple. 16 func ToCoreRelationTuples(tuples []*v0.RelationTuple) []*RelationTuple { 17 coreTuples := make([]*RelationTuple, len(tuples)) 18 for i, elem := range tuples { 19 coreTuples[i] = ToCoreRelationTuple(elem) 20 } 21 return coreTuples 22 } 23 24 // ToCoreObjectAndRelation converts the input to a core ToCoreObjectAndRelation. 25 func ToCoreObjectAndRelation(onr *v0.ObjectAndRelation) *ObjectAndRelation { 26 return &ObjectAndRelation{ 27 Namespace: onr.Namespace, 28 ObjectId: onr.ObjectId, 29 Relation: onr.Relation, 30 } 31 } 32 33 // ToCoreRelationReference converts the input to a core ToCoreRelationReference. 34 func ToCoreRelationReference(ref *v0.RelationReference) *RelationReference { 35 return &RelationReference{ 36 Namespace: ref.Namespace, 37 Relation: ref.Relation, 38 } 39 } 40 41 // ToV0RelationTuple converts the input to a v0 ToV0RelationTuple. 42 func ToV0RelationTuple(tuple *RelationTuple) *v0.RelationTuple { 43 return &v0.RelationTuple{ 44 ObjectAndRelation: &v0.ObjectAndRelation{ 45 Namespace: tuple.ResourceAndRelation.Namespace, 46 ObjectId: tuple.ResourceAndRelation.ObjectId, 47 Relation: tuple.ResourceAndRelation.Relation, 48 }, 49 User: &v0.User{ 50 UserOneof: &v0.User_Userset{ 51 Userset: &v0.ObjectAndRelation{ 52 Namespace: tuple.Subject.Namespace, 53 ObjectId: tuple.Subject.ObjectId, 54 Relation: tuple.Subject.Relation, 55 }, 56 }, 57 }, 58 } 59 } 60 61 // ToV0RelationTuples converts the input slice elements to v0 RelationTuple. 62 func ToV0RelationTuples(tuples []*RelationTuple) []*v0.RelationTuple { 63 v0Tuples := make([]*v0.RelationTuple, len(tuples)) 64 for i, elem := range tuples { 65 v0Tuples[i] = ToV0RelationTuple(elem) 66 } 67 return v0Tuples 68 }