Documentation ¶
Index ¶
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 AddCustomCmpType ¶
AddCustomCmpType for custom comparison
func AddInterfaceCmpType ¶
AddInterfaceCmpType if new type
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"}}}
Output:
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.