match

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any(paths ...string) *anyMatcher

Any matcher acts as a placeholder for any value

It replaces any targeted path with a placeholder string

Any("user.name")
// or with multiple paths
Any("user.name", "user.email")

func Custom

func Custom(path string, callback CustomCallback) *customMatcher

Custom matcher allows you to bring your own validation and placeholder value.

match.Custom("user.age", func(val any) (any, error) {
	age, ok := val.(float64)
	if !ok {
			return nil, fmt.Errorf("expected number but got %T", val)
	}

	return "some number", nil
})

The callback func value for JSON can be on of these types:
 bool // for JSON booleans
 float64 // for JSON numbers
 string // for JSON string literals
 nil // for JSON null
 map[string]any // for JSON objects
 []any // for JSON arrays

func Type added in v0.4.7

func Type[ExpectedType any](paths ...string) *typeMatcher[ExpectedType]

Type matcher evaluates types that are passed in a snapshot

It replaces any targeted path with placeholder in the form of `<Type:ExpectedType>`

match.Type[string]("user.info")
// or with multiple paths
match.Type[float64]("user.age", "data.items")

Types

type CustomCallback

type CustomCallback func(val any) (any, error)

type JSONMatcher

type JSONMatcher interface {
	JSON([]byte) ([]byte, []MatcherError)
}

type MatcherError

type MatcherError struct {
	Reason  error
	Matcher string
	Path    string
}

internal Error struct returned from Matchers

Jump to

Keyboard shortcuts

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