Matcher
Categories:
Overview
Matcher is a string matcher resource.
Technically speaking, it is a reusable component that provides string
and []byte
matching.
It it a reusable resource that is used from other resources.
It is used as, for example,
- path matching
- header value matching
- body content matchin
Defined matcher types are listed in the table.
Match type | Used method | Try on Go Playground |
---|---|---|
Exact | == | https://go.dev/play/p/tzXNQYFeEbm |
Prefix | strings#HasPrefix | https://go.dev/play/p/f_eU7-K49ZV |
Suffix | strings#HasSuffix | https://go.dev/play/p/dOpLnzu74nv |
Contains | strings#Contains | https://go.dev/play/p/tbFRqJTL7vt |
Path | path#Match | https://go.dev/play/p/xY56ZBgCGrO |
FilePath | filepath#Match | https://go.dev/play/p/dHl5dof11ZF |
Regex | regexp#Regexp.Match | https://go.dev/play/p/AjWEC9C_YIh |
RegexPOSIX | regexp#Regexp.Match | https://go.dev/play/p/yP1LIgg0PAe |
Resource Definition
Matcher is defined in the proto/kernel/matcher.proto. It is referred from other resource protos.
1syntax = "proto3";
2package kernel;
3
4import "buf/validate/validate.proto";
5
6option go_package = "github.com/aileron-gateway/aileron-gateway/apis/kernel";
7
8// MatcherSpec is the string or bytes matching specification.
9message MatcherSpec {
10 // [OPTIONAL]
11 // Patterns is the list of matching patterns.
12 // Values must have appropriate expressions for the specified match type.
13 // Default is not set.
14 repeated string Patterns = 1 [json_name = "patterns", (buf.validate.field).repeated.unique = true];
15
16 // [OPTIONAL]
17 // MatchType is the matching method type.
18 // Default is [Exact].
19 MatchType MatchType = 2 [json_name = "matchType"];
20}
21
22// MatchType is the matching methods of string or bytes data.
23// See https://pkg.go.dev/strings for string matching, https://pkg.go.dev/bytes for bytes matching.
24// https://pkg.go.dev/regexp and https://pkg.go.dev/regexp/syntax for regular expression matching.
25enum MatchType {
26 // Exact is the exact matching method type.
27 Exact = 0;
28 // Prefix is the prefix matching method type.
29 // See https://pkg.go.dev/strings#HasPrefix for string matching.
30 // See https://pkg.go.dev/bytes#HasPrefix for bytes matching.
31 Prefix = 1;
32 // Suffix is the suffix matching method type.
33 // See https://pkg.go.dev/strings#HasSuffix for string matching.
34 // See https://pkg.go.dev/bytes#HasSuffix for bytes matching.
35 Suffix = 2;
36 // Contains is the containing matching method type.
37 // See https://pkg.go.dev/strings#Contains for string matching.
38 // See https://pkg.go.dev/bytes#Contains for bytes matching.
39 Contains = 3;
40 // Path is the path matching method type.
41 // See https://pkg.go.dev/path#Match for string and bytes matching.
42 Path = 4;
43 // FilePath is the file path matching method type.
44 // See https://pkg.go.dev/path/filepath#Match for string and bytes matching.
45 FilePath = 5;
46 // Regex is the regular expression matching method type.
47 // See https://pkg.go.dev/regexp#Match and https://pkg.go.dev/regexp/syntax
48 // for string and bytes matching.
49 Regex = 6;
50 // RegexPOSIX is the POSIX regular expression matching method type.
51 // See https://pkg.go.dev/regexp#Match and https://pkg.go.dev/regexp/syntax
52 // for string and bytes matching.
53 RegexPOSIX = 7;
54}
YAML Structure
patterns: <[]string>
matchType: <enum> # MatchType: Exact, Prefix, Suffix, Contains, Path, FilePath, Regex, RegexPOSIX
Examples
Exact
Exact
provides exact matching.
Values are evaluated by ==
operator.
For example:
matchType: Exact
patterns: ["foo", "bar"]
- Match: “foo”, “bar”
- Not match: “baz”, “FOO”
Prefix
Prefix
provides prefix matching.
Values are evaluated by strings#HasPrefix.
For example:
matchType: Prefix
patterns: ["foo", "bar"]
- Match: “foo”, “fooBar”, “barBaz”
- Not match: “baz”, “FOOBAR”
Suffix
Suffix
provides suffix matching.
Values are evaluated by strings#HasSuffix.
For example:
matchType: Suffix
patterns: ["foo", "bar"]
- Match: “foo”, “barfoo”, “bazbar”
- Not match: “Foo”, “fooBar”
Contains
Contains
provides containing matching.
Values are evaluated by strings#Contains.
For example:
matchType: Contains
patterns: ["foo", "bar"]
- Match: “foo”, “barfoo”, “bazbar”
- Not match: “Foo”, “BAR”, “baz”
Path
Path
provides path matching.
Values are evaluated by path#Match.
For example:
matchType: Path
patterns: ["foo/*", "bar/[0-9]?"]
- Match: “foo/”, “foo/bar”, “bar/1”
- Not match: “foo/bar/baz”, “foo”, “bar/baz”
FilePath
FilePath
provides filepath matching.
Values are evaluated by filepath#Match.
For example:
matchType: FilePath
patterns: ["foo/*", "bar/[0-9]?"]
- Match: “foo/”, “foo/bar”, “bar/1”
- Not match: “foo/bar/baz”, “foo”, “bar/baz”
Regex
Regex
provides regular expression matching.
Values are evaluated by regexp#Regexp.Match.
For example:
matchType: Regex
patterns: ["foo.*", "(bar|BAR)"]
- Match: “foo”, “bazfoobaz”, “aliceBARbob”
- Not match: “FOO”, “fo”, “Bar”
Note: Use ^
and $
to represents line starting and ending.
See the documentation of regexp/syntax for details about syntax.
RegexPOSIX
RegexPOSIX
provides regular expression in POSIX matching.
Values are evaluated by regexp#Regexp.Match
and the expressions are parsed by following POSIX rule.
For example:
matchType: RegexPOSIX
patterns: ["foo.*", "(bar|BAR)"]
- Match: “foo”, “bazfoobaz”, “aliceBARbob”
- Not match: “FOO”, “fo”, “Bar”
Note: Use ^
and $
to represents line starting and ending.
See the documentation of regexp/syntax for details about syntax.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.