feat: evolve rate-filter to a more generic filter
This commit is contained in:
@ -1,21 +1,34 @@
|
||||
package automation
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// matchProxies filters proxies down to those satisfying every non-empty
|
||||
// dimension of m.
|
||||
func matchProxies(proxies []ProxyRef, m MatchRule) []ProxyRef {
|
||||
// dimension of m. Returns an error if m.NamePattern is an invalid regexp.
|
||||
func matchProxies(proxies []ProxyRef, m MatchRule) ([]ProxyRef, error) {
|
||||
var namePattern *regexp.Regexp
|
||||
if m.NamePattern != "" {
|
||||
re, err := regexp.Compile(m.NamePattern)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid match.name-pattern %q: %w", m.NamePattern, err)
|
||||
}
|
||||
namePattern = re
|
||||
}
|
||||
|
||||
var out []ProxyRef
|
||||
for _, p := range proxies {
|
||||
if len(m.Subscriptions) > 0 && !containsFold(m.Subscriptions, p.Subscription) {
|
||||
continue
|
||||
}
|
||||
if len(m.NameContains) > 0 && !anyContainsFold(m.NameContains, p.OriginalName) {
|
||||
if namePattern != nil && !namePattern.MatchString(p.OriginalName) {
|
||||
continue
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func containsFold(list []string, s string) bool {
|
||||
@ -27,12 +40,3 @@ func containsFold(list []string, s string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func anyContainsFold(keywords []string, s string) bool {
|
||||
lower := strings.ToLower(s)
|
||||
for _, kw := range keywords {
|
||||
if strings.Contains(lower, strings.ToLower(kw)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user