-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_test.go
More file actions
112 lines (104 loc) · 1.98 KB
/
copy_test.go
File metadata and controls
112 lines (104 loc) · 1.98 KB
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
package copier_test
import (
"github.com/aacfactory/copier"
"testing"
"time"
)
type FooElem struct {
String string
Int int `copy:"int"`
}
type Foo struct {
String string
Bool bool
Int int
Float float64
Uint uint
Byte byte
Bytes []byte
Time time.Time
Slice []FooElem
Map map[string]FooElem
Entry Entry
}
type BarElem struct {
String string
Int64 int `copy:"int"`
}
type Bar struct {
String string
Bool bool
Int int
Float float64
Uint uint
Byte byte
Bytes []byte
Time time.Time
Slice []BarElem
Map map[int]BarElem
Entry EntryImpl
}
func TestCopy(t *testing.T) {
foo := Foo{}
bar := Bar{
String: "string",
Bool: true,
Int: 1,
Float: 2.2,
Uint: 3,
Byte: byte('B'),
Bytes: []byte("bytes"),
Time: time.Now(),
Slice: []BarElem{{String: "1", Int64: 1}, {String: "2", Int64: 2}},
Map: map[int]BarElem{1: {String: "1", Int64: 1}, 2: {String: "2", Int64: 2}},
Entry: EntryImpl{s: "entry"},
}
err := copier.Copy(&foo, bar)
if err != nil {
t.Error(err)
return
}
t.Logf("%+v", foo)
}
func TestValueOf(t *testing.T) {
bar := Bar{
String: "string",
Bool: true,
Int: 1,
Float: 2.2,
Uint: 3,
Byte: byte('B'),
Bytes: []byte("bytes"),
Time: time.Now(),
Slice: []BarElem{{String: "1", Int64: 1}, {String: "2", Int64: 2}},
Map: map[int]BarElem{1: {String: "1", Int64: 1}, 2: {String: "2", Int64: 2}},
}
foo, err := copier.ValueOf[Foo](bar)
if err != nil {
t.Error(err)
return
}
t.Logf("%+v", foo)
}
func BenchmarkCopy(b *testing.B) {
b.ReportAllocs()
bar := Bar{
String: "string",
Bool: true,
Int: 1,
Float: 2.2,
Uint: 3,
Bytes: []byte("bytes"),
Time: time.Now(),
Slice: []BarElem{{String: "1", Int64: 1}, {String: "2", Int64: 2}},
Map: map[int]BarElem{1: {String: "1", Int64: 1}, 2: {String: "2", Int64: 2}},
}
for i := 0; i < b.N; i++ {
foo := Foo{}
err := copier.Copy(&foo, bar)
if err != nil {
b.Error(err)
return
}
}
}