Skip to content
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

filter/: fix black white list caseSensitive bug #342

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions pkg/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func (t *Table) String() string {
return fmt.Sprintf("`%s`", t.Schema)
}

// Clone clones a new filter.Table
func (t *Table) Clone() *Table {
return &Table{
Schema: t.Schema,
Name: t.Name,
}
}

type cache struct {
sync.RWMutex
items map[string]ActionType // `schema`.`table` => do/ignore
Expand Down Expand Up @@ -277,13 +285,14 @@ func (f *Filter) ApplyOn(stbs []*Table) []*Table {

var tbs []*Table
for _, tb := range stbs {
newTb := tb.Clone()
if !f.caseSensitive {
tb.Schema = strings.ToLower(tb.Schema)
tb.Name = strings.ToLower(tb.Name)
newTb.Schema = strings.ToLower(newTb.Schema)
newTb.Name = strings.ToLower(newTb.Name)
}

if f.Match(tb) {
tbs = append(tbs, tb)
if f.Match(newTb) {
tbs = append(tbs, newTb)
}
}

Expand All @@ -295,16 +304,17 @@ func (f *Filter) Match(tb *Table) bool {
if f == nil || f.rules == nil {
return true
}
newTb := tb.Clone()
if !f.caseSensitive {
tb.Schema = strings.ToLower(tb.Schema)
tb.Name = strings.ToLower(tb.Name)
newTb.Schema = strings.ToLower(newTb.Schema)
newTb.Name = strings.ToLower(newTb.Name)
}

name := tb.String()
name := newTb.String()
do, exist := f.c.query(name)
if !exist {
do = ActionType(f.filterOnSchemas(tb) && f.filterOnTables(tb))
f.c.set(tb.String(), do)
do = ActionType(f.filterOnSchemas(newTb) && f.filterOnTables(newTb))
f.c.set(newTb.String(), do)
}
return do == Do
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ import (
. "github.com/pingcap/check"
)

func cloneTables(tbs []*Table) []*Table {
if tbs == nil {
return nil
}
newTbs := make([]*Table, 0, len(tbs))
for _, tb := range tbs {
newTbs = append(newTbs, tb.Clone())
}
return newTbs
}

func (s *testFilterSuite) TestFilterOnSchema(c *C) {
cases := []struct {
rules *Rules
Expand Down Expand Up @@ -148,8 +159,10 @@ func (s *testFilterSuite) TestFilterOnSchema(c *C) {
for _, t := range cases {
ft, err := New(t.caseSensitive, t.rules)
c.Assert(err, IsNil)
originInput := cloneTables(t.Input)
got := ft.ApplyOn(t.Input)
c.Logf("got %+v, expected %+v", got, t.Output)
c.Assert(originInput, DeepEquals, t.Input)
c.Assert(got, DeepEquals, t.Output)
}
}
Expand Down Expand Up @@ -196,13 +209,16 @@ func (s *testFilterSuite) TestCaseSensitive(c *C) {
}

r, err = New(false, rules)
c.Assert(err, IsNil)
inputTable = &Table{"bar", "a"}
c.Assert(r.Match(inputTable), IsTrue)

c.Assert(err, IsNil)

inputTable = &Table{"BAR", "a"}
originInputTable := inputTable.Clone()
c.Assert(r.Match(inputTable), IsTrue)
c.Assert(originInputTable, DeepEquals, inputTable)
}

func (s *testFilterSuite) TestInvalidRegex(c *C) {
Expand Down