forked from cnf/structhash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structhash_examples_test.go
138 lines (130 loc) · 2.76 KB
/
structhash_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package structhash
import (
"crypto/md5"
"crypto/sha1"
"fmt"
)
func ExampleHash() {
type Person struct {
Name string
Age int
Emails []string
Extra map[string]string
Spouse *Person
}
bill := &Person{
Name: "Bill",
Age: 24,
Emails: []string{"[email protected]", "[email protected]"},
Extra: map[string]string{
"facebook": "Bob42",
},
}
bob := &Person{
Name: "Bob",
Age: 42,
Emails: []string{"[email protected]", "[email protected]"},
Extra: map[string]string{
"facebook": "Bob42",
},
Spouse: bill,
}
hash, err := Hash(bob, 1)
if err != nil {
panic(err)
}
fmt.Printf("%s", hash)
// Output:
// v1_6a50d73f3bd0b9ebd001a0b610f387f0
}
func ExampleHash_tags() {
type Person struct {
Ignored string `hash:"-"`
NewName string `hash:"name:OldName version:1"`
Age int `hash:"version:1"`
Emails []string `hash:"version:1"`
Extra map[string]string `hash:"version:1 lastversion:2"`
Spouse *Person `hash:"version:2"`
}
bill := &Person{
NewName: "Bill",
Age: 24,
Emails: []string{"[email protected]", "[email protected]"},
Extra: map[string]string{
"facebook": "Bob42",
},
}
bob := &Person{
NewName: "Bob",
Age: 42,
Emails: []string{"[email protected]", "[email protected]"},
Extra: map[string]string{
"facebook": "Bob42",
},
Spouse: bill,
}
hashV1, err := Hash(bob, 1)
if err != nil {
panic(err)
}
hashV2, err := Hash(bob, 2)
if err != nil {
panic(err)
}
hashV3, err := Hash(bob, 3)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", hashV1)
fmt.Printf("%s\n", hashV2)
fmt.Printf("%s\n", hashV3)
// Output:
// v1_45d8a54c5f5fd287f197b26d128882cd
// v2_babd7618f29036f5564816bee6c8a037
// v3_012b06239f942549772c9139d66c121e
}
func ExampleDump() {
type Person struct {
Name string
Age int
Emails []string
Extra map[string]string
Spouse *Person
}
bill := &Person{
Name: "Bill",
Age: 24,
Emails: []string{"[email protected]", "[email protected]"},
Extra: map[string]string{
"facebook": "Bob42",
},
}
bob := &Person{
Name: "Bob",
Age: 42,
Emails: []string{"[email protected]", "[email protected]"},
Extra: map[string]string{
"facebook": "Bob42",
},
Spouse: bill,
}
fmt.Printf("md5: %x\n", md5.Sum(Dump(bob, 1)))
fmt.Printf("sha1: %x\n", sha1.Sum(Dump(bob, 1)))
// Output:
// md5: 6a50d73f3bd0b9ebd001a0b610f387f0
// sha1: c45f097a37366eaaf6ffbc7357c2272cd8fb64f6
}
func ExampleVersion() {
// A hash string gotten from Hash(). Returns the version as an int.
i := Version("v1_55743877f3ffd5fc834e97bc43a6e7bd")
fmt.Printf("%d", i)
// Output:
// 1
}
func ExampleVersion_errors() {
// A hash string gotten from Hash(). Returns -1 on error.
i := Version("va_55743877f3ffd5fc834e97bc43a6e7bd")
fmt.Printf("%d", i)
// Output:
// -1
}