Skip to content
A library for doing diffs of arbitrary Golang structs.
Go
Branch: master
Clone or download
mewmew and d4l3k record both old and new value of modified fields
This is to support alternative output formats than the
one implemented by PrettyDiff.

Most notably, with this change it would be trivial to
implement a pretty-printed with output similar to that
of the `diff` tool, which can display a modified field
as follows:

    -path: old_value
    +path: new_value

Prior to this commit, only the new value was recorded,
so producing output such as the one examplified above
would require traversing the `a` parameter passed to
DeepDiff, and such traversal would require an extensive
use of reflect, essentially re-implementing the core of
messagediff.

Therefore, to allow for broader use-cases of messagediff,
the API is adjusted to include both the old and the new
value for modified fields.

This may be considered a backwards incompatible change
to the API, in particular the type of the Modified
field of the Diff struct has been updated. Users of
PrettyPrint still have the same functionality, and
all test cases pass without any modifications, so it
may be possible to argue that this API change is more
considered an internal detail, than something facing
the average user.

In either case, the added functionalty may warrant
the update to the API.
1
Latest commit 7e0a312 Aug 28, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example Make sure path is not incorrectly modified via ref Dec 15, 2016
internal/natsort use natural sorting of indices in PrettyDiff Aug 27, 2019
testdata
.coveralls.yml Added coveralls support Jan 20, 2016
.gitignore Initial commit Aug 11, 2015
.travis.yml removed old Go versions + allowed tip to fail Jun 14, 2018
CHANGELOG.md
LICENSE
README.md harmonized code identing in README Jul 18, 2017
bypass.go Updated messagediff to handle tip/1.6 Jan 8, 2016
bypasssafe.go
messagediff.go record both old and new value of modified fields Aug 29, 2019
messagediff_test.go use natural sorting of indices in PrettyDiff Aug 27, 2019

README.md

messagediff Build Status Coverage Status GoDoc

A library for doing diffs of arbitrary Golang structs.

If the unsafe package is available messagediff will diff unexported fields in addition to exported fields. This is primarily used for testing purposes as it allows for providing informative error messages.

Optionally, fields in structs can be tagged as testdiff:"ignore" to make messagediff skip it when doing the comparison.

Example Usage

In a normal file:

package main

import "gopkg.in/d4l3k/messagediff.v1"

type someStruct struct {
    A, b int
    C []int
}

func main() {
    a := someStruct{1, 2, []int{1}}
    b := someStruct{1, 3, []int{1, 2}}
    diff, equal := messagediff.PrettyDiff(a, b)
    /*
        diff =
        `added: .C[1] = 2
        modified: .b = 3`

        equal = false
    */
}

In a test:

import "gopkg.in/d4l3k/messagediff.v1"

...

type someStruct struct {
    A, b int
    C []int
}

func TestSomething(t *testing.T) {
    want := someStruct{1, 2, []int{1}}
    got := someStruct{1, 3, []int{1, 2}}
    if diff, equal := messagediff.PrettyDiff(want, got); !equal {
        t.Errorf("Something() = %#v\n%s", got, diff)
    }
}

To ignore a field in a struct, just annotate it with testdiff:"ignore" like this:

package main

import "gopkg.in/d4l3k/messagediff.v1"

type someStruct struct {
    A int
    B int `testdiff:"ignore"`
}

func main() {
    a := someStruct{1, 2}
    b := someStruct{1, 3}
    diff, equal := messagediff.PrettyDiff(a, b)
    /*
        equal = true
        diff = ""
    */
}

See the DeepDiff function for using the diff results programmatically.

License

Copyright (c) 2015 Tristan Rice rice@fn.lc

messagediff is licensed under the MIT license. See the LICENSE file for more information.

bypass.go and bypasssafe.go are borrowed from go-spew and have a seperate copyright notice.

You can’t perform that action at this time.