-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
diff.go
147 lines (123 loc) · 3.33 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
package structural
import (
"cuelang.org/go/cue"
"github.com/hofstadter-io/cuetils/cmd/cuetils/flags"
)
func DiffGlobs(orig string, next string, opts *flags.RootPflagpole) ([]GlobResult, error) {
return BinaryOpGlobs(orig, []string{next}, opts, DiffValue)
}
func DiffValue(orig, next cue.Value, opts *flags.RootPflagpole) (cue.Value, error) {
r, ok := diffValue(orig, next, opts)
if !ok {
return cue.Value{}, nil
}
return r, nil
}
func diffValue(orig, next cue.Value, opts *flags.RootPflagpole) (cue.Value, bool) {
// TODO, implement proper default helper func
// for flags in hofmod-cli
if opts == nil {
opts = &flags.RootPflagpole{}
}
switch orig.IncompleteKind() {
case cue.StructKind:
// fmt.Println("struct", orig, next)
return diffStruct(orig, next, opts)
case cue.ListKind:
// fmt.Println("list", orig, next)
return diffList(orig, next, opts)
default:
// fmt.Println("leaf", orig, next)
return diffLeaf(orig, next, opts)
}
}
func diffStruct(orig, next cue.Value, opts *flags.RootPflagpole) (cue.Value, bool) {
ctx := orig.Context()
result := newStruct(ctx)
add := newStruct(ctx)
rmv := newStruct(ctx)
didAdd := false
didRmv := false
// first loop over val
iter, _ := orig.Fields(defaultWalkOptions...)
for iter.Next() {
s := iter.Selector()
p := cue.MakePath(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, opts)
// 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(defaultWalkOptions...)
for iter.Next() {
s := iter.Selector()
p := cue.MakePath(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, opts *flags.RootPflagpole) (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(), opts)
if ok {
result = append(result, v)
}
}
return ctx.NewList(result...), len(result) != 0
}
func diffLeaf(orig, next cue.Value, opts *flags.RootPflagpole) (cue.Value, bool) {
ctx := orig.Context()
ret := newStruct(ctx)
lbl := GetLabel(orig)
// check if they are the same type and concreteness, and check if unify, if so, no need to add to diff
if orig.IncompleteKind() == next.IncompleteKind() {
if orig.IsConcrete() == next.IsConcrete() {
u := orig.Unify(next)
if u.Err() == nil {
return ret, false
}
}
}
// otherwise, we have a diff to create
rmv := newStruct(ctx)
rmv = rmv.FillPath(cue.MakePath(lbl), orig)
ret = ret.FillPath(cue.ParsePath("\"-\""), rmv)
add := newStruct(ctx)
add = add.FillPath(cue.MakePath(lbl), next)
ret = ret.FillPath(cue.ParsePath("\"+\""), add)
return ret, true
}