-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
key filter support regex and optimize code (#883)
- Loading branch information
Showing
4 changed files
with
149 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package filter | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// keys pattern | ||
type KeysPattern struct { | ||
regList []*regexp.Regexp | ||
} | ||
|
||
// new keys patterns | ||
func NewKeysPattern(patternList01 []string) (ret *KeysPattern, err error) { | ||
ret = &KeysPattern{ | ||
regList: []*regexp.Regexp{}, | ||
} | ||
for _, k01 := range patternList01 { | ||
k01 = strings.TrimSpace(k01) | ||
if k01 == "" { | ||
continue | ||
} | ||
regItem, err := regexp.Compile(k01) | ||
if err != nil { | ||
err = fmt.Errorf("%s regexp.Compile fail,err:%v", regItem, err) | ||
return nil, err | ||
} | ||
ret.regList = append(ret.regList, regItem) | ||
} | ||
return ret, nil | ||
} | ||
|
||
// match Key | ||
func (f *KeysPattern) MatchKey(k01 string) bool { | ||
if len(f.regList) == 0 { | ||
return false | ||
} | ||
for _, reg01 := range f.regList { | ||
regItem := reg01 | ||
if regItem.MatchString(k01) == true { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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