Skip to content

Commit

Permalink
internal/diff: fix printing and top-level diffs
Browse files Browse the repository at this point in the history
Print concrete values: before %-v was used, which would
print conjuncts, not evaluated expressions.

Top-level diffs were awkward, as it would not return an edit
script and the user would have to do their own printing.
This fixes that.

TODO: modify the API to not return the kind, only the
EditScript.

Change-Id: I7c9d0322fb5e6dac0f4668f14de8ae76bb614823
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9342
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed Apr 8, 2021
1 parent ad4d1a1 commit 72e8fb4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
8 changes: 7 additions & 1 deletion internal/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var (
}
)

// 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)
Expand All @@ -42,7 +44,11 @@ func Diff(x, y cue.Value) (Kind, *EditScript) {
// 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}
return d.diffValue(x, y)
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.
Expand Down
11 changes: 6 additions & 5 deletions internal/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func TestDiff(t *testing.T) {
x: `"foo"`,
y: `"bar"`,
kind: Modified,
diff: `- "foo",
+ "bar",
`,
}, {
name: "basics",
x: `{
Expand Down Expand Up @@ -111,7 +114,7 @@ func TestDiff(t *testing.T) {
`,
kind: Modified,
diff: ` {
ls: [2, 3, 4]
ls: [2,3,4]
- "foo-bar": 2
+ "foo-bar": 3
s: 4
Expand All @@ -125,7 +128,7 @@ func TestDiff(t *testing.T) {
lm2: [
- 6,
]
+ la: [2, 3, 4]
+ la: [2,3,4]
}
`,
}, {
Expand Down Expand Up @@ -299,9 +302,7 @@ a: x: "hello"
`,
kind: Modified,
diff: ` {
- a: {
- x: "hello"
- }
- a: {x:"hello"}
}
`,
}, {
Expand Down
5 changes: 3 additions & 2 deletions internal/diff/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ func (p *printer) script(e *EditScript) {
case cue.ListKind:
p.printList(e)
default:
p.printf("BadExpr")
p.printElem("-", e.x)
p.printElem("+", e.y)
}
}

Expand Down Expand Up @@ -194,7 +195,7 @@ func (p *printer) printSkipped(n int) {

func (p *printer) printValue(v cue.Value) {
// TODO: have indent option.
s := fmt.Sprintf("%-v", v)
s := fmt.Sprintf("%+v", v)
io.WriteString(p, s)
}

Expand Down

0 comments on commit 72e8fb4

Please sign in to comment.