compare

package module
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: MIT Imports: 6 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 PathString

func PathString(p Path) string

PathString returns the string value for a Path.

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 IndexedPath

type IndexedPath struct {
	Index int
	Next  Path
}

IndexedPath is a Path for a slice/array index.

func (IndexedPath) PathNext

func (p IndexedPath) PathNext() Path

PathNext implements Path.

func (IndexedPath) PathString

func (p IndexedPath) PathString() string

PathString implements Path.

func (IndexedPath) String

func (p IndexedPath) String() string

type MapPath

type MapPath struct {
	Key  string
	Next Path
}

MapPath is a Path for a map key.

func (MapPath) PathNext

func (p MapPath) PathNext() Path

PathNext implements Path.

func (MapPath) PathString

func (p MapPath) PathString() string

PathString implements Path.

func (MapPath) String

func (p MapPath) String() string

type Path

type Path interface {
	fmt.Stringer
	PathString() string
	PathNext() Path
}

Path represents a field 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 StructPath

type StructPath struct {
	Field string
	Next  Path
}

StructPath is a Path for a struct field.

func (StructPath) PathNext

func (p StructPath) PathNext() Path

PathNext implements Path.

func (StructPath) PathString

func (p StructPath) PathString() string

PathString implements Path.

func (StructPath) String

func (p StructPath) String() string

Jump to

Keyboard shortcuts

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