Skip to content

Commit 8009b56

Browse files
committed
internal/diff: clean up code a bit ahead of dropping deprecated APIs
The diff package uses the deprecated cue.Value.Struct API to deal with structs, and it's the only non-test piece of code we have which still does this. Ahead of doing that refactor, and to wrap my head around the code, do a few small simplifications and tweaks first: * xMap only needs to track the presence of fields by selector in x * yMap using int makes the code below more consistent * use tighter variable scopes where possible * slightly reduce verbosity elsewhere where it makes sense Signed-off-by: Daniel Martí <[email protected]> Change-Id: I95603e6760ebf6ea777434c2dcfd79f769fe34d4 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202084 Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 5ffd573 commit 8009b56

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

internal/diff/diff.go

+17-31
Original file line numberDiff line numberDiff line change
@@ -222,33 +222,28 @@ func (d *differ) diffStruct(x, y cue.Value) (Kind, *EditScript) {
222222
// We assume that the order of the elements of each value indicate an edge
223223
// in the graph. This means that only the next unprocessed nodes can be
224224
// those with no incoming edges.
225-
xMap := make(map[string]int32, sx.Len())
226-
yMap := make(map[string]int32, sy.Len())
225+
xMap := make(map[string]struct{}, sx.Len())
226+
yMap := make(map[string]int, sy.Len())
227227
for i := 0; i < sx.Len(); i++ {
228228
f, ok := d.field(sx, i)
229-
if !ok {
230-
continue
229+
if ok {
230+
xMap[f.Selector] = struct{}{}
231231
}
232-
xMap[f.Selector] = int32(i + 1)
233232
}
234233
for i := 0; i < sy.Len(); i++ {
235234
f, ok := d.field(sy, i)
236-
if !ok {
237-
continue
235+
if ok {
236+
yMap[f.Selector] = i + 1
238237
}
239-
yMap[f.Selector] = int32(i + 1)
240238
}
241239

242240
edits := []Edit{}
243241
differs := false
244242

245-
var xi, yi int
246-
var xf, yf cue.FieldInfo
247-
var ok bool
248-
for xi < sx.Len() || yi < sy.Len() {
243+
for xi, yi := 0, 0; xi < sx.Len() || yi < sy.Len(); {
249244
// Process zero nodes
250245
for ; xi < sx.Len(); xi++ {
251-
xf, ok = d.field(sx, xi)
246+
xf, ok := d.field(sx, xi)
252247
if !ok {
253248
continue
254249
}
@@ -260,16 +255,15 @@ func (d *differ) diffStruct(x, y cue.Value) (Kind, *EditScript) {
260255
differs = true
261256
}
262257
for ; yi < sy.Len(); yi++ {
263-
yf, ok = d.field(sy, yi)
258+
yf, ok := d.field(sy, yi)
264259
if !ok {
265260
continue
266261
}
267262
if yMap[yf.Selector] == 0 {
268263
// already done
269264
continue
270265
}
271-
xp := xMap[yf.Selector]
272-
if xp > 0 {
266+
if _, ok := xMap[yf.Selector]; ok {
273267
break
274268
}
275269
yMap[yf.Selector] = 0
@@ -278,43 +272,35 @@ func (d *differ) diffStruct(x, y cue.Value) (Kind, *EditScript) {
278272
}
279273

280274
// Compare nodes
281-
var ok bool
282275
for ; xi < sx.Len(); xi++ {
283-
xf, ok = d.field(sx, xi)
276+
xf, ok := d.field(sx, xi)
284277
if !ok {
285278
continue
286279
}
287-
288280
yp := yMap[xf.Selector]
289281
if yp == 0 {
290282
break
291283
}
292284
// If yp != xi+1, the topological sort was not possible.
293285
yMap[xf.Selector] = 0
294286

295-
yf, ok := d.field(sy, int(yp-1))
287+
yf, ok := d.field(sy, yp-1)
296288
if !ok {
297289
continue
298290
}
299291

300-
kind := Identity
292+
var kind Kind
301293
var script *EditScript
302294
switch {
303-
case xf.IsDefinition != yf.IsDefinition,
304-
xf.IsOptional != yf.IsOptional:
295+
case xf.IsDefinition != yf.IsDefinition, xf.IsOptional != yf.IsOptional:
305296
kind = Modified
306-
307297
default:
308-
xv := xf.Value
309-
yv := yf.Value
310298
// TODO(perf): consider evaluating lazily.
311-
kind, script = d.diffValue(xv, yv)
299+
kind, script = d.diffValue(xf.Value, yf.Value)
312300
}
313301

314-
edits = append(edits, Edit{kind, int32(xi + 1), yp, script})
315-
if kind != Identity {
316-
differs = true
317-
}
302+
edits = append(edits, Edit{kind, int32(xi + 1), int32(yp), script})
303+
differs = differs || kind != Identity
318304
}
319305
}
320306
if !differs {

0 commit comments

Comments
 (0)