Skip to content

Commit c3c4714

Browse files
wangfenjinFenjin Wang
andauthored
Add FilterOut method (#80)
Co-authored-by: Fenjin Wang <wangfenjin@bytedance.com>
1 parent e323b24 commit c3c4714

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

diff.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ func StructValues(t string, path []string, s interface{}) (Changelog, error) {
108108
return d.cl, d.structValues(t, path, v)
109109
}
110110

111+
// FilterOut filter out the changes based on path. Paths may contain valid regexp to match items
112+
func (cl *Changelog) FilterOut(path []string) Changelog {
113+
var ncl Changelog
114+
115+
for _, c := range *cl {
116+
if !pathmatch(path, c.Path) {
117+
ncl = append(ncl, c)
118+
}
119+
}
120+
121+
return ncl
122+
}
123+
111124
// Filter filter changes based on path. Paths may contain valid regexp to match items
112125
func (cl *Changelog) Filter(path []string) Changelog {
113126
var ncl Changelog

diff_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,32 @@ func TestFilter(t *testing.T) {
758758
}
759759
}
760760

761+
func TestFilterOut(t *testing.T) {
762+
cases := []struct {
763+
Name string
764+
Filter []string
765+
Expected [][]string
766+
}{
767+
{"simple", []string{"item-1", "subitem"}, [][]string{{"item-2", "subitem"}}},
768+
{"regex", []string{"item-*"}, [][]string{}},
769+
}
770+
771+
cl := diff.Changelog{
772+
{Path: []string{"item-1", "subitem"}},
773+
{Path: []string{"item-2", "subitem"}},
774+
}
775+
776+
for _, tc := range cases {
777+
t.Run(tc.Name, func(t *testing.T) {
778+
ncl := cl.FilterOut(tc.Filter)
779+
assert.Len(t, ncl, len(tc.Expected))
780+
for i, e := range tc.Expected {
781+
assert.Equal(t, e, ncl[i].Path)
782+
}
783+
})
784+
}
785+
}
786+
761787
func TestStructValues(t *testing.T) {
762788
cases := []struct {
763789
Name string

0 commit comments

Comments
 (0)