matchbuild

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 21 Imported by: 0

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

View Source
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

type BuildFunc func(r *Registry, args []any) (vlatte.Matcher, error)

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

func WithMatcher(name string, build BuildFunc) Option

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 MustNew

func MustNew(opts ...Option) *Registry

MustNew is like New, but panics on an error.

func New

func New(opts ...Option) (*Registry, error)

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

func (r *Registry) BuildMatcher(spec any) (vlatte.Matcher, error)

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

func (r *Registry) InDir(dir string) *Registry

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.

func (*Registry) Path added in v1.2.0

func (r *Registry) Path(name string) string

Path returns where name, a file the arguments of a spec name, opens from: a relative name joined to the directory of the mock file, or as it is from the working directory, for a mock built in Go or sent to the admin API. An absolute name stays as it is.

func (*Registry) TryBuildMatcher

func (r *Registry) TryBuildMatcher(spec any) (vlatte.Matcher, error)

TryBuildMatcher builds the matcher of a value of a mock file: a string matches text equal to it, and a list is a matcher spec.

Jump to

Keyboard shortcuts

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