Passed
Push — master ( 496048...563bb3 )
by Tolga
01:01 queued 13s
created

ast.*References.UpdateAttributeReferences   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
package ast
2
3
import (
4
	"fmt"
5
)
6
7
type (
8
	// ReferenceType defines the type of reference.
9
	ReferenceType string
10
)
11
12
const (
13
	UNSPECIFIED ReferenceType = "unspecified"
14
	PERMISSION  ReferenceType = "permission"
15
	RELATION    ReferenceType = "relation"
16
	ATTRIBUTE   ReferenceType = "attribute"
17
	ENTITY      ReferenceType = "entity"
18
	RULE        ReferenceType = "rule"
19
)
20
21
// References - Map of all relational references extracted from the schema
22
type References struct {
23
	// Map of entity references extracted from the schema
24
	entityReferences map[string]struct{}
25
	// Map of rule references extracted from the schema
26
	ruleReferences map[string]map[string]string
27
28
	// Map of permission references extracted from the schema
29
	// -> ["entity_name#permission_name"] = {}
30
	permissionReferences map[string]struct{}
31
	// Map of attribute references extracted from the schema
32
	// -> ["entity_name#attribute_name"] = "type"
33
	attributeReferences map[string]AttributeTypeStatement
34
	// Map of relation references extracted from the schema
35
	// -> ["entity_name#relation_name"] = []{"type", "type#relation"}
36
	relationReferences map[string][]RelationTypeStatement
37
38
	// Map of all relational references extracted from the schema
39
	// its include relation, attribute and permission references
40
	references map[string]ReferenceType
41
}
42
43
// NewReferences creates a new instance of References
44
func NewReferences() *References {
45
	return &References{
46
		entityReferences:     map[string]struct{}{},
47
		ruleReferences:       map[string]map[string]string{},
48
		permissionReferences: map[string]struct{}{},
49
		attributeReferences:  map[string]AttributeTypeStatement{},
50
		relationReferences:   map[string][]RelationTypeStatement{},
51
		references:           map[string]ReferenceType{},
52
	}
53
}
54
55
// AddEntityReference sets a reference for an entity.
56
func (refs *References) AddEntityReference(name string) error {
57
	if len(name) == 0 {
58
		return fmt.Errorf("name cannot be empty")
59
	}
60
	if refs.IsReferenceExist(name) {
61
		return fmt.Errorf("reference %s already exists", name)
62
	}
63
	refs.entityReferences[name] = struct{}{}
64
	refs.references[name] = ENTITY
65
	return nil
66
}
67
68
// AddRuleReference sets a reference for a rule.
69
func (refs *References) AddRuleReference(name string, types map[string]string) error {
70
	if len(name) == 0 {
71
		return fmt.Errorf("name cannot be empty")
72
	}
73
	if refs.IsReferenceExist(name) {
74
		return fmt.Errorf("reference %s already exists", name)
75
	}
76
	refs.ruleReferences[name] = types
77
	refs.references[name] = RULE
78
	return nil
79
}
80
81
// AddRelationReferences sets references for a relation with its types.
82
func (refs *References) AddRelationReferences(key string, types []RelationTypeStatement) error {
83
	if len(key) == 0 {
84
		return fmt.Errorf("key cannot be empty")
85
	}
86
	if refs.IsReferenceExist(key) {
87
		return fmt.Errorf("reference %s already exists", key)
88
	}
89
	refs.relationReferences[key] = types
90
	refs.references[key] = RELATION
91
	return nil
92
}
93
94
// AddPermissionReference sets a reference for a permission.
95
func (refs *References) AddPermissionReference(key string) error {
96
	if len(key) == 0 {
97
		return fmt.Errorf("key cannot be empty")
98
	}
99
	if refs.IsReferenceExist(key) {
100
		return fmt.Errorf("reference %s already exists", key)
101
	}
102
	refs.permissionReferences[key] = struct{}{}
103
	refs.references[key] = PERMISSION
104
	return nil
105
}
106
107
// AddAttributeReferences sets references for an attribute with its type.
108
func (refs *References) AddAttributeReferences(key string, typ AttributeTypeStatement) error {
109
	if len(key) == 0 {
110
		return fmt.Errorf("key cannot be empty")
111
	}
112
	if refs.IsReferenceExist(key) {
113
		return fmt.Errorf("reference %s already exists", key)
114
	}
115
	refs.attributeReferences[key] = typ
116
	refs.references[key] = ATTRIBUTE
117
	return nil
118
}
119
120
// UpdateRelationReferences updates the relationship references for a given key.
121
// It replaces the existing relation types with the new ones provided.
122
// If the key is empty, it returns an error.
123
func (refs *References) UpdateRelationReferences(key string, types []RelationTypeStatement) error {
124
	if len(key) == 0 {
125
		return fmt.Errorf("key cannot be empty")
126
	}
127
	refs.relationReferences[key] = types // Update or set the relationship types for the key.
128
	refs.references[key] = RELATION      // Mark this key as a relation reference.
129
	return nil
130
}
131
132
// UpdatePermissionReference updates the permission references for a given key.
133
// This typically means marking a particular entity or action as permitted.
134
// If the key is empty, it returns an error.
135
func (refs *References) UpdatePermissionReference(key string) error {
136
	if len(key) == 0 {
137
		return fmt.Errorf("key cannot be empty")
138
	}
139
	refs.permissionReferences[key] = struct{}{} // Set the permission flag for the key.
140
	refs.references[key] = PERMISSION           // Mark this key as a permission reference.
141
	return nil
142
}
143
144
// UpdateAttributeReferences updates the attribute references for a given key.
145
// It associates a specific attribute type with the key.
146
// If the key is empty, it returns an error.
147
func (refs *References) UpdateAttributeReferences(key string, typ AttributeTypeStatement) error {
148
	if len(key) == 0 {
149
		return fmt.Errorf("key cannot be empty")
150
	}
151
	refs.attributeReferences[key] = typ // Set the attribute type for the key.
152
	refs.references[key] = ATTRIBUTE    // Mark this key as an attribute reference.
153
	return nil
154
}
155
156
// RemoveRelationReferences removes the relationship references associated with a given key.
157
// If the key is empty, it returns an error.
158
func (refs *References) RemoveRelationReferences(key string) error {
159
	if len(key) == 0 {
160
		return fmt.Errorf("key cannot be empty")
161
	}
162
	delete(refs.relationReferences, key) // Remove the relationship reference for the key.
163
	delete(refs.references, key)         // Remove the general reference.
164
	return nil
165
}
166
167
// RemovePermissionReference removes the permission references associated with a given key.
168
// If the key is empty, it returns an error.
169
func (refs *References) RemovePermissionReference(key string) error {
170
	if len(key) == 0 {
171
		return fmt.Errorf("key cannot be empty")
172
	}
173
	delete(refs.permissionReferences, key) // Remove the permission reference for the key.
174
	delete(refs.references, key)           // Remove the general reference.
175
	return nil
176
}
177
178
// RemoveAttributeReferences removes the attribute references associated with a given key.
179
// If the key is empty, it returns an error.
180
func (refs *References) RemoveAttributeReferences(key string) error {
181
	if len(key) == 0 {
182
		return fmt.Errorf("key cannot be empty")
183
	}
184
	delete(refs.attributeReferences, key) // Remove the attribute reference for the key.
185
	delete(refs.references, key)          // Remove the general reference.
186
	return nil
187
}
188
189
// GetReferenceType retrieves the type of the reference for a given key.
190
func (refs *References) GetReferenceType(key string) (ReferenceType, bool) {
191
	if _, ok := refs.references[key]; ok {
192
		return refs.references[key], true
193
	}
194
	return UNSPECIFIED, false
195
}
196
197
// IsEntityReferenceExist checks if an entity reference exists for the given name.
198
func (refs *References) IsEntityReferenceExist(name string) bool {
199
	if _, ok := refs.entityReferences[name]; ok {
200
		return ok
201
	}
202
	return false
203
}
204
205
// IsRelationReferenceExist checks if a relation reference exists for the given key.
206
func (refs *References) IsRelationReferenceExist(name string) bool {
207
	if _, ok := refs.relationReferences[name]; ok {
208
		return true
209
	}
210
	return false
211
}
212
213
// IsAttributeReferenceExist checks if an attribute reference exists for the given key.
214
func (refs *References) IsAttributeReferenceExist(name string) bool {
215
	if _, ok := refs.attributeReferences[name]; ok {
216
		return true
217
	}
218
	return false
219
}
220
221
// IsRuleReferenceExist checks if a rule reference exists for the given name.
222
func (refs *References) IsRuleReferenceExist(name string) bool {
223
	if _, ok := refs.ruleReferences[name]; ok {
224
		return true
225
	}
226
	return false
227
}
228
229
// IsReferenceExist checks if a reference exists for the given key.
230
func (refs *References) IsReferenceExist(name string) bool {
231
	if _, ok := refs.references[name]; ok {
232
		return true
233
	}
234
	return false
235
}
236
237
// GetAttributeReferenceTypeIfExist retrieves the attribute type for a given attribute reference key.
238
func (refs *References) GetAttributeReferenceTypeIfExist(name string) (AttributeTypeStatement, bool) {
239
	if _, ok := refs.attributeReferences[name]; ok {
240
		return refs.attributeReferences[name], true
241
	}
242
	return AttributeTypeStatement{}, false
243
}
244
245
// GetRelationReferenceTypesIfExist retrieves the relation types for a given relation reference key.
246
func (refs *References) GetRelationReferenceTypesIfExist(name string) ([]RelationTypeStatement, bool) {
247
	if _, ok := refs.relationReferences[name]; ok {
248
		return refs.relationReferences[name], true
249
	}
250
	return nil, false
251
}
252
253
// GetRuleArgumentTypesIfRuleExist retrieves the rule argument types for a given rule reference key.
254
func (refs *References) GetRuleArgumentTypesIfRuleExist(name string) (map[string]string, bool) {
255
	if _, ok := refs.ruleReferences[name]; ok {
256
		return refs.ruleReferences[name], true
257
	}
258
	return map[string]string{}, false
259
}
260