messagediff

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2020 License: MIT Imports: 6 Imported by: 3

README

messagediff

A library for doing diffs of arbitrary Golang structs. Fork of d4l3k/messagediff. Go-moduled, more options.

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.

Options:

  • fields in structs can be tagged as testdiff:"ignore" to make messagediff skip it when doing the comparison (see example).
  • fields can also be ignored by passing an option: PrettyDiff(a, b, IgnoreStructField("X"), ...)
  • ignore the differences in empty slices and nil slices. PrettyDiff(a, b, SliceWeakEmptyOption{}, ...)

Example Usage

package main

import "github.com/protolambda/messagediff"

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, from = 2; to = 3`

        equal = false
    */
}

Test usage example
import "github.com/protolambda/messagediff"

...

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("Unexpected difference in something: %s", got, diff)
    }
}
Ignore struct field with tag

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

package main

import "github.com/protolambda/messagediff"

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

Original: Copyright (c) 2015 Tristan Rice rice@fn.lc, GitHub: d4l3k/messagediff

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.

Documentation

Index

Constants

View Source
const (
	// UnsafeDisabled is a build-time constant which specifies whether or
	// not access to the unsafe package is available.
	UnsafeDisabled = false
)

Variables

This section is empty.

Functions

func PrettyDiff

func PrettyDiff(a, b interface{}, options ...Option) (string, bool)

PrettyDiff does a deep comparison and returns the nicely formated results. See DeepDiff for more details.

Types

type BytesDifferent

type BytesDifferent struct {
	From, To interface{}
}

type Diff

type Diff struct {
	Added, Removed, Modified map[*Path]interface{}
	// contains filtered or unexported fields
}

Diff represents a change in a struct.

func DeepDiff

func DeepDiff(a, b interface{}, options ...Option) (*Diff, bool)

DeepDiff does a deep comparison and returns the results. If the field is time.Time, use Equal to compare

type Different

type Different struct {
	From, To interface{}
}

type IgnoreFieldOption

type IgnoreFieldOption struct {
	Field string
}

IgnoreFieldOption is an option for specifying a field that does not diff

type MapKey

type MapKey struct {
	Key interface{}
}

MapKey is a path element representing a key of a map.

func (MapKey) String

func (n MapKey) String() string

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is an option to specify in diff

func IgnoreStructField

func IgnoreStructField(field string) Option

IgnoreStructField return an option of IgnoreFieldOption

type Path

type Path []PathNode

Path represents a path to a changed datum.

func (Path) String

func (p Path) String() string

type PathNode

type PathNode interface {
	String() string
}

PathNode represents one step in the path.

type SliceIndex

type SliceIndex int

SliceIndex is a path element representing a index of a slice.

func (SliceIndex) String

func (n SliceIndex) String() string

type SliceWeakEmptyOption

type SliceWeakEmptyOption struct{}

type StructField

type StructField string

StructField is a path element representing a field of a struct.

func (StructField) String

func (n StructField) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL