Documentation
¶
Index ¶
- Constants
- Variables
- func AreType(a, b reflect.Value, types ...reflect.Type) bool
- func Changed(a, b interface{}) bool
- func CustomValueDiffers(vd ...ValueDiffer) func(d *Differ) error
- func DisableStructValues() func(d *Differ) error
- func SliceOrdering(enabled bool) func(d *Differ) error
- type Change
- type Changelog
- type Comparative
- type ComparativeList
- type Differ
- type ValueDiffer
Examples ¶
Constants ¶
View Source
const ( // CREATE represents when an element has been added CREATE = "create" // UPDATE represents when an element has been updated UPDATE = "update" // DELETE represents when an element has been removed DELETE = "delete" )
Variables ¶
View Source
var ( // ErrTypeMismatch Compared types do not match ErrTypeMismatch = errors.New("types do not match") // ErrInvalidChangeType The specified change values are not unsupported ErrInvalidChangeType = errors.New("change type must be one of 'create' or 'delete'") )
Functions ¶
func CustomValueDiffers ¶ added in v1.1.0
func CustomValueDiffers(vd ...ValueDiffer) func(d *Differ) error
CustomValueDiffers allows you to register custom differs for specific types
func DisableStructValues ¶
DisableStructValues disables populating a separate change for each item in a struct, where the struct is being compared to a nil value
func SliceOrdering ¶
SliceOrdering determines whether the ordering of items in a slice results in a change
Types ¶
type Change ¶
type Change struct {
Type string `json:"type"`
Path []string `json:"path"`
From interface{} `json:"from"`
To interface{} `json:"to"`
}
Change stores information about a changed item
type Changelog ¶
type Changelog []Change
Changelog stores a list of changed items
func Diff ¶
Diff returns a changelog of all mutated values from both
Example ¶
type Tag struct {
Name string `diff:"name,identifier"`
Value string `diff:"value"`
}
type Fruit struct {
ID int `diff:"id"`
Name string `diff:"name"`
Healthy bool `diff:"healthy"`
Nutrients []string `diff:"nutrients"`
Tags []Tag `diff:"tags"`
}
a := Fruit{
ID: 1,
Name: "Green Apple",
Healthy: true,
Nutrients: []string{
"vitamin c",
"vitamin d",
},
Tags: []Tag{
{
Name: "kind",
Value: "fruit",
},
},
}
b := Fruit{
ID: 2,
Name: "Red Apple",
Healthy: true,
Nutrients: []string{
"vitamin c",
"vitamin d",
"vitamin e",
},
Tags: []Tag{
{
Name: "popularity",
Value: "high",
},
{
Name: "kind",
Value: "fruit",
},
},
}
changelog, err := Diff(a, b)
if err != nil {
panic(err)
}
fmt.Printf("%#v", changelog)
// Produces: diff.Changelog{diff.Change{Type:"update", Path:[]string{"id"}, From:1, To:2}, diff.Change{Type:"update", Path:[]string{"name"}, From:"Green Apple", To:"Red Apple"}, diff.Change{Type:"create", Path:[]string{"nutrients", "2"}, From:interface {}(nil), To:"vitamin e"}, diff.Change{Type:"create", Path:[]string{"tags", "popularity"}, From:interface {}(nil), To:main.Tag{Name:"popularity", Value:"high"}}}
func StructValues ¶
StructValues gets all values from a struct values are stored as "created" or "deleted" entries in the changelog, depending on the change type specified
type ComparativeList ¶
type ComparativeList struct {
// contains filtered or unexported fields
}
ComparativeList : stores indexed comparative
func NewComparativeList ¶
func NewComparativeList() *ComparativeList
NewComparativeList : returns a new comparative list
Click to show internal directories.
Click to hide internal directories.