-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathemail_matcher.go
38 lines (31 loc) · 888 Bytes
/
email_matcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package gomatch
import (
"errors"
"regexp"
)
var emailRe = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
var errNotEmail = errors.New("expected email")
// An EmailMatcher matches email
type EmailMatcher struct {
pattern string
}
// CanMatch returns true if pattern p can be handled
func (m *EmailMatcher) CanMatch(p interface{}) bool {
return isPattern(p, m.pattern)
}
// Match performs value matching against given pattern.
func (m *EmailMatcher) Match(p, v interface{}) (bool, error) {
s, ok := v.(string)
if !ok {
return false, errNotEmail
}
ok = emailRe.MatchString(s)
if !ok {
return false, errNotEmail
}
return true, nil
}
// NewEmailMatcher creates EmailMatcher.
func NewEmailMatcher(pattern string) *EmailMatcher {
return &EmailMatcher{pattern}
}