glider/rule/internal/matcher/integerMatcher.go
2020-12-04 18:49:11 +08:00

30 lines
499 B
Go

package matcher
import (
"github.com/nadoo/glider/log"
"strconv"
)
type IntegerMatcher map[int]struct{}
func NewIntegerMatcher(ports []string) *IntegerMatcher {
m := make(map[int]struct{})
for _, port := range ports {
p, err := strconv.Atoi(port)
if err != nil {
log.F("invalid port: ", port)
continue
}
m[p] = struct{}{}
}
if len(m) == 0 {
return nil
}
return (*IntegerMatcher)(&m)
}
func (m IntegerMatcher) Match(t interface{}) bool {
_, ok := m[t.(int)]
return ok
}