-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exclude option and support filter option
Add exclude option & support regular expression on filter option Split options to filter or exclude by regular expression
- Loading branch information
1 parent
416d58b
commit 4cf6fb5
Showing
4 changed files
with
128 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,93 @@ | ||
package dyff | ||
|
||
import "github.com/gonvenience/ytbx" | ||
import "regexp" | ||
|
||
func (r Report) filter(f func(string) bool) (result Report) { | ||
result = Report{ | ||
From: r.From, | ||
To: r.To, | ||
} | ||
|
||
for _, diff := range r.Diffs { | ||
diffPathString := diff.Path.String() | ||
if f(diffPathString) { | ||
result.Diffs = append(result.Diffs, diff) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// Filter accepts YAML paths as input and returns a new report with differences for those paths only | ||
func (r Report) Filter(paths ...*ytbx.Path) (result Report) { | ||
func (r Report) Filter(paths ...string) (result Report) { | ||
if len(paths) == 0 { | ||
return r | ||
} | ||
|
||
result = Report{ | ||
From: r.From, | ||
To: r.To, | ||
return r.filter(func(s string) bool { | ||
for _, path := range paths { | ||
if path == s { | ||
return true | ||
} | ||
} | ||
return false | ||
}) | ||
} | ||
|
||
// Exclude accepts YAML paths as input and returns a new report with differences without those paths | ||
func (r Report) Exclude(paths ...string) (result Report) { | ||
if len(paths) == 0 { | ||
return r | ||
} | ||
|
||
pathsMap := make(map[string]struct{}) | ||
return r.filter(func(s string) bool { | ||
for _, path := range paths { | ||
if path == s { | ||
return false | ||
} | ||
} | ||
return true | ||
}) | ||
} | ||
|
||
for _, path := range paths { | ||
pathsMap[path.String()] = struct{}{} | ||
// FilterRegexp accepts regular expressions as input and returns a new report with differences for matching those patterns | ||
func (r Report) FilterRegexp(pattern ...string) (result Report) { | ||
if len(pattern) == 0 { | ||
return r | ||
} | ||
|
||
for _, diff := range r.Diffs { | ||
diffPathString := diff.Path.String() | ||
if _, ok := pathsMap[diffPathString]; ok { | ||
result.Diffs = append(result.Diffs, diff) | ||
regexps := make([]*regexp.Regexp, len(pattern)) | ||
for i := range pattern { | ||
regexps[i] = regexp.MustCompile(pattern[i]) | ||
} | ||
|
||
return r.filter(func(s string) bool { | ||
for _, regexp := range regexps { | ||
if regexp.MatchString(s) { | ||
return true | ||
} | ||
} | ||
return false | ||
}) | ||
} | ||
|
||
// ExcludeRegexp accepts regular expressions as input and returns a new report with differences for not matching those patterns | ||
func (r Report) ExcludeRegexp(pattern ...string) (result Report) { | ||
if len(pattern) == 0 { | ||
return r | ||
} | ||
|
||
return result | ||
regexps := make([]*regexp.Regexp, len(pattern)) | ||
for i := range pattern { | ||
regexps[i] = regexp.MustCompile(pattern[i]) | ||
} | ||
|
||
return r.filter(func(s string) bool { | ||
for _, regexp := range regexps { | ||
if regexp.MatchString(s) { | ||
return false | ||
} | ||
} | ||
return true | ||
}) | ||
} |