-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
61 lines (53 loc) · 1.53 KB
/
match.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
// Created by Yakka (https://theyakka.com)
//
// Copyright (c) 2020 Yakka LLC.
// All rights reserved.
// See the LICENSE file for licensing details and requirements.
package cors
import "regexp"
// Match is a generic exact value or "wildcard" (via regex) matcher that can be used
// whenever you need to match things in the system.
type Match struct {
Value string
IsWildcard bool
regex *regexp.Regexp
}
// NewMatch defines a new Match that should be an exact match
func NewMatch(value string) *Match {
return &Match{
Value: value,
IsWildcard: false,
regex: nil,
}
}
// NewWildcardMatch defines a new Match that will have a wildcard component.
// Note: this function will automatically apply boundaries to the pattern
// to allow for exact matching of the pattern only.
func NewWildcardMatch(pattern string) *Match {
boundaryPattern := `\b` + pattern + `\b`
regex := regexp.MustCompile(boundaryPattern)
return &Match{
Value: pattern,
IsWildcard: true,
regex: regex,
}
}
// EM is a convenience function that wraps NewMatch for Exact Matches
func EM(value string) *Match {
return NewMatch(value)
}
// WC is a convenience function that wraps NewMatch for Wildcard Matches.
func WC(pattern string) *Match {
match := NewWildcardMatch(pattern)
return match
}
// Matches evaluates an input vs the Match instance to see if it matches
func (og *Match) Matches(input string) bool {
if !og.IsWildcard {
return og.Value == input
}
if og.regex == nil {
return false
}
return og.regex.MatchString(input)
}