Reporter work-in-progress
diff --git a/cmp/compare.go b/cmp/compare.go
index 8a90940..9e6fc91 100644
--- a/cmp/compare.go
+++ b/cmp/compare.go
@@ -96,7 +96,7 @@
// Do not depend on this output being stable.
func Diff(x, y interface{}, opts ...Option) string {
r := new(defaultReporter)
- opts = Options{Options(opts), r}
+ opts = Options{Options(opts), Reporter(r)}
eq := Equal(x, y, opts...)
d := r.String()
if (d == "") != eq {
@@ -108,9 +108,9 @@
type state struct {
// These fields represent the "comparison state".
// Calling statelessCompare must not result in observable changes to these.
- result diff.Result // The current result of comparison
- curPath Path // The current path in the value tree
- reporter reporter // Optional reporter used for difference formatting
+ result diff.Result // The current result of comparison
+ curPath Path // The current path in the value tree
+ out output // Optional reporter and/or debugger
// dynChecker triggers pseudo-random checks for option correctness.
// It is safe for statelessCompare to mutate this value.
@@ -151,11 +151,11 @@
for t := range opt {
s.exporters[t] = true
}
- case reporter:
- if s.reporter != nil {
- panic("difference reporter already registered")
+ case output:
+ if s.out != (output{}) {
+ panic("Reporter already registered")
}
- s.reporter = opt
+ s.out = opt
default:
panic(fmt.Sprintf("unknown option %T", opt))
}
@@ -170,17 +170,17 @@
// It is an implementation bug if the contents of curPath differs from
// when calling this function to when returning from it.
- oldResult, oldReporter := s.result, s.reporter
- s.result = diff.Result{} // Reset result
- s.reporter = nil // Remove reporter to avoid spurious printouts
+ oldResult, oldOutput := s.result, s.out
+ s.result, s.out = diff.Result{}, output{} // Reset result and output
s.compareAny(vx, vy)
res := s.result
- s.result, s.reporter = oldResult, oldReporter
+ s.result, s.out = oldResult, oldOutput
return res
}
func (s *state) compareAny(vx, vy reflect.Value) {
// TODO: Support cyclic data structures.
+ // TODO: Make use of s.out.debugger if available.
// Rule 0: Differing types are never equal.
if !vx.IsValid() || !vy.IsValid() {
@@ -193,8 +193,8 @@
}
t := vx.Type()
if len(s.curPath) == 0 {
- s.curPath.push(&pathStep{typ: t})
- defer s.curPath.pop()
+ s.pushStep(&pathStep{typ: t})
+ defer s.popStep()
}
vx, vy = s.tryExporting(vx, vy)
@@ -239,8 +239,8 @@
s.report(vx.IsNil() && vy.IsNil(), vx, vy)
return
}
- s.curPath.push(&indirect{pathStep{t.Elem()}})
- defer s.curPath.pop()
+ s.pushStep(&indirect{pathStep{t.Elem()}})
+ defer s.popStep()
s.compareAny(vx.Elem(), vy.Elem())
return
case reflect.Interface:
@@ -252,8 +252,8 @@
s.report(false, vx.Elem(), vy.Elem())
return
}
- s.curPath.push(&typeAssertion{pathStep{vx.Elem().Type()}})
- defer s.curPath.pop()
+ s.pushStep(&typeAssertion{pathStep{vx.Elem().Type()}})
+ defer s.popStep()
s.compareAny(vx.Elem(), vy.Elem())
return
case reflect.Slice:
@@ -389,12 +389,14 @@
func (s *state) compareArray(vx, vy reflect.Value, t reflect.Type) {
step := &sliceIndex{pathStep{t.Elem()}, 0, 0}
- s.curPath.push(step)
// Compute an edit-script for slices vx and vy.
es := diff.Difference(vx.Len(), vy.Len(), func(ix, iy int) diff.Result {
step.xkey, step.ykey = ix, iy
- return s.statelessCompare(vx.Index(ix), vy.Index(iy))
+ s.pushStep(step)
+ res := s.statelessCompare(vx.Index(ix), vy.Index(iy))
+ s.popStep()
+ return res
})
// Report the entire slice as is if the arrays are of primitive kind,
@@ -407,7 +409,6 @@
isPrimitive = true
}
if isPrimitive && es.Dist() > (vx.Len()+vy.Len())/4 {
- s.curPath.pop() // Pop first since we are reporting the whole slice
s.report(false, vx, vy)
return
}
@@ -418,14 +419,17 @@
switch e {
case diff.UniqueX:
step.xkey, step.ykey = ix, -1
+ s.pushStep(step)
s.report(false, vx.Index(ix), nothing)
ix++
case diff.UniqueY:
step.xkey, step.ykey = -1, iy
+ s.pushStep(step)
s.report(false, nothing, vy.Index(iy))
iy++
default:
step.xkey, step.ykey = ix, iy
+ s.pushStep(step)
if e == diff.Identity {
s.report(true, vx.Index(ix), vy.Index(iy))
} else {
@@ -434,8 +438,8 @@
ix++
iy++
}
+ s.popStep()
}
- s.curPath.pop()
return
}
@@ -448,10 +452,9 @@
// We combine and sort the two map keys so that we can perform the
// comparisons in a deterministic order.
step := &mapIndex{pathStep: pathStep{t.Elem()}}
- s.curPath.push(step)
- defer s.curPath.pop()
for _, k := range value.SortKeys(append(vx.MapKeys(), vy.MapKeys()...)) {
step.key = k
+ s.pushStep(step)
vvx := vx.MapIndex(k)
vvy := vy.MapIndex(k)
switch {
@@ -468,6 +471,7 @@
// See https://golang.org/issue/11104
panic(fmt.Sprintf("%#v has map key with NaNs", s.curPath))
}
+ s.popStep()
}
}
@@ -475,8 +479,6 @@
var vax, vay reflect.Value // Addressable versions of vx and vy
step := &structField{}
- s.curPath.push(step)
- defer s.curPath.pop()
for i := 0; i < t.NumField(); i++ {
vvx := vx.Field(i)
vvy := vy.Field(i)
@@ -499,7 +501,23 @@
step.pvy = vay
step.field = t.Field(i)
}
+ s.pushStep(step)
s.compareAny(vvx, vvy)
+ s.popStep()
+ }
+}
+
+func (s *state) pushStep(ps PathStep) {
+ s.curPath.push(ps)
+ if s.out.reporter != nil {
+ s.out.PushStep(ps)
+ }
+}
+
+func (s *state) popStep() {
+ s.curPath.pop()
+ if s.out.reporter != nil {
+ s.out.PopStep()
}
}
@@ -511,8 +529,8 @@
} else {
s.result.NDiff++
}
- if s.reporter != nil {
- s.reporter.Report(vx, vy, eq, s.curPath)
+ if s.out.reporter != nil {
+ s.out.Report(vx, vy, eq)
}
}
diff --git a/cmp/options.go b/cmp/options.go
index a4e159a..34f1408 100644
--- a/cmp/options.go
+++ b/cmp/options.go
@@ -258,8 +258,8 @@
func (tr *transformer) apply(s *state, vx, vy reflect.Value) bool {
// Update path before calling the Transformer so that dynamic checks
// will use the updated path.
- s.curPath.push(&transform{pathStep{tr.fnc.Type().Out(0)}, tr})
- defer s.curPath.pop()
+ s.pushStep(&transform{pathStep{tr.fnc.Type().Out(0)}, tr})
+ defer s.popStep()
vx = s.callTRFunc(tr.fnc, vx)
vy = s.callTRFunc(tr.fnc, vy)
@@ -367,23 +367,50 @@
panic("not implemented")
}
-// reporter is an Option that configures how differences are reported.
-type reporter interface {
- // TODO: Not exported yet.
- //
- // Perhaps add PushStep and PopStep and change Report to only accept
- // a PathStep instead of the full-path? Adding a PushStep and PopStep makes
- // it clear that we are traversing the value tree in a depth-first-search
- // manner, which has an effect on how values are printed.
+// Reporter returns an Option that configures Equal to report the result of
+// every comparison made to the provided reporter, r.
+//
+// As Equal traverses the values, it does so in a depth-first search.
+// PushStep is called when descending into a sub-value and
+// PopStep is called when ascending back out of the sub-value.
+//
+// The Report method is called for every comparison made and will be provided
+// with the two values being compared and the result of equality.
+// When traversing slices or maps, one of the reflect.Values may be invalid
+// if it is non-existent. Since Equal does not mutate the input values,
+// it is safe to keep a reference to the values past the Report call.
+// However, Report must not mutate the input values.
+//
+// If r has a method Debug(string), then Debug will be called with verbose
+// debugging information regarding the execution of Equal.
+func Reporter(r interface {
+ PushStep(PathStep)
+ PopStep()
+ Report(x, y reflect.Value, eq bool)
+}) Option {
+ if dbg, ok := r.(debugger); ok {
+ return output{reporter: r, debugger: dbg}
+ }
+ return output{reporter: r}
+}
- Option
+type (
+ reporter interface {
+ PushStep(PathStep)
+ PopStep()
+ Report(_, _ reflect.Value, _ bool)
+ }
+ debugger interface {
+ Debug(string)
+ }
+ output struct {
+ reporter
+ debugger
+ }
+)
- // Report is called for every comparison made and will be provided with
- // the two values being compared, the equality result, and the
- // current path in the value tree. It is possible for x or y to be an
- // invalid reflect.Value if one of the values is non-existent;
- // which is possible with maps and slices.
- Report(x, y reflect.Value, eq bool, p Path)
+func (output) filter(_ *state, _, _ reflect.Value, _ reflect.Type) applicableOption {
+ panic("not implemented")
}
// normalizeOption normalizes the input options such that all Options groups
diff --git a/cmp/options_test.go b/cmp/options_test.go
index 009b524..7aecf28 100644
--- a/cmp/options_test.go
+++ b/cmp/options_test.go
@@ -129,7 +129,7 @@
}, {
label: "FilterPath",
fnc: FilterPath,
- args: []interface{}{func(Path) bool { return true }, &defaultReporter{}},
+ args: []interface{}{func(Path) bool { return true }, Reporter(nil)},
wantPanic: "invalid option type",
}, {
label: "FilterPath",
@@ -138,7 +138,7 @@
}, {
label: "FilterPath",
fnc: FilterPath,
- args: []interface{}{func(Path) bool { return true }, Options{Ignore(), &defaultReporter{}}},
+ args: []interface{}{func(Path) bool { return true }, Options{Ignore(), Reporter(nil)}},
wantPanic: "invalid option type",
}, {
label: "FilterValues",
@@ -171,7 +171,7 @@
}, {
label: "FilterValues",
fnc: FilterValues,
- args: []interface{}{func(int, int) bool { return true }, &defaultReporter{}},
+ args: []interface{}{func(int, int) bool { return true }, Reporter(nil)},
wantPanic: "invalid option type",
}, {
label: "FilterValues",
@@ -180,7 +180,7 @@
}, {
label: "FilterValues",
fnc: FilterValues,
- args: []interface{}{func(int, int) bool { return true }, Options{Ignore(), &defaultReporter{}}},
+ args: []interface{}{func(int, int) bool { return true }, Options{Ignore(), Reporter(nil)}},
wantPanic: "invalid option type",
}}
diff --git a/cmp/reporter.go b/cmp/reporter.go
index 3be5664..b0d3f3f 100644
--- a/cmp/reporter.go
+++ b/cmp/reporter.go
@@ -13,16 +13,17 @@
)
type defaultReporter struct {
- Option
+ path Path // The current path
diffs []string // List of differences, possibly truncated
ndiffs int // Total number of differences
nbytes int // Number of bytes in diffs
nlines int // Number of lines in diffs
}
-var _ reporter = (*defaultReporter)(nil)
+func (r *defaultReporter) PushStep(ps PathStep) { r.path.push(ps) }
+func (r *defaultReporter) PopStep() { r.path.pop() }
-func (r *defaultReporter) Report(x, y reflect.Value, eq bool, p Path) {
+func (r *defaultReporter) Report(x, y reflect.Value, eq bool) {
if eq {
return // Ignore equal results
}
@@ -37,7 +38,7 @@
sx = value.Format(x, false)
sy = value.Format(y, false)
}
- s := fmt.Sprintf("%#v:\n\t-: %s\n\t+: %s\n", p, sx, sy)
+ s := fmt.Sprintf("%#v:\n\t-: %s\n\t+: %s\n", r.path, sx, sy)
r.diffs = append(r.diffs, s)
r.nbytes += len(s)
r.nlines += strings.Count(s, "\n")