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
- type Entry
- type GexMatcher
- type IntervalMatcher
- type ListEntry
- type MatchValue
- type Matcher
- type Options
- type Patrun
- func (p *Patrun) Add(pattern map[string]string, data *string) *Patrun
- func (p *Patrun) Collect(query map[string]string) []string
- func (p *Patrun) CollectExact(query map[string]string) []string
- func (p *Patrun) Find(query map[string]string) (*string, bool)
- func (p *Patrun) FindExact(query map[string]string) (*string, bool)
- func (p *Patrun) Get(pattern map[string]string) (*string, bool)
- func (p *Patrun) List(part map[string]string) []ListEntry
- func (p *Patrun) Remove(pattern map[string]string)
- func (p *Patrun) String() string
- func (p *Patrun) ToJSON() string
- func (p *Patrun) Top() *keymap
- type Router
Constants ¶
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 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 ¶
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 ¶
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) CollectExact ¶
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 ¶
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) Get ¶ added in v0.3.0
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 ¶
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 ¶
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) ToJSON ¶
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.