xdeep

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2019 License: MIT Imports: 5 Imported by: 0

README

go-xdeep

Example

Basic type
 err := xdeep.Equal("a","a")
 // err == nil
 
 err := xdeep.Equal(1,"1")
 // err != nil
map type
m1 := map[string]interface{}{
    "int": 1,
    "string": "1",
    "intarray": []int{1, 2, 3},
}

m2 := map[string]interface{}{
    "string": "1",
    "int": 1,
    "intarray": []int{1, 2, 3},
}

err := xdeep.Equal(m1, m2)
// err == nil

m3 := map[string]interface{}{
    "string": "2",
    "int": 1,
    "intarray": []int{1, 2, 3},
}

err := xdeep.Equal(m1, m3)
// err != nil
slice type
arr1 := []int{1, 2}
arr2 := []int{1, 2}

err := xdeep.Equal(arr1, arr2)
// err == nil

arr2 := []int{2, 1}
err := xdeep.Equal(arr1, arr2)
// err != nil

opt := Option{
    IgnoreArrayOrder: map[string]bool{
        "": true,
    }
}

// ignore array order
arr2 := []int{2, 1}
err := xdeep.Equal(arr1, arr2, &opt)
// err == nil
struct type
type Foo struct {
	Name         string
	Arr          []int
}

f1 := Foo{"name", []int{1,2}}
f2 := Foo{"name", []int{1,2}}
err := xdeep.Equal(f1, f2)
// err == nil

f3 := Foo{"bar", []int{1,2}}
err := xdeep.Equal(f1, f3)
// err != nil

// ignore field

opt := Option{
    IgnoreFields: []string{"Name"},
}
err := xdeep.Equal(f1, f3, &opt)
// err == nil

IEqual interface
type EqualBar struct {
	Name string
}

func (e *EqualBar) Equal(b interface{}) bool {
	if b, ok := b.(*EqualBar); ok {
		if strings.Contains(e.Name, b.Name) || strings.Contains(b.Name, e.Name) {
			return true
		}
	}
	return false
}
bar1 := EqualBar{ "bar1" }
bar2 := EqualBar{ "bar1 copy" }
err := xdeep.Equal(bar1, bar2, &opt)
// err == nil
Combine
type Foo struct {
	Name         string
	Arr          []int
	M            map[string]interface{}
	InterfaceArr []interface{}
	T            time.Time
}
t1 := time.Now()
t2 := t1
f1 := Foo{
	Name: "foo",
	Arr: []int{1, 3, 9},
	M: map[string]interface{}{
		"int":1,
		"string":"2",
		"foo": EqualBar{
			Name: "bar 1",
		},
	},
	T: t1,
}

f2 := Foo{
	Name: "foo",
	Arr: []int{9,3,1},
	M: map[string]interface{}{
		"int":1,
		"string":"!2",
		"foo": EqualBar{
			Name: "bar 1 copy",
		},
	},
	T: t2,
}

opt := Option{
	IgnoreArrayOrder: map[string]bool{
		"Arr": true,
	},
	IgnoreFields: []string{"M.string"},
}

err := xdeep.Equal(f1, f2, &opt)
// err == nil

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(expect interface{}, actual interface{}, opts ...*Option) error

Equal Compare `expect` with `actual`. they are equal when: 1. Both types are same. 2. Length of map's key must be equal, each pair of (key,value) must be `Equal` to the other, except the key is in the IgnoreFields. 3. Length of array or slice must be `Equal`. Without IgnoreArrayOrder setting, each item in the array must be `Equal` one by one. 4. Each exportable fields in struct must be `Equal`. 5. Implemented IEqual method returns true. 6. Otherwise, including `Time` types, the `reflect.DeepEqual(expect, actual)` return `true`. If return error is not nil, it contains information which field and why they are not equal.

Types

type IEqual

type IEqual interface {
	Equal(b interface{}) bool
}

If item implements IEqual interface, Equal will compare items with item.Equal.

type Option

type Option struct {
	// Fields than in IgnoreFields will skip compare with each other.
	// This is useful when you compare with object which come from create api results, which auto create id, create_at, updated_at.
	IgnoreFields []string
	// ignore fields with tag, eg. `json`
	IgnoreByTagName string
	// If set true, compare ignore the items order, which means `[1, 2]` equals `[2, 1]`.
	// If compared items is top level, just set empty key to true.
	IgnoreArrayOrder map[string]bool
	// deep(default), or unixNano
	TimeEqual string
	// contains filtered or unexported fields
}

Equal Option

Jump to

Keyboard shortcuts

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