-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_matcher.go
112 lines (87 loc) · 2.55 KB
/
string_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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package hex
import (
"fmt"
"regexp"
)
// StringMatcher is used for matching parts of a request that can only ever be strings, such as the HTTP Method,
// path, query string/header/form keys (not values, which can be arrays), etc.
type StringMatcher func(string) bool
// makeStringMatcher takes as input a string, regular expression, or StringMatcher and returns a StringMatcher
func makeStringMatcher(arg interface{}) (stringMatcher, error) {
if str, ok := arg.(string); ok {
return &stringLiteralMatcher{str: str}, nil
} else if re, ok := arg.(*regexp.Regexp); ok {
return &stringRegexMatcher{pattern: re}, nil
} else if sm, ok := arg.(func(string) bool); ok {
return &funcStringMatcher{fn: sm}, nil
} else if c, ok := arg.(MatchConst); ok {
if c == Any {
return &funcStringMatcher{
fn: func(string) bool { return true },
}, nil
} else if c == None {
return &funcStringMatcher{
fn: func(string) bool { return false },
}, nil
}
}
return nil, fmt.Errorf("Cannot use value %v when matching against strings", arg)
}
func mustMakeStringMatcher(value interface{}) stringMatcher {
if m, err := makeStringMatcher(value); err != nil {
panic(err)
} else {
return m
}
}
// MatchConst is used to define some built-in matchers with predefined
// behavior, namely All or None
type MatchConst int
const (
// Any matches anything, matching against Any will always return true
Any MatchConst = 1
// None matches nothing, matching against None will always return false
None MatchConst = 2
)
type stringMatcher interface {
match(string) bool
String() string
}
type stringLiteralMatcher struct {
str string
}
var _ stringMatcher = &stringLiteralMatcher{}
func (s *stringLiteralMatcher) String() string {
return s.str
}
func (s *stringLiteralMatcher) match(candidate string) bool {
return s.str == candidate
}
type stringRegexMatcher struct {
pattern *regexp.Regexp
}
var _ stringMatcher = &stringRegexMatcher{}
func (s *stringRegexMatcher) String() string {
return s.pattern.String()
}
func (s *stringRegexMatcher) match(candidate string) bool {
return s.pattern.MatchString(candidate)
}
type noStringMatcher struct{}
var _ stringMatcher = &noStringMatcher{}
func (*noStringMatcher) match(string) bool {
return false
}
func (*noStringMatcher) String() string {
return ""
}
type funcStringMatcher struct {
fn StringMatcher
}
var _ stringMatcher = &funcStringMatcher{}
func (s *funcStringMatcher) match(candidate string) bool {
return s.fn(candidate)
}
func (s *funcStringMatcher) String() string {
return "custom string matching function"
}