Skip to content

Commit bdad007

Browse files
committed
adding --ignore-values
1 parent 11d85d3 commit bdad007

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

config.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type config struct {
1818
Ignore ignorePatterns `long:"ignore" short:"i" description:"paths to ignore (glob)"`
1919
output
2020
IgnoreExcess bool `long:"ignore-excess" description:"ignore excess keys and arrey elements"`
21+
IgnoreValues bool `long:"ignore-values" description:"ignore scalar's values (only type is compared)"`
2122
OutputReport bool `long:"report" short:"r" description:"output report format"`
2223
}
2324

diff/diff.go

+43-8
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (t Type) String() string {
8888
return "invalid type"
8989
}
9090

91-
// IsExcess can be used in a WalkFn to find values missing from the LHS
91+
// IsExcess returns true if d represent value missing from the LHS (in a map or an array)
9292
func IsExcess(d Differ) bool {
9393
switch d.(type) {
9494
default:
@@ -100,7 +100,7 @@ func IsExcess(d Differ) bool {
100100
}
101101
}
102102

103-
// IsMissing can be used in a WalkFn to find values missing from the RHS
103+
// IsMissing returns true if d represent value missing from the RHS (in a map or an array)
104104
func IsMissing(d Differ) bool {
105105
switch d.(type) {
106106
default:
@@ -112,23 +112,58 @@ func IsMissing(d Differ) bool {
112112
}
113113
}
114114

115+
// IsScalar returns true of d is a diff between two values that can be compared (int, float64, string, ...)
116+
func IsScalar(d Differ) bool {
117+
_, ok := d.(scalar)
118+
119+
return ok
120+
}
121+
122+
// IsTypes returns true if d is a diff between two values of different types that cannot be compared
123+
func IsTypes(d Differ) bool {
124+
_, ok := d.(types)
125+
126+
return ok
127+
}
128+
129+
// IsIgnore returns true if d is a diff created by NewIgnore
130+
func IsIgnore(d Differ) bool {
131+
_, ok := d.(ignore)
132+
133+
return ok
134+
}
135+
136+
// IsMap returns true if d is a diff between towo maps
137+
func IsMap(d Differ) bool {
138+
_, ok := d.(mapDiff)
139+
140+
return ok
141+
}
142+
143+
// IsSlice returns true if d is a diff between towo slices
144+
func IsSlice(d Differ) bool {
145+
_, ok := d.(slice)
146+
147+
return ok
148+
}
149+
115150
type visited struct {
116-
LHS []uintptr
117-
RHS []uintptr
151+
lhs []uintptr
152+
rhs []uintptr
118153
}
119154

120155
func (v *visited) add(lhs, rhs reflect.Value) error {
121156
if canAddr(lhs) {
122-
if inPointers(v.LHS, lhs) {
157+
if inPointers(v.lhs, lhs) {
123158
return ErrCyclic
124159
}
125-
v.LHS = append(v.LHS, lhs.Pointer())
160+
v.lhs = append(v.lhs, lhs.Pointer())
126161
}
127162
if canAddr(rhs) {
128-
if inPointers(v.RHS, rhs) {
163+
if inPointers(v.rhs, rhs) {
129164
return ErrCyclic
130165
}
131-
v.RHS = append(v.RHS, rhs.Pointer())
166+
v.rhs = append(v.rhs, rhs.Pointer())
132167
}
133168

134169
return nil

main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func main() {
3030
os.Exit(statusDiffError)
3131
}
3232

33-
d, err = pruneIgnore(d, conf.IgnoreExcess, conf.Ignore)
33+
d, err = pruneIgnore(d, conf.IgnoreExcess, conf.IgnoreValues, conf.Ignore)
3434
if err != nil {
3535
fmt.Fprintf(os.Stderr, "Error: ignoring failed: %s", err)
3636
os.Exit(statusDiffError)
@@ -53,7 +53,7 @@ func main() {
5353
}
5454
}
5555

56-
func pruneIgnore(d diff.Differ, ingoreExcess bool, ignore ignorePatterns) (diff.Differ, error) {
56+
func pruneIgnore(d diff.Differ, ingoreExcess, ignoreValues bool, ignore ignorePatterns) (diff.Differ, error) {
5757
return diff.Walk(d, func(parent diff.Differ, d diff.Differ, path string) (diff.Differ, error) {
5858
if ignore.Match(path) {
5959
return diff.Ignore()
@@ -63,6 +63,10 @@ func pruneIgnore(d diff.Differ, ingoreExcess bool, ignore ignorePatterns) (diff.
6363
return diff.Ignore()
6464
}
6565

66+
if ignoreValues && diff.IsScalar(d) && d.Diff() == diff.ContentDiffer {
67+
return diff.Ignore()
68+
}
69+
6670
return nil, nil
6771
})
6872
}

0 commit comments

Comments
 (0)