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