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

     1  package tuple
     2  
     3  import (
     4  	v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
     5  )
     6  
     7  // JoinObject joins the namespace and the objectId together into the standard
     8  // format.
     9  //
    10  // This function assumes that the provided values have already been validated.
    11  func JoinObjectRef(namespace, objectID string) string { return namespace + ":" + objectID }
    12  
    13  // StringObjectRef marshals a *v1.ObjectReference into a string.
    14  //
    15  // This function assumes that the provided values have already been validated.
    16  func StringObjectRef(ref *v1.ObjectReference) string {
    17  	return JoinObjectRef(ref.ObjectType, ref.ObjectId)
    18  }
    19  
    20  // StringSubjectRef marshals a *v1.SubjectReference into a string.
    21  //
    22  // This function assumes that the provided values have already been validated.
    23  func StringSubjectRef(ref *v1.SubjectReference) string {
    24  	if ref.OptionalRelation == "" {
    25  		return StringObjectRef(ref.Object)
    26  	}
    27  	return JoinRelRef(StringObjectRef(ref.Object), ref.OptionalRelation)
    28  }
    29  
    30  // MustStringRelationship converts a v1.Relationship to a string.
    31  func MustStringRelationship(rel *v1.Relationship) string {
    32  	relString, err := StringRelationship(rel)
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  	return relString
    37  }
    38  
    39  // StringRelationship converts a v1.Relationship to a string.
    40  func StringRelationship(rel *v1.Relationship) (string, error) {
    41  	if rel == nil || rel.Resource == nil || rel.Subject == nil {
    42  		return "", nil
    43  	}
    44  
    45  	caveatString, err := StringCaveatRef(rel.OptionalCaveat)
    46  	if err != nil {
    47  		return "", err
    48  	}
    49  
    50  	return StringRelationshipWithoutCaveat(rel) + caveatString, nil
    51  }
    52  
    53  // StringRelationshipWithoutCaveat converts a v1.Relationship to a string, excluding any caveat.
    54  func StringRelationshipWithoutCaveat(rel *v1.Relationship) string {
    55  	if rel == nil || rel.Resource == nil || rel.Subject == nil {
    56  		return ""
    57  	}
    58  
    59  	return StringObjectRef(rel.Resource) + "#" + rel.Relation + "@" + StringSubjectRef(rel.Subject)
    60  }
    61  
    62  // StringCaveatRef converts a v1.ContextualizedCaveat to a string.
    63  func StringCaveatRef(caveat *v1.ContextualizedCaveat) (string, error) {
    64  	if caveat == nil || caveat.CaveatName == "" {
    65  		return "", nil
    66  	}
    67  
    68  	contextString, err := StringCaveatContext(caveat.Context)
    69  	if err != nil {
    70  		return "", err
    71  	}
    72  
    73  	if len(contextString) > 0 {
    74  		contextString = ":" + contextString
    75  	}
    76  
    77  	return "[" + caveat.CaveatName + contextString + "]", nil
    78  }