Skip to content

Commit c33b494

Browse files
authored
Merge pull request #4 from yazgazan/adding-ignore-values
Adding --ignore-values
2 parents 11d85d3 + 8305b67 commit c33b494

File tree

6 files changed

+125
-10
lines changed

6 files changed

+125
-10
lines changed

Readme.md

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Application Options:
2828
--indent= indent string (default: "\t")
2929
-t, --show-types show types
3030
--ignore-excess ignore excess keys and arrey elements
31+
--ignore-values ignore scalar's values (only type is compared)
3132
-r, --report output report format
3233
3334
Help Options:
@@ -115,6 +116,17 @@ $ jaydiff --report --show-types --ignore-excess old.json new.json
115116
- .f: float64 42
116117
```
117118

119+
Ignore values (type must still match):
120+
121+
```diff
122+
$ jaydiff --report --show-types --ignore-excess --ignore-values old.json new.json
123+
124+
- .c.b: float64 23
125+
+ .c.b: string 23
126+
- .e: []interface {} []
127+
- .f: float64 42
128+
```
129+
118130
# Ideas
119131

120132
- JayPatch

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

diff/diff_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,58 @@ func testStrings(context string, t *testing.T, test stringTest, ss []string, ind
502502
}
503503
}
504504
}
505+
506+
func TestIsScalar(t *testing.T) {
507+
d, err := Diff(42, 23)
508+
if err != nil {
509+
t.Errorf("Diff(42, 23): unexpected error: %s", err)
510+
return
511+
}
512+
if !IsScalar(d) {
513+
t.Error("IsScalar(Diff(42, 23)) = false, expected true")
514+
}
515+
}
516+
517+
func TestIsTypes(t *testing.T) {
518+
d, err := Diff(42, []string{"hop"})
519+
if err != nil {
520+
t.Errorf("Diff(42, \"hop\"): unexpected error: %s", err)
521+
return
522+
}
523+
if !IsTypes(d) {
524+
t.Error("IsTypes(Diff(42, \"hop\")) = false, expected true")
525+
}
526+
}
527+
528+
func TestIsIgnore(t *testing.T) {
529+
d, err := Ignore()
530+
if err != nil {
531+
t.Errorf("Ignore(): unexpected error: %s", err)
532+
return
533+
}
534+
if !IsIgnore(d) {
535+
t.Error("IsIgnore(Ignore()) = false, expected true")
536+
}
537+
}
538+
539+
func TestIsMap(t *testing.T) {
540+
d, err := Diff(map[int]bool{0: true, 1: false}, map[int]bool{0: false, 1: true})
541+
if err != nil {
542+
t.Errorf("Diff(map[int]bool, map[int]bool): unexpected error: %s", err)
543+
return
544+
}
545+
if !IsMap(d) {
546+
t.Error("IsMap(Diff(map[int]bool, map[int]bool)) = false, expected true")
547+
}
548+
}
549+
550+
func TestIsSlice(t *testing.T) {
551+
d, err := Diff([]int{0, 1}, []int{2, 3})
552+
if err != nil {
553+
t.Errorf("Diff([]int, []int): unexpected error: %s", err)
554+
return
555+
}
556+
if !IsSlice(d) {
557+
t.Error("IsSlice(Diff(map{...}, map{...})) = false, expected true")
558+
}
559+
}

generate_readme_examples.sh

+8
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,12 @@ $ jaydiff --report --show-types --ignore-excess old.json new.json
3838
$(./jaydiff --report --ignore-excess --indent=' ' --show-types test_files/lhs.json test_files/rhs.json)
3939
$(echo '```')
4040
41+
Ignore values (type must still match):
42+
43+
$(echo '```diff')
44+
$ jaydiff --report --show-types --ignore-excess --ignore-values old.json new.json
45+
46+
$(./jaydiff --report --ignore-excess --ignore-values --indent=' ' --show-types test_files/lhs.json test_files/rhs.json)
47+
$(echo '```')
48+
4149
EOF

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)