cmp

package
Version: v0.0.0-...-66a0726 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(l, r interface{}) (bool, error)

Equal will perform a comparison between two objects and indicate whether there are any differences between the two. It's like Compare(), but faster since it will return on the first difference found.

func Traverse

func Traverse(i interface{}, path []string) (interface{}, error)

Traverse take in an arbitrary object, and returns whatever object resides at the path given by the path variable. If the given path doesn't exist or isn't viable, an error is returned. The path may specific specific fields or map keys to use. The assumption is that any map keys must be strings.

func Walk

func Walk(i interface{}, f WalkFunc) error

If an error is returned from the given function, all walking is stopped and the error is returned.

func WalkAll

func WalkAll(i interface{}, f WalkFunc) []error

WalkAll is similar to Walk, but will not immediately stop after receiving an error. Rather, it will append it to an error array and return the combined result.

func WalkAllPath

func WalkAllPath(i interface{}, f WalkPathFunc) []error

WalkAll is similar to Walk, but will not immediately stop after receiving an error. Rather, it will append it to an error array and return the combined result.

Types

type Comparable

type Comparable interface {
	Compare(other interface{}) (*Comparison, error)
}

Comparable allows nodes to insert their own comparison logic. If this is implemented comparisons at the node's subtree will use this function, and appended with the comparison further up the tree

type Comparison

type Comparison struct {
	Add    []ComparisonField `json:"add,omitempty"`
	Remove []ComparisonField `json:"remove,omitempty"`
	Update []ComparisonField `json:"update,omitempty"`
	// contains filtered or unexported fields
}

Comparison is the result of a comparison between two values. It contains the full list of everything added, removed, or updated between the two values.

func Compare

func Compare(l, r interface{}) (*Comparison, error)

Compare will perform a deep comparison of two arbitrary values.

func CompareSlices

func CompareSlices(l, r interface{}, isOrdered bool) (*Comparison, error)

CompareSlices allows for the specific use case of comparing slices in either an ordered or unordered fashion. If the slices are compared in an ordered fashion, the index of items will be considered in the equality condition. If an item is contained within both arrays but is at a different index, it will be considered to have moved.

Note, the algorithm is currently naive in that it will simply compare indexes rather than using a more sophisticated diff approach (e.g. Myers Diff).As such, it's likely to be too verbose.

func CompareWithLimit

func CompareWithLimit(l, r interface{}, limit int) (*Comparison, error)

CompareWithLimit performs a recursive comparison of two arbitrary structs. However, once the number of differences equals the number set in 'limit', the recursive search will stop and the Comparison object will be returned. This is useful for cases like the Equal method, where all we care about is if there are any differences at all.

If limit is set to 0, there is no limit on the number of diffs returned.

func (*Comparison) IsEmpty

func (c *Comparison) IsEmpty() bool

IsEmpty reports if there are any differences found

func (*Comparison) Len

func (c *Comparison) Len() int

Len returns the total number of differences found

type ComparisonField

type ComparisonField struct {
	// New value contains the new value of item item. This will only be present
	// if the node is being updated or added
	NewValue interface{} `json:"new_value,omitempty"`

	// OldValue contains the old value of the item. This will only be present if
	// the node is updated or removed.
	OldValue interface{} `json:"old_value,omitempty"`

	// Path contains the full path to the node within the tree. Each string in
	// the path indicates one of:
	// 1. A struct field name
	// 2. A map key
	Path []string `json:"path,omitempty"`

	// NewIndex indicates the new index of an ordered node. This field will only
	// be non-nil if the node is ordered and is either being moved or inserted.
	NewIndex *uint `json:"new_index,omitempty"`

	// OldIndex indicates the old index of an ordered node. This field will only
	// be non-nil if the node is ordered and is either being moved or removed.
	OldIndex *uint `json:"old_index,omitempty"`
}

ComparisonField represents the change that occurs at a specific level of the compared values.

type PathError

type PathError struct {
	// Path lists the path at which the error occurred.
	Path []string

	// Err contains the inner error
	Err error
}

A PathError is a type of error used to indicate an error occurring while applying a function recursively through a value.

func WalkAllPathError

func WalkAllPathError(i interface{}, f WalkFunc) []PathError

WalkAllPathError walks through a given object, and will apply function f recursively through the given struct. If f returns an error, that error will be returned as well as the struct path at which that error occurred.

func (*PathError) Error

func (p *PathError) Error() string

Error simply returns whatever string is contained by the inner error

type WalkFunc

type WalkFunc func(interface{}) error

type WalkPathFunc

type WalkPathFunc func(i interface{}, path []string) error

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL