wildcard

package
v1.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2019 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package wildcard implements simple wildcard matching meant to be used to match URIs and paths against simple patterns. It's less powerful but also less error-prone than regular expressions.

We expose functions to build matchers from simple wildcard patterns. Each pattern is a sequence of segments separated by a separator, usually a forward slash. Each segment in the pattern may be a literal string, or a wildcard. A literal string will be matched exactly, a wildcard will match an arbitrary string.

Two types of wildcards are supported:

(1) A single '*' wildcard will match any literal string that does not contain the separator. It may occur anywhere between two separators in the pattern.

(2) A double '**' wildcard will match anything, including the separator rune. It may only occur at the end of a pattern, after a separator.

Furthermore, the matcher will consider the separator optional if it occurs at the end of a string. This means that, for example, the strings "test://foo/bar" and "test://foo/bar/" are treated as equivalent.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher interface {
	// Matches checks if the given input matches the compiled pattern.
	Matches(string) bool
}

Matcher represents a compiled pattern that can be matched against a string.

func Compile

func Compile(pattern string) (Matcher, error)

Compile creates a new Matcher given a pattern, using '/' as the separator.

Example (DoubleWildcard)
matcher, err := Compile("spiffe://some/*/pattern/**")
if err != nil {
	panic(err)
}

fmt.Printf("%t\n", matcher.Matches("spiffe://some/test/pattern"))
fmt.Printf("%t\n", matcher.Matches("spiffe://some/test/pattern/that/continues"))
Output:

true
true
Example (Simple)
matcher, err := Compile("spiffe://some/*/pattern")
if err != nil {
	panic(err)
}

fmt.Printf("%t\n", matcher.Matches("spiffe://some/test/pattern"))
fmt.Printf("%t\n", matcher.Matches("spiffe://some/test/example"))
Output:

true
false

func CompileList

func CompileList(patterns []string) ([]Matcher, error)

CompileList creates new Matchers given a list patterns, using '/' as the separator.

func CompileWithSeparator

func CompileWithSeparator(pattern string, separator rune) (Matcher, error)

CompileWithSeparator creates a new Matcher given a pattern and separator rune.

func MustCompile

func MustCompile(pattern string) Matcher

MustCompile creates a new Matcher given a pattern, using '/' as the separator, and panics if the given pattern was invalid.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL