attr.go 969 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package geodata
  2. import (
  3. "strings"
  4. "github.com/metacubex/mihomo/component/geodata/router"
  5. )
  6. type AttributeList struct {
  7. matcher []AttributeMatcher
  8. }
  9. func (al *AttributeList) Match(domain *router.Domain) bool {
  10. for _, matcher := range al.matcher {
  11. if !matcher.Match(domain) {
  12. return false
  13. }
  14. }
  15. return true
  16. }
  17. func (al *AttributeList) IsEmpty() bool {
  18. return len(al.matcher) == 0
  19. }
  20. func parseAttrs(attrs []string) *AttributeList {
  21. al := new(AttributeList)
  22. for _, attr := range attrs {
  23. trimmedAttr := strings.ToLower(strings.TrimSpace(attr))
  24. if len(trimmedAttr) == 0 {
  25. continue
  26. }
  27. al.matcher = append(al.matcher, BooleanMatcher(trimmedAttr))
  28. }
  29. return al
  30. }
  31. type AttributeMatcher interface {
  32. Match(*router.Domain) bool
  33. }
  34. type BooleanMatcher string
  35. func (m BooleanMatcher) Match(domain *router.Domain) bool {
  36. for _, attr := range domain.Attribute {
  37. if strings.EqualFold(attr.GetKey(), string(m)) {
  38. return true
  39. }
  40. }
  41. return false
  42. }