github.com/hedzr/evendeep@v0.4.8/diff/withopts.go (about) 1 package diff 2 3 // Opt the options functor for New(). 4 type Opt func(*info) 5 6 // WithIgnoredFields specifies the struct field whom should be ignored 7 // in comparing. 8 func WithIgnoredFields(names ...string) Opt { 9 return func(i *info) { 10 for _, name := range names { 11 if name != "" { 12 i.ignoredFields[name] = true 13 } 14 } 15 } 16 } 17 18 // WithSliceOrderedComparison allows which one algorithm in comparing 19 // two slice. 20 // 21 // 1. false (default), each element will be compared one by one. 22 // 23 // 2. true, the elements in slice will be compared without ordered 24 // insensitive. In this case, [9, 5] and [5, 9] are identity. 25 func WithSliceOrderedComparison(b bool) Opt { 26 return func(i *info) { 27 i.sliceNoOrder = b 28 } 29 } 30 31 // WithComparer registers your customized Comparer into internal structure. 32 func WithComparer(comparer ...Comparer) Opt { 33 return func(i *info) { 34 for _, c := range comparer { 35 if c != nil { 36 i.compares = append(i.compares, c) 37 } 38 } 39 } 40 } 41 42 // func WithSliceNoOrder(b bool) Opt { 43 // return func(i *info) { 44 // i.sliceNoOrder = b 45 // } 46 // } 47 48 // WithStripPointerAtFirst set the flag which allow finding the real 49 // targets of the input objects. 50 // 51 // Typically, the two struct pointers will be compared with field 52 // by field rather than comparing its pointer addresses. 53 // 54 // For example, when you diff.Diff(&b1, &b2, diff.WithStripPointerAtFirst(true)), 55 // we compare the fields content of b1 and b2, we don't compare its 56 // pointer addresses at this time. 57 // 58 // The another implicit thing is, this feature also strips `interface{}`/`any` 59 // variable out of to its underlying typed object: a `var lhs any = int64(0)` 60 // will be decoded as `var lhsReal int64 = 0`. 61 // 62 // Even if without stripPointerAtFirst enabled, any input parameters which 63 // are `diff`ing will be striped to underlying dattype if it's wrapped by 64 // `interface{}`. 65 func WithStripPointerAtFirst(b bool) Opt { 66 return func(i *info) { 67 i.stripPtr1st = b 68 } 69 } 70 71 // WithTreatEmptyStructPtrAsNilPtr set the flag which allow a field 72 // with nil pointer to a struct is treated as equal to the pointer 73 // to this field to pointed to an empty struct. 74 // 75 // For example, 76 // 77 // struct A{I int} 78 // struct B( A *A,} 79 // b1, b2 := B{}, B{ &A{}} 80 // diffs := diff.Diff(b1, b2, diff. diff.WithTreatEmptyStructPtrAsNilPtr(true)) 81 // println(diffs) 82 // 83 // And the result is the two struct are equal. the nil pointer `b1.A` 84 // and the empty struct pointer `b2.A` are treated as identity. 85 func WithTreatEmptyStructPtrAsNilPtr(b bool) Opt { 86 return func(i *info) { 87 i.treatEmptyStructPtrAsNil = b 88 } 89 } 90 91 // WithCompareDifferentTypeStructs gives a way to compare two different 92 // type structs with their fields one by one. 93 // 94 // By default, the unmatched fields will be ignored. But you can 95 // disable the feature by calling WithIgnoreUnmatchedFields(false). 96 func WithCompareDifferentTypeStructs(b bool) Opt { 97 return func(i *info) { 98 i.differentTypeStructs = b 99 i.ignoreUnmatchedFields = true 100 } 101 } 102 103 // WithIgnoreUnmatchedFields takes effect except in 104 // WithCompareDifferentTypeStructs(true) mode. It allows those unmatched 105 // fields don't stop the fields comparing processing. 106 // 107 // So, the two different type structs are equivalent even if some 108 // fields are unmatched each others, so long as the matched fields 109 // are equivalent. 110 func WithIgnoreUnmatchedFields(b bool) Opt { 111 return func(i *info) { 112 i.ignoreUnmatchedFields = b 113 } 114 } 115 116 // WithCompareDifferentSizeArrays supports a feature to treat these 117 // two array is equivalent: `[2]string{"1","2"}` and 118 // `[3]string{"1","2",<empty>}`. 119 func WithCompareDifferentSizeArrays(b bool) Opt { 120 return func(i *info) { 121 i.differentSizeArrays = b 122 } 123 }