mihomo/component/geodata/attr.go

60 lines
1.1 KiB
Go
Raw Normal View History

2021-11-17 16:03:47 +08:00
package geodata
import (
"strings"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/component/geodata/router"
2021-11-17 16:03:47 +08:00
)
type AttributeList struct {
matcher []BooleanMatcher
2021-11-17 16:03:47 +08:00
}
func (al *AttributeList) Match(domain *router.Domain) bool {
for _, matcher := range al.matcher {
if !matcher.Match(domain) {
return false
}
}
return true
}
func (al *AttributeList) IsEmpty() bool {
return len(al.matcher) == 0
}
func (al *AttributeList) String() string {
matcher := make([]string, len(al.matcher))
for i, match := range al.matcher {
matcher[i] = string(match)
}
return strings.Join(matcher, ",")
}
2021-11-17 16:03:47 +08:00
func parseAttrs(attrs []string) *AttributeList {
al := new(AttributeList)
for _, attr := range attrs {
trimmedAttr := strings.ToLower(strings.TrimSpace(attr))
if len(trimmedAttr) == 0 {
continue
}
al.matcher = append(al.matcher, BooleanMatcher(trimmedAttr))
}
return al
}
type AttributeMatcher interface {
Match(*router.Domain) bool
}
type BooleanMatcher string
func (m BooleanMatcher) Match(domain *router.Domain) bool {
for _, attr := range domain.Attribute {
if strings.EqualFold(attr.GetKey(), string(m)) {
return true
}
}
return false
}