matcher

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2023 License: Apache-2.0 Imports: 9 Imported by: 1

Documentation

Overview

Package matcher provides a generic map with regex keys, that can be used to match items to paths, e.g. when using middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher[T any] struct {
	// contains filtered or unexported fields
}

Matcher is a map with regular expressions as keys. It is used to add middleware on a path based level.

In addition to setting regex selectors for each item, they are also de-duplocated with a name key, to make sure each item is only added once.

func NewMatcher

func NewMatcher[T any](plugins *container.Plugins[T]) Matcher[T]

NewMatcher creates a new matcher object.

func (*Matcher[T]) Add

func (m *Matcher[T]) Add(selector, name string, item T)

Add will add an item to every item the selector matches. The selector is a regexp.

Example selector:

  • /*
  • /echo
  • /echo/*
  • /echo[1-9]
  • suffix$

func (*Matcher[T]) AddPlugin

func (m *Matcher[T]) AddPlugin(selector, plugin string) error

AddPlugin will add plugin item, with a selector.

func (Matcher[T]) Len

func (m Matcher[T]) Len() int

Len returns the total number of items defined.

func (*Matcher[T]) Match

func (m *Matcher[T]) Match(operation string) []T

Match will fetch the list of items that match against a path.

func (*Matcher[T]) UnmarshalJSON

func (m *Matcher[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON will unmarshal a JSON file into the matcher. JSON can contain either a string or <name, selector> map.

All items provided through the config need to be registered as plugins. All config will add to, not replace the exisiting items.

JSON config example:

{
   "middleware":[
      "abc",
      {
         "name":"def",
         "selector":"/foo"
      }
   ]
}

func (*Matcher[T]) UnmarshalYAML

func (m *Matcher[T]) UnmarshalYAML(data *yaml.Node) error

UnmarshalYAML will unmarshal a yaml file into the matcher. Yaml can contain either a string or <name, selector> map.

All items provided through the config need to be registered as plugins. All config will add to, not replace the exisiting items.

Yaml config example:

middleware:
 - abc
 - name: def
   selector: "/foo"

func (*Matcher[T]) Use

func (m *Matcher[T]) Use(name string, item T)

Use will use the elements provided on all paths.

Jump to

Keyboard shortcuts

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