Documentation
¶
Index ¶
- type Comparer
- type Context
- type Diff
- type Opt
- func WithCompareDifferentSizeArrays(b bool) Opt
- func WithCompareDifferentTypeStructs(b bool) Opt
- func WithComparer(comparer ...Comparer) Opt
- func WithIgnoreUnmatchedFields(b bool) Opt
- func WithIgnoredFields(names ...string) Opt
- func WithSliceOrderedComparison(b bool) Opt
- func WithStripPointerAtFirst(b bool) Opt
- func WithTreatEmptyStructPtrAsNilPtr(b bool) Opt
- type Path
- type PathPart
- type Update
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 ¶
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
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
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 ¶
WithComparer registers your customized Comparer into internal structure.
func WithIgnoreUnmatchedFields ¶ added in v0.3.1
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 ¶
WithIgnoredFields specifies the struct field whom should be ignored in comparing.
func WithSliceOrderedComparison ¶
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
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
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.