Documentation
¶
Overview ¶
Package matchbuild builds matchers from the matcher specs of mock files: a list whose first item names the matcher and whose other items are its arguments, such as ["contains", "x"] or ["jsonpath", "$.a", ["gt", 1]]. Arguments are checked when a spec is built, so a mock file with a bad spec doesn't load, and no built-in matcher a spec builds fails on a request: only a custom one can, as its BuildFunc makes it.
A Registry builds specs: the built-in matchers, and the custom ones it was given with WithMatcher. Each server reads its mock files with a registry of its own, or one several servers share.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNameEmpty is the error of a custom matcher with no name. ErrNameEmpty = errors.New("matchbuild: matcher name is empty") // ErrNameTaken is the error of a custom matcher whose name a built-in matcher, or another custom one, has. ErrNameTaken = errors.New("matchbuild: matcher name is taken") )
Functions ¶
This section is empty.
Types ¶
type BuildFunc ¶
BuildFunc builds a custom matcher from the arguments of its spec, as a mock file gives them: strings, int64 and float64 numbers, bools, nil, lists ([]any) and objects (map[string]any). For ["body-too-long", 2048], args is [2048]. r is the registry building the spec: r.BuildMatcher builds an argument that is itself a matcher spec, custom names included.
An error rejects the spec, so a mock file that holds it doesn't load. The matcher may return errors from Match, but an error stops the search for a mock, so it should only report a failure of its own.
A file that args name opens from r.Path(name): a relative name is relative to the mock file. A matcher that names a file in its description names it absolute, as the description may load back from another directory, such as the admin API's; one that doesn't describe itself is described with args as given, which then open from there.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures a Registry.
func WithMatcher ¶
WithMatcher adds a custom matcher: specs that name it, such as [name, args...], are built with build. Names are read regardless of case, and can be neither a built-in matcher's name nor another custom one's.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"github.com/caffeine-projects/vlatte"
"github.com/caffeine-projects/vlatte/match"
"github.com/caffeine-projects/vlatte/match/matchbuild"
)
func main() {
// [longer-than, n] matches text longer than n bytes.
registry := matchbuild.MustNew(matchbuild.WithMatcher("longer-than",
func(_ *matchbuild.Registry, args []any) (vlatte.Matcher, error) {
if len(args) != 1 {
return nil, errors.New("takes a length")
}
n, ok := args[0].(int64)
if !ok {
return nil, errors.New("takes a length")
}
return match.Function(func(v any) (bool, error) {
s, isText := v.(string)
return isText && int64(len(s)) > n, nil
}), nil
}))
m, err := registry.BuildMatcher([]any{"longer-than", int64(3)})
if err != nil {
panic(err)
}
for _, v := range []string{"abc", "abcd"} {
ok, _ := m.Match(vlatte.NewMatcherArg(context.Background(), vlatte.NewExplanation(), v))
fmt.Println(v, ok)
}
}
Output: abc false abcd true
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry builds matcher specs: the built-in matchers, and the custom ones it was given. It can't change once built, so servers can share one.
func New ¶
New creates a Registry of the built-in matchers and the custom ones that options give. A name that is empty, taken by a built-in matcher or given twice fails with an error wrapping ErrNameEmpty or ErrNameTaken, one for each such name.
func (*Registry) BuildMatcher ¶
BuildMatcher builds the matcher a spec names: a matcher's name alone, such as "present", or a list such as ["contains", "x"]. The name is a built-in matcher's or one the registry was given.
func (*Registry) InDir ¶ added in v1.2.0
InDir returns a copy of r for the specs of a mock file in dir, whose Registry.Path opens relative names from dir. r is unchanged, so servers that share it are unaffected.