-
Notifications
You must be signed in to change notification settings - Fork 245
/
onrbytypeset.go
74 lines (64 loc) · 2.04 KB
/
onrbytypeset.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
package tuple
import (
"fmt"
"strings"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
)
// ONRByTypeSet is a set of ObjectAndRelation's, grouped by namespace+relation.
type ONRByTypeSet struct {
byType map[string][]string
}
// NewONRByTypeSet creates and returns a new ONRByTypeSet.
func NewONRByTypeSet() *ONRByTypeSet {
return &ONRByTypeSet{
byType: map[string][]string{},
}
}
// Add adds the specified ObjectAndRelation to the set.
func (s *ONRByTypeSet) Add(onr *core.ObjectAndRelation) {
typeKey := fmt.Sprintf("%s#%s", onr.Namespace, onr.Relation)
if _, ok := s.byType[typeKey]; !ok {
s.byType[typeKey] = []string{}
}
s.byType[typeKey] = append(s.byType[typeKey], onr.ObjectId)
}
// ForEachType invokes the handler for each type of ObjectAndRelation found in the set, along
// with all IDs of objects of that type.
func (s *ONRByTypeSet) ForEachType(handler func(rr *core.RelationReference, objectIds []string)) {
for key, objectIds := range s.byType {
parts := strings.Split(key, "#")
handler(&core.RelationReference{
Namespace: parts[0],
Relation: parts[1],
}, objectIds)
}
}
// Map runs the mapper function over each type of object in the set, returning a new ONRByTypeSet with
// the object type replaced by that returned by the mapper function.
func (s *ONRByTypeSet) Map(mapper func(rr *core.RelationReference) (*core.RelationReference, error)) (*ONRByTypeSet, error) {
mapped := NewONRByTypeSet()
for key, objectIds := range s.byType {
parts := strings.Split(key, "#")
updatedType, err := mapper(&core.RelationReference{
Namespace: parts[0],
Relation: parts[1],
})
if err != nil {
return nil, err
}
if updatedType == nil {
continue
}
updatedTypeKey := fmt.Sprintf("%s#%s", updatedType.Namespace, updatedType.Relation)
mapped.byType[updatedTypeKey] = objectIds
}
return mapped, nil
}
// IsEmpty returns true if the set is empty.
func (s *ONRByTypeSet) IsEmpty() bool {
return len(s.byType) == 0
}
// Len returns the number of keys in the set.
func (s *ONRByTypeSet) Len() int {
return len(s.byType)
}