Skip to content

Commit

Permalink
Add FilterOut method (#80)
Browse files Browse the repository at this point in the history
Co-authored-by: Fenjin Wang <[email protected]>
  • Loading branch information
wangfenjin and Fenjin Wang authored Feb 10, 2022
1 parent e323b24 commit c3c4714
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ func StructValues(t string, path []string, s interface{}) (Changelog, error) {
return d.cl, d.structValues(t, path, v)
}

// FilterOut filter out the changes based on path. Paths may contain valid regexp to match items
func (cl *Changelog) FilterOut(path []string) Changelog {
var ncl Changelog

for _, c := range *cl {
if !pathmatch(path, c.Path) {
ncl = append(ncl, c)
}
}

return ncl
}

// Filter filter changes based on path. Paths may contain valid regexp to match items
func (cl *Changelog) Filter(path []string) Changelog {
var ncl Changelog
Expand Down
26 changes: 26 additions & 0 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,32 @@ func TestFilter(t *testing.T) {
}
}

func TestFilterOut(t *testing.T) {
cases := []struct {
Name string
Filter []string
Expected [][]string
}{
{"simple", []string{"item-1", "subitem"}, [][]string{{"item-2", "subitem"}}},
{"regex", []string{"item-*"}, [][]string{}},
}

cl := diff.Changelog{
{Path: []string{"item-1", "subitem"}},
{Path: []string{"item-2", "subitem"}},
}

for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
ncl := cl.FilterOut(tc.Filter)
assert.Len(t, ncl, len(tc.Expected))
for i, e := range tc.Expected {
assert.Equal(t, e, ncl[i].Path)
}
})
}
}

func TestStructValues(t *testing.T) {
cases := []struct {
Name string
Expand Down

0 comments on commit c3c4714

Please sign in to comment.