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
|
// Copyright 2019 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package diff
import (
"cuelang.org/go/cue"
)
// Profile configures a diff operation.
type Profile struct {
Concrete bool
// Hidden fields are only useful to compare when a values are from the same
// package.
SkipHidden bool
// TODO: Use this method instead of SkipHidden. To do this, we need to have
// access the package associated with a hidden field, which is only
// accessible through the Iterator API. And we should probably get rid of
// the cue.Struct API.
//
// HiddenPkg compares hidden fields for the package if this is not the empty
// string. Use "_" for the anonymous package.
// HiddenPkg string
}
var (
// Schema is the standard profile used for comparing schema.
Schema = &Profile{}
// Final is the standard profile for comparing data.
Final = &Profile{
Concrete: true,
}
)
// TODO: don't return Kind, which is always Modified or not.
// Diff is a shorthand for Schema.Diff.
func Diff(x, y cue.Value) (Kind, *EditScript) {
return Schema.Diff(x, y)
}
// Diff returns an edit script representing the difference between x and y.
func (p *Profile) Diff(x, y cue.Value) (Kind, *EditScript) {
d := differ{cfg: *p}
k, es := d.diffValue(x, y)
if k == Modified && es == nil {
es = &EditScript{X: x, Y: y}
}
return k, es
}
// Kind identifies the kind of operation of an edit script.
type Kind uint8
const (
// Identity indicates that a value pair is identical in both list X and Y.
Identity Kind = iota
// UniqueX indicates that a value only exists in X and not Y.
UniqueX
// UniqueY indicates that a value only exists in Y and not X.
UniqueY
// Modified indicates that a value pair is a modification of each other.
Modified
)
// EditScript represents the series of differences between two CUE values.
// x and y must be either both list or struct.
type EditScript struct {
X, Y cue.Value
Edits []Edit
}
// Edit represents a single operation within an edit-script.
type Edit struct {
Kind Kind
XSel cue.Selector // valid if UniqueY
YSel cue.Selector // valid if UniqueX
Sub *EditScript // non-nil if Modified
}
type differ struct {
cfg Profile
}
func (d *differ) diffValue(x, y cue.Value) (Kind, *EditScript) {
if d.cfg.Concrete {
x, _ = x.Default()
y, _ = y.Default()
}
if x.IncompleteKind() != y.IncompleteKind() {
return Modified, nil
}
switch xc, yc := x.IsConcrete(), y.IsConcrete(); {
case xc != yc:
return Modified, nil
case xc && yc:
switch k := x.Kind(); k {
case cue.StructKind:
return d.diffStruct(x, y)
case cue.ListKind:
return d.diffList(x, y)
}
fallthrough
default:
// In concrete mode we do not care about non-concrete values.
if d.cfg.Concrete {
return Identity, nil
}
if !x.Equals(y) {
return Modified, nil
}
}
return Identity, nil
}
type field struct {
sel cue.Selector
val cue.Value
}
// TODO(mvdan): use slices.Collect once we swap cue.Iterator for a Go iterator
func (d *differ) collectFields(v cue.Value) []field {
iter, _ := v.Fields(cue.Hidden(!d.cfg.SkipHidden), cue.Definitions(true), cue.Optional(true))
var fields []field
for iter.Next() {
fields = append(fields, field{iter.Selector(), iter.Value()})
}
return fields
}
func (d *differ) diffStruct(x, y cue.Value) (Kind, *EditScript) {
xFields := d.collectFields(x)
yFields := d.collectFields(y)
// Best-effort topological sort, prioritizing x over y, using a variant of
// Kahn's algorithm (see, for instance
// https://www.geeksforgeeks.org/topological-sorting-indegree-based-solution/).
// We assume that the order of the elements of each value indicate an edge
// in the graph. This means that only the next unprocessed nodes can be
// those with no incoming edges.
xMap := make(map[cue.Selector]struct{}, len(xFields))
yMap := make(map[cue.Selector]int, len(yFields))
for _, f := range xFields {
xMap[f.sel] = struct{}{}
}
for i, f := range yFields {
yMap[f.sel] = i + 1
}
edits := []Edit{}
differs := false
for xi, yi := 0, 0; xi < len(xFields) || yi < len(yFields); {
// Process zero nodes
for ; xi < len(xFields); xi++ {
xf := xFields[xi]
yp := yMap[xf.sel]
if yp > 0 {
break
}
edits = append(edits, Edit{UniqueX, xf.sel, cue.Selector{}, nil})
differs = true
}
for ; yi < len(yFields); yi++ {
yf := yFields[yi]
if yMap[yf.sel] == 0 {
// already done
continue
}
if _, ok := xMap[yf.sel]; ok {
break
}
yMap[yf.sel] = 0
edits = append(edits, Edit{UniqueY, cue.Selector{}, yf.sel, nil})
differs = true
}
// Compare nodes
for ; xi < len(xFields); xi++ {
xf := xFields[xi]
yp := yMap[xf.sel]
if yp == 0 {
break
}
// If yp != xi+1, the topological sort was not possible.
yMap[xf.sel] = 0
yf := yFields[yp-1]
var kind Kind
var script *EditScript
switch {
case xf.sel.IsDefinition() != yf.sel.IsDefinition(), xf.sel.ConstraintType() != yf.sel.ConstraintType():
kind = Modified
default:
// TODO(perf): consider evaluating lazily.
kind, script = d.diffValue(xf.val, yf.val)
}
edits = append(edits, Edit{kind, xf.sel, yf.sel, script})
differs = differs || kind != Identity
}
}
if !differs {
return Identity, nil
}
return Modified, &EditScript{X: x, Y: y, Edits: edits}
}
// TODO: right now we do a simple element-by-element comparison. Instead,
// use an algorithm that approximates a minimal Levenshtein distance, like the
// one in github.com/google/go-cmp/internal/diff.
func (d *differ) diffList(x, y cue.Value) (Kind, *EditScript) {
ix, _ := x.List()
iy, _ := y.List()
edits := []Edit{}
differs := false
i := 0
for {
// TODO: This would be much easier with a Next/Done API.
hasX := ix.Next()
hasY := iy.Next()
if !hasX {
for hasY {
differs = true
edits = append(edits, Edit{UniqueY, cue.Selector{}, cue.Index(i), nil})
hasY = iy.Next()
i++
}
break
}
if !hasY {
for hasX {
differs = true
edits = append(edits, Edit{UniqueX, cue.Index(i), cue.Selector{}, nil})
hasX = ix.Next()
i++
}
break
}
// Both x and y have a value.
kind, script := d.diffValue(ix.Value(), iy.Value())
if kind != Identity {
differs = true
}
edits = append(edits, Edit{kind, cue.Index(i), cue.Index(i), script})
i++
}
if !differs {
return Identity, nil
}
return Modified, &EditScript{X: x, Y: y, Edits: edits}
}
|