github.com/kelleygo/clashcore@v1.0.2/component/geodata/attr.go (about)

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