gomatch: github.com/jfilipczyk/gomatch Index | Files

package gomatch

import "github.com/jfilipczyk/gomatch"

Package gomatch provides types for pattern based JSON matching.

It provides JSONMatcher type which performs deep comparison of two JSON strings. JSONMatcher may be created with a set of ValueMatcher implementations. A ValueMatcher is used to make comparison less strict than a regular value comparison.

Use NewDefaultJSONMatcher to create JSONMatcher with a chain of all available ValueMatcher implementations.

Basic usage:

actual := `
{
	"id": 351,
	"name": "John Smith",
	"address": {
		"city": "Boston"
	}
}
`
expected := `
{
	"id": "@number@",
	"name": "John Smith",
	"address": {
		"city": "@string@"
	}
}
`

m := gomatch.NewDefaultJSONMatcher()
ok, err := m.Match(expected, actual)
if ok {
	fmt.Printf("actual JSON matches expected JSON")
} else {
	fmt.Printf("actual JSON does not match expected JSON: %s", err.Error())
}

Use NewJSONMatcher to create JSONMatcher with a custom ValueMatcher implementation. Use ChainMatcher to chain multiple ValueMacher implementations.

m := gomatch.NewJSONMatcher(
	NewChainMatcher(
		[]ValueMatcher{
			NewStringMatcher("@string@"),
			NewNumberMatcher("@number@"),
		},
	)
);

Index

Package Files

array_matcher.go bool_matcher.go chain_matcher.go json_matcher.go number_matcher.go string_matcher.go uuid_matcher.go wildcard_matcher.go

type ArrayMatcher Uses

type ArrayMatcher struct {
    // contains filtered or unexported fields
}

An ArrayMatcher matches []interface{}.

func NewArrayMatcher Uses

func NewArrayMatcher(pattern string) *ArrayMatcher

NewArrayMatcher creates ArrayMatcher.

func (*ArrayMatcher) CanMatch Uses

func (m *ArrayMatcher) CanMatch(p interface{}) bool

CanMatch returns true if pattern p can be handled

func (*ArrayMatcher) Match Uses

func (m *ArrayMatcher) Match(p, v interface{}) (bool, error)

Match performs value matching against given pattern.

type BoolMatcher Uses

type BoolMatcher struct {
    // contains filtered or unexported fields
}

A BoolMatcher matches booleans.

func NewBoolMatcher Uses

func NewBoolMatcher(pattern string) *BoolMatcher

NewBoolMatcher creates BoolMatcher.

func (*BoolMatcher) CanMatch Uses

func (m *BoolMatcher) CanMatch(p interface{}) bool

CanMatch returns true if pattern p can be handled

func (*BoolMatcher) Match Uses

func (m *BoolMatcher) Match(p, v interface{}) (bool, error)

Match performs value matching against given pattern.

type ChainMatcher Uses

type ChainMatcher struct {
    // contains filtered or unexported fields
}

A ChainMatcher allows to chain multiple value matchers

func NewChainMatcher Uses

func NewChainMatcher(matchers []ValueMatcher) *ChainMatcher

NewChainMatcher creates ChainMatcher.

func (*ChainMatcher) CanMatch Uses

func (m *ChainMatcher) CanMatch(p interface{}) bool

CanMatch returns true if pattern p can be handled by any of internal matchers

func (*ChainMatcher) Match Uses

func (m *ChainMatcher) Match(p, v interface{}) (bool, error)

Match performs value matching against given pattern. It iterates through internal matchers and uses first which can handle given pattern.

type JSONMatcher Uses

type JSONMatcher struct {
    // contains filtered or unexported fields
}

A JSONMatcher provides Match method to match two JSONs with pattern matching support.

func NewDefaultJSONMatcher Uses

func NewDefaultJSONMatcher() *JSONMatcher

NewDefaultJSONMatcher creates JSONMatcher with default chain of value matchers. Default chain contains:

- StringMatcher handling "@string@" pattern

- NumberMatcher handling "@number@" pattern

- BoolMatcher handling "@bool@" pattern

- ArrayMatcher handling "@array@" pattern

- UUIDMatcher handling "@uuid@" pattern

- WildcardMatcher handling "@wildcard@" pattern

func NewJSONMatcher Uses

func NewJSONMatcher(matcher ValueMatcher) *JSONMatcher

NewJSONMatcher creates JSONMatcher with given value matcher.

func (*JSONMatcher) Match Uses

func (m *JSONMatcher) Match(expectedJSON, actualJSON string) (bool, error)

Match performs deep match of given JSON with an expected JSON pattern.

It traverses expected JSON pattern and checks if actual JSON has expected values. When traversing it checks if expected value is a pattern supported by internal ValueMatcher. In such case it uses the ValueMatcher to match actual value otherwise it compares expected value with actual value.

Expected JSON pattern example:

{
	"id": "@number@",
	"name": "John Smith",
	"address": {
		"city": "@string@"
	}
}

Matching actual JSON:

{
	"id": 351,
	"name": "John Smith",
	"address": {
		"city": "Boston"
	}
}

In above example we assume that ValueMatcher supports "@number@" and "@string@" patterns, otherwise matching will fail.

Besides value patterns JSONMatcher supports an "unbounded pattern" - "@...@". It can be used at the end of an array to allow any extra array elements:

[
	"John Smith",
	"Joe Doe",
	"@...@"
]

It can be used at the end of an object to allow any extra keys:

{
	"id": 351,
	"name": "John Smith",
	"@...@": ""
}

When matching fails then error message contains a path to invalid value.

type NumberMatcher Uses

type NumberMatcher struct {
    // contains filtered or unexported fields
}

A NumberMatcher matches float64. It expects float64 because json.Unmarshal uses float64 by default for numbers.

func NewNumberMatcher Uses

func NewNumberMatcher(pattern string) *NumberMatcher

NewNumberMatcher creates NumberMatcher.

func (*NumberMatcher) CanMatch Uses

func (m *NumberMatcher) CanMatch(p interface{}) bool

CanMatch returns true if pattern p can be handled

func (*NumberMatcher) Match Uses

func (m *NumberMatcher) Match(p, v interface{}) (bool, error)

Match performs value matching against given pattern.

type StringMatcher Uses

type StringMatcher struct {
    // contains filtered or unexported fields
}

A StringMatcher matches any string

func NewStringMatcher Uses

func NewStringMatcher(pattern string) *StringMatcher

NewStringMatcher creates StringMatcher.

func (*StringMatcher) CanMatch Uses

func (m *StringMatcher) CanMatch(p interface{}) bool

CanMatch returns true if pattern p can be handled.

func (*StringMatcher) Match Uses

func (m *StringMatcher) Match(p, v interface{}) (bool, error)

Match performs value matching against given pattern.

type UUIDMatcher Uses

type UUIDMatcher struct {
    // contains filtered or unexported fields
}

A UUIDMatcher matches booleans.

func NewUUIDMatcher Uses

func NewUUIDMatcher(pattern string) *UUIDMatcher

NewUUIDMatcher creates UUIDMatcher.

func (*UUIDMatcher) CanMatch Uses

func (m *UUIDMatcher) CanMatch(p interface{}) bool

CanMatch returns true if pattern p can be handled

func (*UUIDMatcher) Match Uses

func (m *UUIDMatcher) Match(p, v interface{}) (bool, error)

Match performs value matching against given pattern.

type ValueMatcher Uses

type ValueMatcher interface {
    // CanMatch returns true if given pattern can be handled by value matcher implementation.
    CanMatch(p interface{}) bool

    // Match performs the matching of given value v.
    // It also expects pattern p so implementation may handle multiple patterns or some DSL.
    Match(p, v interface{}) (bool, error)
}

A ValueMatcher interface should be implemented by any matcher used by JSONMatcher.

type WildcardMatcher Uses

type WildcardMatcher struct {
    // contains filtered or unexported fields
}

A WildcardMatcher matches any value.

func NewWildcardMatcher Uses

func NewWildcardMatcher(pattern string) *WildcardMatcher

NewWildcardMatcher creates WildcardMatcher.

func (*WildcardMatcher) CanMatch Uses

func (m *WildcardMatcher) CanMatch(p interface{}) bool

CanMatch returns true if pattern p can be handled

func (*WildcardMatcher) Match Uses

func (m *WildcardMatcher) Match(p, v interface{}) (bool, error)

Match return true for any value

Package gomatch imports 6 packages (graph). Updated 2019-01-28. Refresh now. Tools for package owners.