-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
diff.go
443 lines (363 loc) · 8.58 KB
/
diff.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package datamodel
import (
"fmt"
"io"
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/ast/astutil"
"cuelang.org/go/cue/format"
"github.com/hofstadter-io/hof/cmd/hof/flags"
)
var fieldOptions = []cue.Option{
cue.Attributes(true),
cue.Concrete(false),
cue.Definitions(true),
cue.Hidden(true),
cue.Optional(true),
cue.Docs(true),
}
/*
func (dm *Datamodel) Diff() cue.Value {
return dm.T.diff
}
*/
func (V *Value) Diff() cue.Value {
return V.Snapshot.Lense.CurrDiff
}
func (dm *Datamodel) HasDiff() bool {
return dm.T.hasDiffR()
}
func (V *Value) hasDiffR() bool {
// load own history
if V.Hof.Datamodel.History {
if V.hasDiff() {
return true
}
}
// recurse if children to load any nested histories
for _, c := range V.Children {
if c.T.hasDiffR() {
return true
}
}
return false
}
func (V *Value) hasDiff() bool {
return V.Diff().Exists()
}
func (dm *Datamodel) CalcDiffs() error {
return dm.T.calcDiffR()
}
func (V *Value) calcDiffR() error {
// load own history
if V.Hof.Datamodel.History {
err := V.calcDiff()
if err != nil {
return err
}
}
// recurse if children to load any nested histories
for _, c := range V.Children {
err := c.T.calcDiffR()
if err != nil {
return err
}
}
return nil
}
func (V *Value) calcDiff() error {
// no history
if len(V.history) == 0 {
return nil
}
// curr & last vars
var cv, lv cue.Value
var cs, ls *Snapshot
cv = V.Value
cs = V.Snapshot
for _, S := range V.history {
ls, lv = S, S.Value
// finalize current value
node := cv.Syntax(
cue.Final(),
cue.Docs(true),
cue.Attributes(true),
cue.Definitions(true),
cue.Optional(true),
cue.Hidden(true),
cue.Concrete(true),
cue.ResolveReferences(true),
)
cv = cv.Context().BuildExpr(node.(*ast.StructLit))
// calculate CurrDiff
diff, err := DiffValue(lv, cv)
if err != nil {
return err
}
cs.Lense.CurrDiff = diff
// calculate PrevDiff
diff, err = DiffValue(cv, lv)
if err != nil {
return err
}
cs.Lense.PrevDiff = diff
// TODO, calc other diff shapes or representations?
// update current before next iteration
cv = lv
cs = ls
}
return nil
}
func diffDatamodel(dm *Datamodel) error {
/*
dms, err := LoadDatamodels(args, flgs)
if err != nil {
return err
}
dms, err = filterDatamodelsByTimestamp(dms, flgs)
if err != nil {
return err
}
for _, dm := range dms {
if len(dm.History.Past) == 0 {
fmt.Printf("%s: no history\n", dm.Name)
} else {
past := dm.History.Past[0]
if flgs.Since != "" {
past = dm.History.Past[len(dm.History.Past)-1]
}
fmt.Printf("// %s -> %s\n%s: ", dm.History.Past[0].Timestamp, dm.Timestamp, dm.Name)
diff, err := structural.DiffValue(past.Value, dm.Value, nil)
if err != nil {
return err
}
if !diff.Exists() {
fmt.Println("{}")
} else {
ctx := diff.Context()
m := ctx.CompileString(orderedMask)
r, err := structural.MaskValue(m, diff, nil)
if err != nil {
return err
}
fmt.Println(r)
}
}
}
*/
return nil
}
/*
func CalcDatamodelStepwiseDiff(dm *Datamodel) error {
if dm.History == nil || len(dm.History.Past) == 0 {
return nil
}
past := dm.History.Past
// loop back through time (checkpoints)
curr := dm
for i := 0; i < len(past); i++ {
// get prev to compare against
prev := past[i]
// calculate what needs to be done to prev to get to curr
diff, err := structural.DiffValue(prev.Value, curr.Value, nil)
if err != nil {
return err
}
curr.Subsume = prev.Value.Subsume(curr.Value)
// set changes need to arrive at curr
curr.Diff = diff
// update before relooping
curr = prev
}
// TODO(subsume), descend into Models and Fields for diff / subsume for more granular information
return nil
}
*/
//
//
///// Everything below is a copy of structural.Diff
// we have a copy here because we are thinking
// it might be different or optimized, while
// not adding complexity to the more general structural.Diff
// TODO we still need to think about List diff in both places
//
func DiffValue(orig, next cue.Value) (cue.Value, error) {
r, ok := diffValue(orig, next)
if !ok {
return cue.Value{}, nil
}
return r, nil
}
func diffValue(orig, next cue.Value) (cue.Value, bool) {
switch orig.IncompleteKind() {
case cue.StructKind:
// fmt.Println("struct", orig, next)
return diffStruct(orig, next)
case cue.ListKind:
// fmt.Println("list", orig, next)
return diffList(orig, next)
default:
// fmt.Println("leaf", orig, next)
return diffLeaf(orig, next)
}
}
func diffStruct(orig, next cue.Value) (cue.Value, bool) {
ctx := orig.Context()
result := ctx.CompileString("{}")
rmv := ctx.CompileString("{}")
add := ctx.CompileString("{}")
didAdd := false
didRmv := false
// first loop over val
iter, _ := orig.Fields(fieldOptions...)
for iter.Next() {
s := iter.Selector()
// HACK, this works around a bug in CUE
// p := cue.MakePath(s)
p := cue.ParsePath(fmt.Sprint(s))
u := next.LookupPath(p)
// check that field exists in from. Should we be checking f.Err()?
if u.Exists() {
r, ok := diffValue(iter.Value(), u)
// fmt.Println("r:", r, ok, p)
if ok {
result = result.FillPath(p, r)
}
} else {
// remove if orig not in next
didRmv = true
rmv = rmv.FillPath(p, iter.Value())
}
}
// add anything in next that is not in orig
iter, _ = next.Fields(fieldOptions...)
for iter.Next() {
s := iter.Selector()
// HACK, this works around a bug in CUE
// p := cue.MakePath(s)
p := cue.ParsePath(fmt.Sprint(s))
v := orig.LookupPath(p)
// check that field exists in from. Should we be checking f.Err()?
if !v.Exists() {
didAdd = true
add = add.FillPath(p, iter.Value())
}
}
if didRmv {
result = result.FillPath(cue.ParsePath("\"-\""), rmv)
}
if didAdd {
result = result.FillPath(cue.ParsePath("\"+\""), add)
}
// checks to see if nothing changed
i := 0
iter, _ = result.Fields()
for iter.Next() {
i++
}
if i == 0 {
return result, false
}
return result, true
}
func diffList(orig, next cue.Value) (cue.Value, bool) {
ctx := orig.Context()
oi, _ := orig.List()
ni, _ := next.List()
result := []cue.Value{}
for oi.Next() && ni.Next() {
v, ok := diffValue(oi.Value(), ni.Value())
if ok {
result = append(result, v)
}
}
return ctx.NewList(result...), len(result) != 0
}
func diffLeaf(orig, next cue.Value) (cue.Value, bool) {
// ss := orig.Path().Selectors()
// lbl := ss[len(ss)-1]
// check if they are the same
// by type, concreteness, and unify
// if same, no need to include in diff
if orig.IncompleteKind() == next.IncompleteKind() {
if orig.IsConcrete() == next.IsConcrete() {
u := orig.Unify(next)
if u.Err() == nil {
return cue.Value{}, false
}
}
}
// need to know if this is a basic lit, so we know if we are changing a concrete value
ctx := orig.Context()
ret := ctx.CompileString("{}")
ret = ret.FillPath(cue.ParsePath("\"-\""), orig)
ret = ret.FillPath(cue.ParsePath("\"+\""), next)
// otherwise, we have a diff to create
/*
rmv := ctx.CompileString("{}")
rmv = rmv.FillPath(cue.MakePath(lbl), orig)
ret = ret.FillPath(cue.ParsePath("\"-\""), rmv)
add := ctx.CompileString("{}")
add = add.FillPath(cue.MakePath(lbl), next)
ret = ret.FillPath(cue.ParsePath("\"+\""), add)
*/
return ret, true
}
func (dm *Datamodel) PrintDiff(out io.Writer, dflags flags.DatamodelPflagpole) error {
return dm.T.printDiffR(out, dflags)
}
func (V *Value) printDiffR(out io.Writer, dflags flags.DatamodelPflagpole) error {
// load own history
if V.Hof.Datamodel.History {
if V.hasDiff() {
if err := V.printDiff(out, dflags); err != nil {
return err
}
}
}
// recurse if children to load any nested histories
for _, c := range V.Children {
if err := c.T.printDiffR(out, dflags); err != nil {
return err
}
}
return nil
}
func (V *Value) printDiff(out io.Writer, dflags flags.DatamodelPflagpole) error {
name := V.Hof.Label
p := cue.ParsePath(name)
d := V.Diff()
ctx := d.Context()
val := ctx.CompileString("_")
val = val.FillPath(p, d)
// add lacunas
node := val.Syntax(
cue.Final(),
cue.Docs(true),
cue.Attributes(true),
cue.Definitions(true),
cue.Optional(true),
cue.Hidden(true),
cue.Concrete(true),
cue.ResolveReferences(true),
)
file, err := astutil.ToFile(node.(*ast.StructLit))
if err != nil {
return err
}
pkg := &ast.Package{
Name: ast.NewIdent("diff"),
}
file.Decls = append([]ast.Decl{pkg}, file.Decls...)
// fmt.Printf("%#+v\n", file)
bytes, err := format.Node(
file,
format.Simplify(),
)
if err != nil {
return err
}
str := string(bytes)
fmt.Fprintln(out, str)
return nil
}