Matcher

Overview

Matcher は文字列マッチングのためのリソースです。
技術的には、string および []byte に対するマッチング機能を提供する再利用可能なコンポーネントです。
これは他のリソースから利用される再利用可能なリソースです。

次のような用途で使用されます:

  • パスのマッチング
  • ヘッダー値のマッチング
  • ボディ内容のマッチング

定義済みのマッチタイプは以下の表に示します。

Match typeUsed methodTry on Go Playground
Exact==https://go.dev/play/p/tzXNQYFeEbm
Prefixstrings#HasPrefixhttps://go.dev/play/p/f_eU7-K49ZV
Suffixstrings#HasSuffixhttps://go.dev/play/p/dOpLnzu74nv
Containsstrings#Containshttps://go.dev/play/p/tbFRqJTL7vt
Pathpath#Matchhttps://go.dev/play/p/xY56ZBgCGrO
FilePathfilepath#Matchhttps://go.dev/play/p/dHl5dof11ZF
Regexregexp#Regexp.Matchhttps://go.dev/play/p/AjWEC9C_YIh
RegexPOSIXregexp#Regexp.Matchhttps://go.dev/play/p/yP1LIgg0PAe

Resource Definition

Matcher は proto/kernel/matcher.proto に定義されています。
このリソースは他のリソースの proto から参照されます。

 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>

Examples

Exact

Exact は完全一致のマッチングを行います。 == 演算子によって評価されます。

例:

matchType: Exact
patterns: ["foo", "bar"]
  • 一致: “foo”, “bar”
  • 不一致: “baz”, “FOO”

Prefix

Prefix は接頭語(プレフィックス)でのマッチングを行います。 値は strings#HasPrefix によって評価されます。

例:

matchType: Prefix
patterns: ["foo", "bar"]
  • 一致: “foo”, “fooBar”, “barBaz”
  • 不一致: “baz”, “FOOBAR”

Suffix

Suffix は接尾語(サフィックス)でのマッチングを行います。 値は strings#HasSuffix によって評価されます。

例:

matchType: Suffix
patterns: ["foo", "bar"]
  • 一致: “foo”, “barfoo”, “bazbar”
  • 不一致: “Foo”, “fooBar”

Contains

Contains は部分一致(含む)マッチングを行います。 値は strings#Contains によって評価されます。

例:

matchType: Contains
patterns: ["foo", "bar"]
  • 一致: “foo”, “barfoo”, “bazbar”
  • 不一致: “Foo”, “BAR”, “baz”

Path

Path provides path matching. 値は path#Match によって評価されます。

例:

matchType: Path
patterns: ["foo/*", "bar/[0-9]?"]
  • 一致: “foo/”, “foo/bar”, “bar/1”
  • 不一致: “foo/bar/baz”, “foo”, “bar/baz”

FilePath

FilePath provides filepath matching. 値は filepath#Match によって評価されます。

例:

matchType: FilePath
patterns: ["foo/*", "bar/[0-9]?"]
  • 一致: “foo/”, “foo/bar”, “bar/1”
  • 不一致: “foo/bar/baz”, “foo”, “bar/baz”

Regex

Regex provides regular expression matching. 値は regexp#Regexp.Match によって評価されます。

例:

matchType: Regex
patterns: ["foo.*", "(bar|BAR)"]
  • 一致: “foo”, “bazfoobaz”, “aliceBARbob”
  • 不一致: “FOO”, “fo”, “Bar”

※ 行頭や行末を指定したい場合は ^$ を使用してください。

詳しい構文は regexp/syntax を参照してください。

RegexPOSIX

RegexPOSIX provides regular expression in POSIX matching. 値はPOSIX表記の正規表現としてパースされた regexp#Regexp.Match によって評価されます。

例:

matchType: RegexPOSIX
patterns: ["foo.*", "(bar|BAR)"]
  • 一致: “foo”, “bazfoobaz”, “aliceBARbob”
  • 不一致: “FOO”, “fo”, “Bar”

※ 行頭や行末を指定したい場合は ^$ を使用してください。

詳しい構文は regexp/syntax を参照してください。


最終更新 June 7, 2025: add japanese (f2a41f1)