-
Notifications
You must be signed in to change notification settings - Fork 0
/
pruner.go
113 lines (95 loc) · 2.41 KB
/
pruner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package bacom
import (
"github.com/pkg/errors"
"github.com/yazgazan/jaydiff/diff"
"github.com/yazgazan/jaydiff/jpath"
)
// Prune returns a diff.Differ, stripping the diff tree of the following differences:
//
// - Excess keys in the right hand side
// - Excess and Missing values in slices
// - Values of the same type with different content (excluding slices and maps)
// - Type difference where null is involved
func Prune(d diff.Differ, ignoreNull bool) diff.Differ {
d, err := diff.Walk(d, func(parent diff.Differ, d diff.Differ, path string) (diff.Differ, error) {
switch {
case diff.IsScalar(d) && d.Diff() == diff.ContentDiffer:
fallthrough
case ignoreNull && isNil(d):
fallthrough
case diff.IsExcess(d):
fallthrough
case diff.IsSlice(parent) && diff.IsMissing(d):
return diff.Ignore()
}
return nil, nil
})
if err != nil {
panic(errors.Wrap(err, "impossible error while pruning"))
}
return d
}
func isNil(d diff.Differ) bool {
lhs, err := diff.LHS(d)
if err != nil {
return false
}
if lhs == nil {
return true
}
rhs, err := diff.RHS(d)
if err != nil {
return false
}
if rhs == nil {
return true
}
return false
}
// IgnorePrunner can be used to ignore json paths in a diff tree
type IgnorePrunner []string
// Prune Removes ignored diff branches from the diff tree
func (p IgnorePrunner) Prune(d diff.Differ) diff.Differ {
if len(p) == 0 {
return d
}
d, err := diff.Walk(d, func(parent diff.Differ, d diff.Differ, path string) (diff.Differ, error) {
if pathMatches(p, path) {
return diff.Ignore()
}
return nil, nil
})
if err != nil {
panic(errors.Wrap(err, "impossible error while pruning"))
}
return d
}
// IgnoreMissingPrunner can be used to ignore missing json paths (from the right hand side) in a diff tree
type IgnoreMissingPrunner []string
// Prune Removes ignored diff branches from the diff tree
func (p IgnoreMissingPrunner) Prune(d diff.Differ) diff.Differ {
if len(p) == 0 {
return d
}
d, err := diff.Walk(d, func(parent diff.Differ, d diff.Differ, path string) (diff.Differ, error) {
if !diff.IsMissing(d) {
return nil, nil
}
if pathMatches(p, path) {
return diff.Ignore()
}
return nil, nil
})
if err != nil {
panic(errors.Wrap(err, "impossible error while pruning"))
}
return d
}
func pathMatches(paths []string, path string) bool {
for _, p := range paths {
if jpath.HasSuffix(path, p) {
return true
}
}
return false
}