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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
package namespace
import (
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/spiceerrors"
)
// ExpressionChangeType defines the type of expression changes.
type ExpressionChangeType string
const (
// ExpressionUnchanged indicates that the expression was unchanged.
ExpressionUnchanged ExpressionChangeType = "expression-unchanged"
// ExpressionOperationChanged indicates that the operation type of the expression was changed.
ExpressionOperationChanged ExpressionChangeType = "operation-changed"
// ExpressionChildrenChanged indicates that the children of the expression were changed.
ExpressionChildrenChanged ExpressionChangeType = "children-changed"
// ExpressionOperationExpanded indicates that the operation type of the expression was expanded
// from a union of a single child to multiple children under a union, intersection or another
// operation.
ExpressionOperationExpanded ExpressionChangeType = "operation-expanded"
)
// ExpressionDiff holds the diff between two expressions.
type ExpressionDiff struct {
existing *core.UsersetRewrite
updated *core.UsersetRewrite
change ExpressionChangeType
childDiffs []*OperationDiff
}
// Existing returns the existing expression, if any.
func (ed *ExpressionDiff) Existing() *core.UsersetRewrite {
return ed.existing
}
// Updated returns the updated expression, if any.
func (ed *ExpressionDiff) Updated() *core.UsersetRewrite {
return ed.updated
}
// Change returns the type of change that occurred.
func (ed *ExpressionDiff) Change() ExpressionChangeType {
return ed.change
}
// ChildDiffs returns the child diffs, if any.
func (ed *ExpressionDiff) ChildDiffs() []*OperationDiff {
return ed.childDiffs
}
// SetOperationChangeType defines the type of set operation changes.
type SetOperationChangeType string
const (
// OperationUnchanged indicates that the set operation was unchanged.
OperationUnchanged SetOperationChangeType = "operation-changed"
// OperationAdded indicates that a set operation was added.
OperationAdded SetOperationChangeType = "operation-added"
// OperationRemoved indicates that a set operation was removed.
OperationRemoved SetOperationChangeType = "operation-removed"
// OperationTypeChanged indicates that the type of set operation was changed.
OperationTypeChanged SetOperationChangeType = "operation-type-changed"
// OperationComputedUsersetChanged indicates that the computed userset of the operation was changed.
OperationComputedUsersetChanged SetOperationChangeType = "operation-computed-userset-changed"
// OperationTuplesetChanged indicates that the tupleset of the operation was changed.
OperationTuplesetChanged SetOperationChangeType = "operation-tupleset-changed"
// OperationChildExpressionChanged indicates that the child expression of the operation was changed.
OperationChildExpressionChanged SetOperationChangeType = "operation-child-expression-changed"
)
// OperationDiff holds the diff between two set operations.
type OperationDiff struct {
existing *core.SetOperation_Child
updated *core.SetOperation_Child
change SetOperationChangeType
childExprDiff *ExpressionDiff
}
// Existing returns the existing set operation, if any.
func (od *OperationDiff) Existing() *core.SetOperation_Child {
return od.existing
}
// Updated returns the updated set operation, if any.
func (od *OperationDiff) Updated() *core.SetOperation_Child {
return od.updated
}
// Change returns the type of change that occurred.
func (od *OperationDiff) Change() SetOperationChangeType {
return od.change
}
// ChildExpressionDiff returns the child expression diff, if any.
func (od *OperationDiff) ChildExpressionDiff() *ExpressionDiff {
return od.childExprDiff
}
// DiffExpressions diffs two expressions.
func DiffExpressions(existing *core.UsersetRewrite, updated *core.UsersetRewrite) (*ExpressionDiff, error) {
// Check for a difference in the operation type.
var existingType string
var existingOperation *core.SetOperation
var updatedType string
var updatedOperation *core.SetOperation
switch t := existing.RewriteOperation.(type) {
case *core.UsersetRewrite_Union:
existingType = "union"
existingOperation = t.Union
case *core.UsersetRewrite_Intersection:
existingType = "intersection"
existingOperation = t.Intersection
case *core.UsersetRewrite_Exclusion:
existingType = "exclusion"
existingOperation = t.Exclusion
default:
return nil, spiceerrors.MustBugf("unknown operation type %T", existing.RewriteOperation)
}
switch t := updated.RewriteOperation.(type) {
case *core.UsersetRewrite_Union:
updatedType = "union"
updatedOperation = t.Union
case *core.UsersetRewrite_Intersection:
updatedType = "intersection"
updatedOperation = t.Intersection
case *core.UsersetRewrite_Exclusion:
updatedType = "exclusion"
updatedOperation = t.Exclusion
default:
return nil, spiceerrors.MustBugf("unknown operation type %T", updated.RewriteOperation)
}
childChangeKind := ExpressionChildrenChanged
if existingType != updatedType {
// If the expression has changed from a union with a single child, then
// treat this as a special case, since there wasn't really an operation
// before.
if existingType != "union" || len(existingOperation.Child) != 1 {
return &ExpressionDiff{
existing: existing,
updated: updated,
change: ExpressionOperationChanged,
}, nil
}
childChangeKind = ExpressionOperationExpanded
}
childDiffs := make([]*OperationDiff, 0, abs(len(updatedOperation.Child)-len(existingOperation.Child)))
if len(existingOperation.Child) < len(updatedOperation.Child) {
for _, updatedChild := range updatedOperation.Child[len(existingOperation.Child):] {
childDiffs = append(childDiffs, &OperationDiff{
change: OperationAdded,
updated: updatedChild,
})
}
}
if len(existingOperation.Child) > len(updatedOperation.Child) {
for _, existingChild := range existingOperation.Child[len(updatedOperation.Child):] {
childDiffs = append(childDiffs, &OperationDiff{
change: OperationRemoved,
existing: existingChild,
})
}
}
for i := 0; i < len(existingOperation.Child) && i < len(updatedOperation.Child); i++ {
childDiff, err := compareChildren(existingOperation.Child[i], updatedOperation.Child[i])
if err != nil {
return nil, err
}
if childDiff.change != OperationUnchanged {
childDiffs = append(childDiffs, childDiff)
}
}
if len(childDiffs) > 0 {
return &ExpressionDiff{
existing: existing,
updated: updated,
change: childChangeKind,
childDiffs: childDiffs,
}, nil
}
return &ExpressionDiff{
existing: existing,
updated: updated,
change: ExpressionUnchanged,
}, nil
}
func abs(i int) int {
if i < 0 {
return -i
}
return i
}
func compareChildren(existing *core.SetOperation_Child, updated *core.SetOperation_Child) (*OperationDiff, error) {
existingType, err := typeOfSetOperationChild(existing)
if err != nil {
return nil, err
}
updatedType, err := typeOfSetOperationChild(updated)
if err != nil {
return nil, err
}
if existingType != updatedType {
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationTypeChanged,
}, nil
}
switch existingType {
case "usersetrewrite":
childDiff, err := DiffExpressions(existing.GetUsersetRewrite(), updated.GetUsersetRewrite())
if err != nil {
return nil, err
}
if childDiff.change != ExpressionUnchanged {
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationChildExpressionChanged,
childExprDiff: childDiff,
}, nil
}
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationUnchanged,
}, nil
case "computed":
if existing.GetComputedUserset().Relation != updated.GetComputedUserset().Relation {
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationComputedUsersetChanged,
}, nil
}
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationUnchanged,
}, nil
case "_this":
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationUnchanged,
}, nil
case "nil":
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationUnchanged,
}, nil
case "ttu":
existingTTU := existing.GetTupleToUserset()
updatedTTU := updated.GetTupleToUserset()
if existingTTU.GetComputedUserset().Relation != updatedTTU.GetComputedUserset().Relation {
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationComputedUsersetChanged,
}, nil
}
if existingTTU.Tupleset.Relation != updatedTTU.Tupleset.Relation {
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationTuplesetChanged,
}, nil
}
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationUnchanged,
}, nil
case "anyttu":
fallthrough
case "intersectionttu":
existingTTU := existing.GetFunctionedTupleToUserset()
updatedTTU := updated.GetFunctionedTupleToUserset()
if existingTTU.GetComputedUserset().Relation != updatedTTU.GetComputedUserset().Relation {
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationComputedUsersetChanged,
}, nil
}
if existingTTU.Tupleset.Relation != updatedTTU.Tupleset.Relation {
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationTuplesetChanged,
}, nil
}
return &OperationDiff{
existing: existing,
updated: updated,
change: OperationUnchanged,
}, nil
default:
return nil, spiceerrors.MustBugf("unknown child type %s", existingType)
}
}
func typeOfSetOperationChild(child *core.SetOperation_Child) (string, error) {
switch t := child.ChildType.(type) {
case *core.SetOperation_Child_XThis:
return "_this", nil
case *core.SetOperation_Child_ComputedUserset:
return "computed", nil
case *core.SetOperation_Child_UsersetRewrite:
return "usersetrewrite", nil
case *core.SetOperation_Child_TupleToUserset:
return "ttu", nil
case *core.SetOperation_Child_FunctionedTupleToUserset:
switch t.FunctionedTupleToUserset.Function {
case core.FunctionedTupleToUserset_FUNCTION_UNSPECIFIED:
return "", spiceerrors.MustBugf("function type unspecified")
case core.FunctionedTupleToUserset_FUNCTION_ANY:
return "anyttu", nil
case core.FunctionedTupleToUserset_FUNCTION_ALL:
return "intersectionttu", nil
default:
return "", spiceerrors.MustBugf("unknown function type %v", t.FunctionedTupleToUserset.Function)
}
case *core.SetOperation_Child_XNil:
return "nil", nil
default:
return "", spiceerrors.MustBugf("unknown child type %T", t)
}
}
|