-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcopiers.go
174 lines (139 loc) · 3.8 KB
/
copiers.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Package copy provides a copy library to make copying of structs to/from others structs a bit easier.
package copy
import (
"fmt"
"reflect"
"sync"
"unsafe"
"github.com/gotidy/copy/internal/cache"
)
const defaultTagName = "copy"
type copierKey struct {
Src reflect.Type
Dest reflect.Type
}
type indirectCopierKey struct {
Src Type
Dest Type
}
// Options is Copiers parameters.
type Options struct {
Tag string
Skip bool
}
// Option changes default Copiers parameters.
type Option func(c *Options)
// Tag set tag name.
func Tag(tag string) Option {
return func(o *Options) {
o.Tag = tag
}
}
// Skip nonassignable types else cause panic.
func Skip() Option {
return func(o *Options) {
o.Skip = true
}
}
// StructCopier fills a destination from source.
type Copier interface {
Copy(dst interface{}, src interface{})
}
type internalCopier interface {
Copier
copy(dst, src unsafe.Pointer)
init(dst, src reflect.Type)
}
// Copiers is a structs copier.
type Copiers struct {
cache *cache.Cache
options Options
mu sync.RWMutex
copiers map[copierKey]internalCopier
indirectCopiers map[indirectCopierKey]Copier
}
// New create new internalCopier.
func New(options ...Option) *Copiers {
var opts Options
for _, option := range options {
option(&opts)
}
return &Copiers{
cache: cache.New(opts.Tag),
options: opts,
copiers: make(map[copierKey]internalCopier),
indirectCopiers: make(map[indirectCopierKey]Copier),
}
}
// Prepare caches structures of src and dst. Dst and src each must be a pointer to struct.
// contents is not copied. It can be used for checking ability of copying.
//
// c := copy.New()
// c.Prepare(&dst, &src)
func (c *Copiers) Prepare(dst, src interface{}) {
_ = c.Get(dst, src)
}
// Copy copies the contents of src into dst. Dst and src each must be a pointer to struct.
func (c *Copiers) Copy(dst, src interface{}) {
c.Get(dst, src).Copy(dst, src)
}
func checkGet(copier internalCopier, err error) internalCopier {
if err != nil {
panic(err)
}
return copier
}
func (c *Copiers) get(dst, src reflect.Type) (internalCopier, error) {
copier, ok := c.copiers[copierKey{Src: src, Dest: dst}]
if ok {
return copier, nil
}
copier = getCopier(c, dst, src)
if copier == nil {
return nil, fmt.Errorf("the combination of destination(%s) and source(%s) types is not supported", dst, src)
}
c.copiers[copierKey{Src: src, Dest: dst}] = copier
copier.init(dst, src)
return copier, nil
}
// Get Copier for a specific destination and source.
func (c *Copiers) Get(dst, src interface{}) Copier {
c.mu.RLock()
copier, ok := c.indirectCopiers[indirectCopierKey{Dest: TypeOf(dst), Src: TypeOf(src)}]
c.mu.RUnlock()
if ok {
return copier
}
c.mu.Lock()
defer c.mu.Unlock()
srcType := reflect.TypeOf(src)
if srcType.Kind() != reflect.Ptr {
panic("source must be pointer")
}
srcType = srcType.Elem()
dstType := reflect.TypeOf(dst)
if dstType.Kind() != reflect.Ptr {
panic("destination must be pointer")
}
dstType = dstType.Elem()
copier = checkGet(c.get(dstType, srcType))
c.indirectCopiers[indirectCopierKey{Dest: TypeOf(dst), Src: TypeOf(src)}] = copier
return copier
}
// defaultCopier uses Copier with a "copy" tag.
var defaultCopier = New(Tag(defaultTagName))
// Prepare caches structures of src and dst. Dst and src each must be a pointer to struct.
// contents is not copied. It can be used for checking ability of copying.
//
// copy.Prepare(&dst, &src)
func Prepare(dst, src interface{}) {
defaultCopier.Prepare(dst, src)
}
// Copy copies the contents of src into dst. Dst and src each must be a pointer to a struct.
func Copy(dst, src interface{}) {
defaultCopier.Copy(dst, src)
}
// Get Copier for a specific destination and source.
func Get(dst, src interface{}) Copier {
return defaultCopier.Get(dst, src)
}