-
Notifications
You must be signed in to change notification settings - Fork 126
/
collections.go
166 lines (138 loc) · 4.35 KB
/
collections.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package database
import (
base "github.com/Permify/permify/pkg/pb/base/v1"
)
// TupleCollection -Tuple collection.
type TupleCollection struct {
tuples []*base.Tuple
}
// NewTupleCollection - Create new tuple collection.
func NewTupleCollection(tuples ...*base.Tuple) *TupleCollection {
if len(tuples) == 0 {
return &TupleCollection{}
}
return &TupleCollection{
tuples: tuples,
}
}
// CreateTupleIterator - Create tuple iterator according to collection.
func (t *TupleCollection) CreateTupleIterator() *TupleIterator {
return &TupleIterator{
tuples: t.tuples,
}
}
// GetTuples - Get tuples
func (t *TupleCollection) GetTuples() []*base.Tuple {
return t.tuples
}
// Add - New subject to collection.
func (t *TupleCollection) Add(tuple *base.Tuple) {
t.tuples = append(t.tuples, tuple)
}
// ToSubjectCollection - Converts new subject collection from given tuple collection
func (t *TupleCollection) ToSubjectCollection() *SubjectCollection {
subjects := make([]*base.Subject, len(t.tuples))
for index, tuple := range t.tuples {
subjects[index] = tuple.GetSubject()
}
return NewSubjectCollection(subjects...)
}
// SUBJECT
// SubjectCollection - Subject collection.
type SubjectCollection struct {
subjects []*base.Subject
}
// NewSubjectCollection - Create new subject collection.
func NewSubjectCollection(subjects ...*base.Subject) *SubjectCollection {
if len(subjects) == 0 {
return &SubjectCollection{}
}
return &SubjectCollection{
subjects: subjects,
}
}
// CreateSubjectIterator - Create subject iterator according to collection.
func (s *SubjectCollection) CreateSubjectIterator() *SubjectIterator {
return &SubjectIterator{
subjects: s.subjects,
}
}
// GetSubjects - Get subject collection
func (s *SubjectCollection) GetSubjects() []*base.Subject {
return s.subjects
}
// Add - New subject to collection.
func (s *SubjectCollection) Add(subject *base.Subject) {
s.subjects = append(s.subjects, subject)
}
// ENTITY
// EntityCollection - Entity collection.
type EntityCollection struct {
entities []*base.Entity
}
// NewEntityCollection - Create new subject collection.
func NewEntityCollection(entities ...*base.Entity) *EntityCollection {
if len(entities) == 0 {
return &EntityCollection{}
}
return &EntityCollection{
entities: entities,
}
}
// CreateEntityIterator - Create entity iterator according to collection.
func (e *EntityCollection) CreateEntityIterator() *EntityIterator {
return &EntityIterator{
entities: e.entities,
}
}
// GetEntities - Get entities
func (e *EntityCollection) GetEntities() []*base.Entity {
return e.entities
}
// Add - New subject to collection.
func (e *EntityCollection) Add(entity *base.Entity) {
e.entities = append(e.entities, entity)
}
// AttributeCollection -Attribute collection.
type AttributeCollection struct {
attributes []*base.Attribute
}
// NewAttributeCollection - Create new attribute collection.
func NewAttributeCollection(attributes ...*base.Attribute) *AttributeCollection {
if len(attributes) == 0 {
return &AttributeCollection{}
}
return &AttributeCollection{
attributes: attributes,
}
}
// GetAttributes - Get entities
func (t *AttributeCollection) GetAttributes() []*base.Attribute {
return t.attributes
}
// CreateAttributeIterator - Create tuple iterator according to collection.
func (t *AttributeCollection) CreateAttributeIterator() *AttributeIterator {
return &AttributeIterator{
attributes: t.attributes,
}
}
// Add - New subject to collection.
func (t *AttributeCollection) Add(attribute *base.Attribute) {
t.attributes = append(t.attributes, attribute)
}
// TupleBundle defines a structure for managing collections of tuples,
// with separate collections for write (create/update) and delete operations.
type TupleBundle struct {
// Write is a TupleCollection intended to hold tuples that are to be created or updated.
Write TupleCollection
// Delete is a TupleCollection intended to hold tuples that are to be deleted.
Delete TupleCollection
}
// AttributeBundle defines a structure for managing collections of attributes,
// with separate collections for write (create/update) and delete operations.
type AttributeBundle struct {
// Write is an AttributeCollection intended to hold attributes that are to be created or updated.
Write AttributeCollection
// Delete is an AttributeCollection intended to hold attributes that are to be deleted.
Delete AttributeCollection
}