forked from r3labs/diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_examples_test.go
65 lines (58 loc) · 1.31 KB
/
diff_examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package diff
import (
"fmt"
)
func ExampleDiff() {
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"}}}
}