diff

package
v1.1.10 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comparer

type Comparer interface {
	Match(typeToMatch reflect.Type) bool
	Equal(ctx Context, lhs, rhs reflect.Value, path Path) (equal bool)
}

Comparer interface.

type Context

type Context interface {
	PutAdded(k string, v typ.Any)
	PutRemoved(k string, v typ.Any)
	PutModified(k string, v Update)
	PutPath(path Path, parts ...PathPart) string
}

Context interface.

type Diff

type Diff interface {
	ForAdded(fn func(key string, val typ.Any))
	ForRemoved(fn func(key string, val typ.Any))
	ForModified(fn func(key string, val Update))

	PrettyPrint() string
	String() string
}

Diff includes added, removed and modified records of the two values.

func New

func New(lhs, rhs typ.Any, opts ...Opt) (inf Diff, equal bool)

New compares two value deeply and returns the Diff of them.

A Diff includes added, removed, and modified records, you can PrettyPrint them for displaying.

delta, equal := evendeep.DeepDiff([]int{3, 0}, []int{9, 3, 0})
t.Logf("delta: %v", delta)
//        added: [2] = <zero>
//        modified: [0] = 9 (int) (Old: 3)
//        modified: [1] = 3 (int) (Old: <zero>)

type Opt

type Opt func(*info)

Opt the options functor for New().

func WithCompareDifferentSizeArrays added in v0.3.1

func WithCompareDifferentSizeArrays(b bool) Opt

WithCompareDifferentSizeArrays supports a feature to treat these two array is equivalent: `[2]string{"1","2"}` and `[3]string{"1","2",<empty>}`.

func WithCompareDifferentTypeStructs added in v0.3.1

func WithCompareDifferentTypeStructs(b bool) Opt

WithCompareDifferentTypeStructs gives a way to compare two different type structs with their fields one by one.

By default, the unmatched fields will be ignored. But you can disable the feature by calling WithIgnoreUnmatchedFields(false).

func WithComparer

func WithComparer(comparer ...Comparer) Opt

WithComparer registers your customized Comparer into internal structure.

func WithIgnoreUnmatchedFields added in v0.3.1

func WithIgnoreUnmatchedFields(b bool) Opt

WithIgnoreUnmatchedFields takes effect except in WithCompareDifferentTypeStructs(true) mode. It allows those unmatched fields don't stop the fields comparing processing.

So, the two different type structs are equivalent even if some fields are unmatched each others, so long as the matched fields are equivalent.

func WithIgnoredFields

func WithIgnoredFields(names ...string) Opt

WithIgnoredFields specifies the struct field whom should be ignored in comparing.

func WithSliceOrderedComparison

func WithSliceOrderedComparison(b bool) Opt

WithSliceOrderedComparison allows which one algorithm in comparing two slice.

1. false (default), each element will be compared one by one.

2. true, the elements in slice will be compared without ordered insensitive. In this case, [9, 5] and [5, 9] are identity.

func WithStripPointerAtFirst added in v0.2.51

func WithStripPointerAtFirst(b bool) Opt

WithStripPointerAtFirst set the flag which allow finding the real targets of the input objects.

Typically, the two struct pointers will be compared with field by field rather than comparing its pointer addresses.

For example, when you diff.Diff(&b1, &b2, diff.WithStripPointerAtFirst(true)), we compare the fields content of b1 and b2, we don't compare its pointer addresses at this time.

The another implicit thing is, this feature also strips `interface{}`/`any` variable out of to its underlying typed object: a `var lhs any = int64(0)` will be decoded as `var lhsReal int64 = 0`.

Even if without stripPointerAtFirst enabled, any input parameters which are `diff`ing will be striped to underlying dattype if it's wrapped by `interface{}`.

func WithTreatEmptyStructPtrAsNilPtr added in v0.3.1

func WithTreatEmptyStructPtrAsNilPtr(b bool) Opt

WithTreatEmptyStructPtrAsNilPtr set the flag which allow a field with nil pointer to a struct is treated as equal to the pointer to this field to pointed to an empty struct.

For example,

struct A{I int}
struct B( A *A,}
b1, b2 := B{}, B{ &A{}}
diffs := diff.Diff(b1, b2, diff. diff.WithTreatEmptyStructPtrAsNilPtr(true))
println(diffs)

And the result is the two struct are equal. the nil pointer `b1.A` and the empty struct pointer `b2.A` are treated as identity.

type Path

type Path struct {
	// contains filtered or unexported fields
}

func (Path) String

func (dp Path) String() string

type PathPart

type PathPart interface {
	String() string
}

type Update

type Update struct {
	Old, New typ.Any // string
	Typ      string
}

func (Update) String

func (n Update) String() string

Jump to

Keyboard shortcuts

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