-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add exclude option & filter or exclude by regular expression #214
Add exclude option & filter or exclude by regular expression #214
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. I ran into a regression with the code changes. Can you have a look at my suggestion let me know what you think?
To: r.To, | ||
return r.filter(func(s string) bool { | ||
for _, path := range paths { | ||
if path == s { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is causing a regression when using Dot style paths, e.g. yaml.foo.bar
instead of /yaml/foo/bar
. I played around with your code and got it to work with the following file:
package dyff
import (
"regexp"
"github.com/gonvenience/ytbx"
)
func (r Report) filter(hasPath func(*ytbx.Path) bool) (result Report) {
result = Report{
From: r.From,
To: r.To,
}
for _, diff := range r.Diffs {
if hasPath(diff.Path) {
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 ...string) (result Report) {
if len(paths) == 0 {
return r
}
return r.filter(func(filterPath *ytbx.Path) bool {
for _, pathString := range paths {
path, err := ytbx.ParsePathStringUnsafe(pathString)
if err == nil && path.String() == filterPath.String() {
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
}
return r.filter(func(filterPath *ytbx.Path) bool {
for _, pathString := range paths {
path, err := ytbx.ParsePathStringUnsafe(pathString)
if err == nil && path.String() == filterPath.String() {
return false
}
}
return true
})
}
// 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
}
regexps := make([]*regexp.Regexp, len(pattern))
for i := range pattern {
regexps[i] = regexp.MustCompile(pattern[i])
}
return r.filter(func(filterPath *ytbx.Path) bool {
for _, regexp := range regexps {
if regexp.MatchString(filterPath.String()) {
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
}
regexps := make([]*regexp.Regexp, len(pattern))
for i := range pattern {
regexps[i] = regexp.MustCompile(pattern[i])
}
return r.filter(func(filterPath *ytbx.Path) bool {
for _, regexp := range regexps {
if regexp.MatchString(filterPath.String()) {
return false
}
}
return true
})
}
Since there are deletions and new lines, I cannot use the suggestions feature in a good way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving PR to be merged into temporary feature branch for further processing.
Fix #173
I've added three options to
between
subcommand.This feature is useful when you have a lot of things you want to filter or exclude.
--exclude
--filter-regexp
--exclude-regexp