-
Notifications
You must be signed in to change notification settings - Fork 0
/
datacopy.go
65 lines (51 loc) · 1.22 KB
/
datacopy.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
/*
* Public Domain Software
*
* I (Matthias Ladkau) am the author of the source code in this file.
* I have placed the source code in this file in the public domain.
*
* For further information see: http://creativecommons.org/publicdomain/zero/1.0/
*/
/*
Package datautil contains general data handling objects and helper methods.
*/
package datautil
import (
"bytes"
"encoding/gob"
"github.com/krotik/common/pools"
)
/*
bufferPool holds buffers which are used to copy objects.
*/
var bufferPool = pools.NewByteBufferPool()
/*
CopyObject copies contents of a given object reference to another given object reference.
*/
func CopyObject(src interface{}, dest interface{}) error {
bb := bufferPool.Get().(*bytes.Buffer)
err := gob.NewEncoder(bb).Encode(src)
if err != nil {
return err
}
err = gob.NewDecoder(bb).Decode(dest)
if err != nil {
return err
}
bb.Reset()
bufferPool.Put(bb)
return nil
}
/*
MergeMaps merges all given maps into a new map. Contents are shallow copies
and conflicts are resolved as last-one-wins.
*/
func MergeMaps(maps ...map[string]interface{}) map[string]interface{} {
ret := make(map[string]interface{})
for _, m := range maps {
for k, v := range m {
ret[k] = v
}
}
return ret
}