compare

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: MIT Imports: 7 Imported by: 8

README

Compare

Go compare library.

Go Reference

Features

  • Similar to reflect.DeepEqual()
  • Returns a detailed diff result
  • Supports custom comparison functions:
    • .Equal() / .Eq() / .Cmp() / ...
    • Add you own functions !

Usage

Documentation

Overview

Package compare provide comparison utilities.

Index

Examples

Constants

This section is empty.

Variables

View Source
var MaxSliceDifferences = 10

MaxSliceDifferences is the maximum number of differences for a slice. If the value is reached, the comparison is stopped for the current slice. It is also used for array. Set to 0 disables it.

Functions

func RegisterFunc

func RegisterFunc(f Func)

RegisterFunc registers a Func. It allows to handle manually the comparison for certain values.

func RegisterMethodEqual

func RegisterMethodEqual(name string)

RegisterMethodEqual registers an equal method. This methods must be callable as "v1.METHOD(v2) bool".

Types

type Difference

type Difference struct {
	Path    Path
	Message string
	V1, V2  any
}

Difference represents a difference between 2 values.

func (Difference) Format

func (d Difference) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

It only supports the 'v' verb. By default, it show the path and message. The '+' flag shows values V1 and V2.

type Func

type Func func(v1, v2 reflect.Value) (r Result, stop bool)

Func represents a comparison function. It is guaranteed that both values: are valid, of the same type, and can be converted to any. If the returned value "stop" is true, the comparison will stop.

type IndexedPathElem added in v1.2.0

type IndexedPathElem struct {
	Index int
}

IndexedPathElem is a PathElem for a slice/array index.

func (IndexedPathElem) String added in v1.2.0

func (e IndexedPathElem) String() string

String returns the string representation.

type MapPathElem added in v1.2.0

type MapPathElem struct {
	Key string
}

MapPathElem is a PathElem for a map key.

func (MapPathElem) String added in v1.2.0

func (e MapPathElem) String() string

String returns the string representation.

type Path

type Path []PathElem

Path represents a field path, which is a list of PathElem.

Elements are stored in reverse order, the first element is the deepest. It helps to prepend elements to the path efficiently.

func (Path) String added in v1.2.0

func (p Path) String() string

String returns the string value for a Path.

type PathElem added in v1.2.0

type PathElem interface {
	String() string
	// contains filtered or unexported methods
}

PathElem is a single element in a Path.

type Result

type Result []Difference

Result is a list of Difference.

func Compare

func Compare(v1, v2 any) Result

Compare compares 2 values.

Example
type T struct {
	String string
	Int    int
	Map    map[string]any
	Slice  []int
}
v1 := T{
	String: "aaa",
	Int:    1,
	Map: map[string]any{
		"a": "a",
		"b": "b",
		"c": "c",
	},
	Slice: []int{1, 2, 3},
}
v2 := T{
	String: "bbb",
	Int:    2,
	Map: map[string]any{
		"a": "z",
		"b": 5,
		"d": "c",
	},
	Slice: []int{1, 2, 4},
}
diff := Compare(v1, v2)
if len(diff) != 0 {
	fmt.Printf("%+v", diff)
}
Output:
.String: string not equal
	v1="aaa"
	v2="bbb"
.Int: int not equal
	v1=1
	v2=2
.Map[a]: string not equal
	v1="a"
	v2="z"
.Map[b]: type not equal
	v1=string
	v2=int
.Map[c]: map key not defined
	v1=true
	v2=false
.Map[d]: map key not defined
	v1=false
	v2=true
.Slice[2]: int not equal
	v1=3
	v2=4

func (Result) Format

func (r Result) Format(s fmt.State, verb rune)

Format implements fmt.Formatter.

See Difference.Format() for supported verb and flag.

func (Result) Merge

func (r Result) Merge(rm Result) Result

Merge merges 2 Result.

type StructPathElem added in v1.2.0

type StructPathElem struct {
	Field string
}

StructPathElem is a PathElem for a struct field.

func (StructPathElem) String added in v1.2.0

func (e StructPathElem) String() string

String returns the string representation.

Jump to

Keyboard shortcuts

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