patrun

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

patrun for Go

patrun matches a map[string]string against a set of patterns, and gives the data of the pattern that matches most specifically. This is the Go port of the JavaScript package, and it gives the same answers to the same patterns.

The documentation of both ports is at rjrodger.github.io/patrun, and the Go API reference states this package in full.

Install

go get github.com/rjrodger/patrun/go

Example

package main

import (
	"fmt"

	patrun "github.com/rjrodger/patrun/go"
)

func s(v string) *string { return &v }

func main() {
	pm := patrun.New()
	pm.Add(map[string]string{"a": "1"}, s("A"))
	pm.Add(map[string]string{"a": "1", "b": "2"}, s("AB"))

	d, ok := pm.Find(map[string]string{"a": "1", "b": "2"})
	fmt.Println(*d, ok) // AB true
}

A query matches a pattern when it has every key of the pattern, and a pattern with more matching keys beats one with fewer. A pattern's data is a *string, and nil is no data. Build your first router in Go is a lesson that starts from here.

Licence

Copyright (c) 2013-2025, Richard Rodger and other contributors. MIT: see LICENSE.

Documentation

Overview

Package patrun provides a fast pattern matcher on map[string]string properties.

Patterns are sets of key-value pairs. A pattern matches a query if all keys in the pattern are present in the query with the same values. A pattern value of "*" takes any value: the query need only have the key. When multiple patterns match, the most specific (most keys matched) wins. Patterns that match equally many keys are ranked by their keys in alphabetical order, and then an exact value ranks before "*". Options.Order names keys that come before the others in that key order.

A pattern is a map that is not nil, and none of its keys is the empty string. Add and Remove panic if given anything else. A query may be any map: nothing matches a nil query, and a key that is the empty string is one that no pattern has.

A Patrun has no lock. Find, FindExact, Collect, CollectExact, Get, List, String and ToJSON only read it, so any number of goroutines may call them at once. Add and Remove change it, and must not run at the same time as any other call: add the patterns first and then share the router, or guard it with a sync.RWMutex.

Index

Constants

View Source
const Version = "0.4.0"

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry added in v0.4.0

type Entry = ListEntry

Entry is ListEntry, by the name that the TypeScript port and the definition in spec/ give it: PatrunEntry and Entry.

type GexMatcher

type GexMatcher struct{}

GexMatcher handles glob-expression patterns using the gex library.

func (*GexMatcher) Make

func (m *GexMatcher) Make(key, fix string) (MatchValue, bool)

type IntervalMatcher

type IntervalMatcher struct{}

IntervalMatcher handles numeric interval/range patterns.

func (*IntervalMatcher) Make

func (m *IntervalMatcher) Make(key, fix string) (MatchValue, bool)

type ListEntry

type ListEntry struct {
	Pattern map[string]string
	Data    string
}

ListEntry is one result from List: a pattern with the data it holds.

type MatchValue

type MatchValue interface {
	Match(val string) bool
	Same(other MatchValue) bool
	Kind() string
	Fix() string
	Keymap() *keymap
	SetKeymap(km *keymap)
}

MatchValue represents a non-literal match (gex glob, interval, etc.).

type Matcher

type Matcher interface {
	Make(key, fix string) (MatchValue, bool)
}

Matcher produces MatchValues for pattern values that it recognises.

type Options

type Options struct {
	Gex      bool
	Interval bool

	// Order names the keys that come first in the router's key order, in
	// the order given. Every other key comes after them, in the order of
	// strings. With none, the key order is the order of strings.
	//
	// This is the order in which a pattern's keys are laid out in the tree,
	// so it is the order that decides between patterns with as many pairs:
	// for Find, for Collect and for List. Each key is named once, and none
	// is the empty string, or New panics.
	Order []string
}

Options configures a Patrun instance.

type Patrun

type Patrun struct {
	// contains filtered or unexported fields
}

Patrun is the router: the patterns, and the data that each holds.

func New

func New(opts ...Options) *Patrun

New creates a new Patrun instance with the given options.

It panics if Options.Order names the empty string, which is not a key, or names a key twice. A key order is written in the program, so one that cannot be meant is a mistake in it, as a pattern that Add cannot take is. The order is copied: a change to the slice afterwards changes nothing.

func (*Patrun) Add

func (p *Patrun) Add(pattern map[string]string, data *string) *Patrun

Add inserts a pattern with associated data. With nil data it leaves the pattern holding nothing, as Remove does, and makes nothing in the tree for it.

It panics if the pattern is nil or has the empty string as a key, as http.ServeMux.Handle does for a pattern that it cannot take. The router is then left as it was.

func (*Patrun) Collect

func (p *Patrun) Collect(query map[string]string) []string

Collect returns all matching data from wider to narrower patterns.

func (*Patrun) CollectExact

func (p *Patrun) CollectExact(query map[string]string) []string

CollectExact returns the data of every pattern that matches the query and has every key of it, in the order of Find. Such patterns have the same keys, and differ in where they have a matcher's value or the star.

func (*Patrun) Find

func (p *Patrun) Find(query map[string]string) (*string, bool)

Find looks up a pattern. Returns the data string and whether it was found. Nothing matches a nil query, not even the empty pattern.

func (*Patrun) FindExact

func (p *Patrun) FindExact(query map[string]string) (*string, bool)

FindExact looks up a pattern with exact matching (all keys must match).

func (*Patrun) Get added in v0.3.0

func (p *Patrun) Get(pattern map[string]string) (*string, bool)

Get returns the data held at exactly a pattern, and whether the pattern holds any. It looks for the pattern's own node, as Add would reach it: a value is had exactly, a glob and the star as themselves and not as tests of other values, and an interval by any of its spellings.

What cannot be a pattern holds nothing: for nil, and for a map with the empty string as a key, it returns nil and false, and does not panic.

func (*Patrun) List

func (p *Patrun) List(part map[string]string) []ListEntry

List returns the patterns that hold data and have every pair of a partial pattern. The values of a key come in ranking order: the exact values, in the order of listedValues, then the matchers' values, the globs and then the intervals, each in their own order, and then the star.

A value of the partial pattern is had exactly, or is "*", which asks only that a pattern has the key. It is never a test of other values: x* asks for the glob x* itself, and not for values that x* would take. An interval is asked for by any of its spellings. An empty or nil partial pattern returns all. One with the empty string as a key returns none: no pattern has such a pair.

func (*Patrun) Remove

func (p *Patrun) Remove(pattern map[string]string)

Remove leaves a pattern holding nothing. It looks for the pattern's own node, as Add would reach it, so a pattern that was never added changes nothing. Like Add, it panics if the pattern is nil or has the empty string as a key.

func (*Patrun) String

func (p *Patrun) String() string

String returns a human-readable representation.

func (*Patrun) ToJSON

func (p *Patrun) ToJSON() string

ToJSON returns the internal tree as JSON, with the names that the TypeScript port's toJSON gives: for each node k, its key, v, the node of each of the key's values, g, the matchers' values for the key, s, the skip path, and d, its data. They are written in that order, and the values of a key in the order in which List gives them, so the text depends on the patterns alone. A matcher's value is written as its kind, its fix, which is the pattern's text, its meta and its keymap, the node it leads to.

func (*Patrun) Top

func (p *Patrun) Top() *keymap

Top returns the internal top-level keymap (for inspection).

type Router added in v0.4.0

type Router = Patrun

Router is Patrun, by the name that the TypeScript port and the definition in spec/ give it: PatrunRouter and Router.

Jump to

Keyboard shortcuts

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