new option to disable providing all struct fields when a struct is compared to nil
diff --git a/diff.go b/diff.go index c67d792..35fe0c2 100644 --- a/diff.go +++ b/diff.go
@@ -30,8 +30,9 @@ // Differ a configurable diff instance type Differ struct { - SliceOrdering bool - cl Changelog + SliceOrdering bool + DisableStructValues bool + cl Changelog } // Changelog stores a list of changed items
diff --git a/diff_struct.go b/diff_struct.go index 1f9c5d5..16e7faa 100644 --- a/diff_struct.go +++ b/diff_struct.go
@@ -15,10 +15,18 @@ } if a.Kind() == reflect.Invalid { + if d.DisableStructValues { + d.cl.add(CREATE, path, nil, b.Interface()) + return nil + } return d.structValues(CREATE, path, b) } if b.Kind() == reflect.Invalid { + if d.DisableStructValues { + d.cl.add(DELETE, path, a.Interface(), nil) + return nil + } return d.structValues(DELETE, path, a) }
diff --git a/options.go b/options.go index 7150f0e..627d2b8 100644 --- a/options.go +++ b/options.go
@@ -7,3 +7,12 @@ return nil } } + +// DisableStructValues disables populating a seperate change for each item in a struct, +// where the struct is being compared to a nil value +func DisableStructValues() func(d *Differ) error { + return func(d *Differ) error { + d.DisableStructValues = true + return nil + } +}