compare

package module
v0.0.0-...-5859836 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2022 License: MIT Imports: 3 Imported by: 0

README

go-compare

Go Reference

Documentation

Overview

Example
type Item struct {
	Key   string
	Value string
}
type Object struct {
	ID    int
	Items []*Item
}

obj1 := Object{
	ID: 1,
	Items: []*Item{
		{Key: "item1", Value: "value1"},
		{Key: "item2", Value: "value2"},
	},
}
obj2 := Object{
	ID: 2,
	Items: []*Item{
		{Key: "item1", Value: "value1"},
		{Key: "item3", Value: "value3"},
	},
}
diff := compare.Diff(obj1, obj2, func(obj Object) compare.C {
	return compare.C{
		// compare `ID` by `==`.
		compare.Comparable("ID", obj.ID),
		// compare `Items` as slice.
		compare.Slice("Items", obj.Items, func(item *Item) compare.C {
			// define nested `compare.C` to compare each `Item`.
			return compare.C{
				compare.Comparable("Key", item.Key),
				compare.Comparable("Value", item.Value),
			}
		}),
	}
})

fmt.Println(diff)
Output:

ID: 1 != 2
Items[1].Key: "item2" != "item3"
Items[1].Value: "value2" != "value3"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Diff

func Diff[T any](v1, v2 T, f func(T) C) string

Types

type C

type C []Comparer

type Comparer

type Comparer interface {
	Value() any
	Diff(v1, v2 any) error
}

func Comparable

func Comparable[T comparable](label string, v T) Comparer
Example
diff := compare.Diff("A", "B", func(v string) compare.C {
	return compare.C{
		compare.Comparable("v", v),
	}
})

fmt.Println(diff)
Output:

v: "A" != "B"

func ComparablePointer

func ComparablePointer[T comparable](label string, v *T) Comparer
Example
type S struct {
	I int
}

diff := compare.Diff(&S{1}, &S{2}, func(v *S) compare.C {
	return compare.C{
		compare.ComparablePointer("v", v),
	}
})

fmt.Println(diff)
Output:

v: compare_test.S{I:1} != compare_test.S{I:2}

func ComparableSlice

func ComparableSlice[T comparable](label string, v []T) Comparer
Example
diff := compare.Diff([]int{1, 2}, []int{3, 4}, func(v []int) compare.C {
	return compare.C{
		compare.ComparableSlice("v", v),
	}
})

fmt.Println(diff)
Output:

v[0]: 1 != 3
v[1]: 2 != 4

func Func

func Func[T any](label string, v T, diffFunc func(v1, v2 T) error) Comparer
Example
t1 := time.Now()
t2 := t1.Add(100 * time.Millisecond)

diff := compare.Diff(t1, t2, func(t time.Time) compare.C {
	return compare.C{
		// Custom comparer to allow maximum 1s diff.
		compare.Func("t", t, func(t1, t2 time.Time) error {
			if t2.Sub(t1).Abs() > time.Second {
				return fmt.Errorf("t1 != t2")
			}
			return nil
		}),
	}
})

if diff == "" {
	fmt.Println("t1 approximately equals t2")
}
Output:

t1 approximately equals t2

func Nest

func Nest[T any](label string, v T, f func(T) C) Comparer
Example
type Child struct {
	F1 int
	F2 string
}
type Parent struct {
	Child Child
}

diff := compare.Diff(Parent{Child{1, "child1"}}, Parent{Child{2, "child2"}}, func(parent Parent) compare.C {
	return compare.C{
		compare.Nest("Child", parent.Child, func(child Child) compare.C {
			return compare.C{
				compare.Comparable("F1", child.F1),
				compare.Comparable("F2", child.F2),
			}
		}),
	}
})

fmt.Println(diff)
Output:

Child.F1: 1 != 2
Child.F2: "child1" != "child2"

func NewComparer

func NewComparer[T any](value T, diffFunc func(v1, v2 T) error) Comparer

func Slice

func Slice[T any](label string, v []T, f func(T) C) Comparer
Example
type S struct {
	F int
}

diff := compare.Diff([]S{{1}, {2}}, []S{{3}, {4}}, func(vs []S) compare.C {
	return compare.C{
		compare.Slice("vs", vs, func(v S) compare.C {
			return compare.C{
				compare.Comparable("F", v.F),
			}
		}),
	}
})

fmt.Println(diff)
Output:

vs[0].F: 1 != 3
vs[1].F: 2 != 4

Jump to

Keyboard shortcuts

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