match

package
v0.5.8 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2024 License: MIT Imports: 7 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", "user.email")
// or for yaml
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 one 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

The callback func value for YAML can be one of these types:
 bool // for YAML booleans
 float64 // for YAML float numbers
 uint64 // for YAML integer numbers
 string // for YAML string literals
 nil // for YAML null
 map[string]any // for YAML objects
 []any // for YAML 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", "user.age")
// or for yaml
match.Type[string]("$.user.info", "$.user.age")

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

type YAMLMatcher added in v0.5.8

type YAMLMatcher interface {
	YAML([]byte) ([]byte, []MatcherError)
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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