1
|
|
|
package ast |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"strings" |
5
|
|
|
|
6
|
|
|
"github.com/Permify/permify/pkg/dsl/token" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
type ( |
10
|
|
|
StatementType string |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
const ( |
14
|
|
|
PERMISSION_STATEMENT StatementType = "permission" |
15
|
|
|
RELATION_STATEMENT StatementType = "relation" |
16
|
|
|
ATTRIBUTE_STATEMENT StatementType = "attribute" |
17
|
|
|
ENTITY_STATEMENT StatementType = "entity" |
18
|
|
|
RULE_STATEMENT StatementType = "rule" |
19
|
|
|
EXPRESSION_STATEMENT StatementType = "expression" |
20
|
|
|
RELATION_TYPE_STATEMENT StatementType = "relation_type" |
21
|
|
|
ATTRIBUTE_TYPE_STATEMENT StatementType = "attribute_type" |
22
|
|
|
) |
23
|
|
|
|
24
|
|
|
// Statement defines an interface for a statement node. |
25
|
|
|
type Statement interface { |
26
|
|
|
Node |
27
|
|
|
statementNode() |
28
|
|
|
GetName() string |
29
|
|
|
StatementType() StatementType |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// EntityStatement represents a statement that refers to an entity. |
33
|
|
|
type EntityStatement struct { |
34
|
|
|
Entity token.Token // token.ENTITY |
35
|
|
|
Name token.Token // token.IDENT |
36
|
|
|
RelationStatements []Statement // Statements that define relationships between entities |
37
|
|
|
AttributeStatements []Statement // Statements that define attributes of the entity |
38
|
|
|
PermissionStatements []Statement // Statements that define permissions performed on the entity |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// statementNode is a dummy method that satisfies the Statement interface. |
42
|
|
|
func (ls *EntityStatement) statementNode() {} |
43
|
|
|
|
44
|
|
|
// String returns a string representation of the EntityStatement. |
45
|
|
|
func (ls *EntityStatement) String() string { |
46
|
|
|
var sb strings.Builder |
47
|
|
|
sb.WriteString("entity") |
48
|
|
|
sb.WriteString(" ") |
49
|
|
|
sb.WriteString(ls.Name.Literal) |
50
|
|
|
sb.WriteString(" {") |
51
|
|
|
sb.WriteString("\n") |
52
|
|
|
|
53
|
|
|
// Iterate over the relation statements and add them to the string builder. |
54
|
|
|
for _, rs := range ls.RelationStatements { |
55
|
|
|
sb.WriteString(rs.String()) |
56
|
|
|
sb.WriteString("\n") |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
sb.WriteString("\n") |
60
|
|
|
|
61
|
|
|
// Iterate over the attribute statements and add them to the string builder. |
62
|
|
|
for _, as := range ls.AttributeStatements { |
63
|
|
|
sb.WriteString(as.String()) |
64
|
|
|
sb.WriteString("\n") |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
sb.WriteString("\n") |
68
|
|
|
|
69
|
|
|
// Iterate over the permission statements and add them to the string builder. |
70
|
|
|
for _, ps := range ls.PermissionStatements { |
71
|
|
|
sb.WriteString(ps.String()) |
72
|
|
|
sb.WriteString("\n") |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
sb.WriteString("}") |
76
|
|
|
sb.WriteString(" ") |
77
|
|
|
sb.WriteString("\n") |
78
|
|
|
|
79
|
|
|
// Return the final string. |
80
|
|
|
return sb.String() |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
func (ls *EntityStatement) GetName() string { |
84
|
|
|
return ls.Name.Literal |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
func (ls *EntityStatement) StatementType() StatementType { |
88
|
|
|
return ENTITY_STATEMENT |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// AttributeStatement represents a statement that defines an attribute of an entity. |
92
|
|
|
type AttributeStatement struct { |
93
|
|
|
Attribute token.Token // token.ATTRIBUTE |
94
|
|
|
Name token.Token // token.IDENT |
95
|
|
|
AttributeType AttributeTypeStatement |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// statementNode is a dummy method that satisfies the Statement interface. |
99
|
|
|
func (as *AttributeStatement) statementNode() {} |
100
|
|
|
|
101
|
|
|
// String returns a string representation of the AttributeStatement. |
102
|
|
|
func (as *AttributeStatement) String() string { |
103
|
|
|
var sb strings.Builder |
104
|
|
|
sb.WriteString("\t") |
105
|
|
|
sb.WriteString("attribute") |
106
|
|
|
sb.WriteString(" ") |
107
|
|
|
sb.WriteString(as.Name.Literal) |
108
|
|
|
sb.WriteString(" ") |
109
|
|
|
sb.WriteString(as.AttributeType.String()) |
110
|
|
|
|
111
|
|
|
// Return the final string. |
112
|
|
|
return sb.String() |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
func (as *AttributeStatement) GetName() string { |
116
|
|
|
return as.Name.Literal |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
func (as *AttributeStatement) StatementType() StatementType { |
120
|
|
|
return ATTRIBUTE_STATEMENT |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// RelationStatement represents a statement that defines a relationship between two entities. |
124
|
|
|
type RelationStatement struct { |
125
|
|
|
Relation token.Token // token.RELATION |
126
|
|
|
Name token.Token // token.IDENT |
127
|
|
|
RelationTypes []RelationTypeStatement // Statements that define the types of the relationship |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// statementNode is a dummy method that satisfies the Statement interface. |
131
|
|
|
func (ls *RelationStatement) statementNode() {} |
132
|
|
|
|
133
|
|
|
// String returns a string representation of the RelationStatement. |
134
|
|
|
func (ls *RelationStatement) String() string { |
135
|
|
|
var sb strings.Builder |
136
|
|
|
sb.WriteString("\t") |
137
|
|
|
sb.WriteString("relation") |
138
|
|
|
sb.WriteString(" ") |
139
|
|
|
sb.WriteString(ls.Name.Literal) |
140
|
|
|
sb.WriteString(" ") |
141
|
|
|
|
142
|
|
|
// Iterate over the relation types and append them to the string builder. |
143
|
|
|
for _, rs := range ls.RelationTypes { |
144
|
|
|
sb.WriteString(rs.String()) |
145
|
|
|
sb.WriteString(" ") |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Return the final string. |
149
|
|
|
return sb.String() |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
func (ls *RelationStatement) GetName() string { |
153
|
|
|
return ls.Name.Literal |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
func (ls *RelationStatement) StatementType() StatementType { |
157
|
|
|
return RELATION_STATEMENT |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// AttributeTypeStatement represents a statement that defines the type of a relationship. |
161
|
|
|
type AttributeTypeStatement struct { |
162
|
|
|
Type token.Token // token.IDENT |
163
|
|
|
IsArray bool |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// String returns a string representation of the RelationTypeStatement. |
167
|
|
|
func (as *AttributeTypeStatement) String() string { |
168
|
|
|
var sb strings.Builder |
169
|
|
|
sb.WriteString(as.Type.Literal) |
170
|
|
|
if as.IsArray { |
171
|
|
|
sb.WriteString("[]") |
172
|
|
|
} |
173
|
|
|
return sb.String() |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
func (as *AttributeTypeStatement) GetName() string { |
177
|
|
|
return "" |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
func (as *AttributeTypeStatement) StatementType() StatementType { |
181
|
|
|
return ATTRIBUTE_TYPE_STATEMENT |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
func (as *AttributeTypeStatement) statementNode() {} |
185
|
|
|
|
186
|
|
|
// RelationTypeStatement represents a statement that defines the type of relationship. |
187
|
|
|
type RelationTypeStatement struct { |
188
|
|
|
Sign token.Token // token.SIGN |
189
|
|
|
Type token.Token // token.IDENT |
190
|
|
|
Relation token.Token // token.IDENT |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// String returns a string representation of the RelationTypeStatement. |
194
|
|
|
func (ls *RelationTypeStatement) String() string { |
195
|
|
|
var sb strings.Builder |
196
|
|
|
sb.WriteString("@") |
197
|
|
|
sb.WriteString(ls.Type.Literal) |
198
|
|
|
if ls.Relation.Literal != "" { |
199
|
|
|
sb.WriteString("#") |
200
|
|
|
sb.WriteString(ls.Relation.Literal) |
201
|
|
|
} |
202
|
|
|
return sb.String() |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
func (ls *RelationTypeStatement) GetName() string { |
206
|
|
|
return "" |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
func (ls *RelationTypeStatement) StatementType() StatementType { |
210
|
|
|
return RELATION_TYPE_STATEMENT |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
func (ls *RelationTypeStatement) statementNode() {} |
214
|
|
|
|
215
|
|
|
// IsDirectEntityReference returns true if the RelationTypeStatement is a direct entity reference. |
216
|
|
|
func IsDirectEntityReference(s RelationTypeStatement) bool { |
217
|
|
|
return s.Relation.Literal == "" |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// PermissionStatement represents an permission statement, which consists of an permission name and an optional expression statement. |
221
|
|
|
// It implements the Statement interface. |
222
|
|
|
type PermissionStatement struct { |
223
|
|
|
Permission token.Token // token.PERMISSION |
224
|
|
|
Name token.Token // token.IDENT |
225
|
|
|
ExpressionStatement Statement |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// statementNode is a marker method used to implement the Statement interface. |
229
|
|
|
func (ls *PermissionStatement) statementNode() {} |
230
|
|
|
|
231
|
|
|
// String returns a string representation of the permission statement. |
232
|
|
|
func (ls *PermissionStatement) String() string { |
233
|
|
|
var sb strings.Builder |
234
|
|
|
sb.WriteString("\t") |
235
|
|
|
sb.WriteString("permission") |
236
|
|
|
sb.WriteString(" ") |
237
|
|
|
sb.WriteString(ls.Name.Literal) |
238
|
|
|
sb.WriteString(" = ") |
239
|
|
|
if ls.ExpressionStatement != nil { |
240
|
|
|
sb.WriteString(ls.ExpressionStatement.String()) |
241
|
|
|
} |
242
|
|
|
return sb.String() |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
func (ls *PermissionStatement) GetName() string { |
246
|
|
|
return ls.Name.Literal |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
func (ls *PermissionStatement) StatementType() StatementType { |
250
|
|
|
return PERMISSION_STATEMENT |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
// ExpressionStatement struct represents an expression statement |
254
|
|
|
type ExpressionStatement struct { |
255
|
|
|
Expression Expression |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
// statementNode function is needed to mark the struct as a Statement node |
259
|
|
|
func (es *ExpressionStatement) statementNode() {} |
260
|
|
|
|
261
|
|
|
// String function returns a string representation of the ExpressionStatement |
262
|
|
|
func (es *ExpressionStatement) String() string { |
263
|
|
|
if es.Expression != nil { |
264
|
|
|
return es.Expression.String() |
265
|
|
|
} |
266
|
|
|
// If there is no expression, return an empty string |
267
|
|
|
return "" |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
func (es *ExpressionStatement) GetName() string { |
271
|
|
|
return "" |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
func (es *ExpressionStatement) StatementType() StatementType { |
275
|
|
|
return EXPRESSION_STATEMENT |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
// RuleStatement represents a rule statement, which consists of a rule name, a list of parameters and a body. |
279
|
|
|
type RuleStatement struct { |
280
|
|
|
Rule token.Token // token.RULE |
281
|
|
|
Name token.Token // token.IDENT |
282
|
|
|
Arguments map[token.Token]AttributeTypeStatement |
283
|
|
|
Expression string |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// statementNode is a marker method used to implement the Statement interface. |
287
|
|
|
func (rs *RuleStatement) statementNode() {} |
288
|
|
|
|
289
|
|
|
// String returns a string representation of the permission statement. |
290
|
|
|
func (rs *RuleStatement) String() string { |
291
|
|
|
var sb strings.Builder |
292
|
|
|
sb.WriteString("rule") |
293
|
|
|
sb.WriteString(" ") |
294
|
|
|
sb.WriteString(rs.Name.Literal) |
295
|
|
|
sb.WriteString("(") |
296
|
|
|
|
297
|
|
|
var literals []string |
298
|
|
|
for param, typ := range rs.Arguments { |
299
|
|
|
var pb strings.Builder |
300
|
|
|
pb.WriteString(param.Literal) |
301
|
|
|
pb.WriteString(" ") |
302
|
|
|
pb.WriteString(typ.Type.Literal) |
303
|
|
|
if typ.IsArray { |
304
|
|
|
pb.WriteString("[]") |
305
|
|
|
} |
306
|
|
|
literals = append(literals, pb.String()) |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
sb.WriteString(strings.Join(literals, ", ")) |
310
|
|
|
|
311
|
|
|
sb.WriteString(")") |
312
|
|
|
sb.WriteString(" ") |
313
|
|
|
sb.WriteString("{") |
314
|
|
|
|
315
|
|
|
sb.WriteString("\n") |
316
|
|
|
sb.WriteString("\t") |
317
|
|
|
sb.WriteString(rs.Expression) |
318
|
|
|
sb.WriteString("\n") |
319
|
|
|
|
320
|
|
|
sb.WriteString("}") |
321
|
|
|
return sb.String() |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
func (rs *RuleStatement) GetName() string { |
325
|
|
|
return rs.Name.Literal |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
func (rs *RuleStatement) StatementType() StatementType { |
329
|
|
|
return RULE_STATEMENT |
330
|
|
|
} |
331
|
|
|
|