-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcloneHelpers.go
50 lines (43 loc) · 1.06 KB
/
cloneHelpers.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
package refmt
import (
"github.com/polydawn/refmt/obj"
"github.com/polydawn/refmt/obj/atlas"
"github.com/polydawn/refmt/shared"
)
func Clone(src, dst interface{}) error {
return CloneAtlased(src, dst, atlas.MustBuild())
}
func MustClone(src, dst interface{}) {
if err := Clone(src, dst); err != nil {
panic(err)
}
}
func CloneAtlased(src, dst interface{}, atl atlas.Atlas) error {
return NewCloner(atl).Clone(src, dst)
}
func MustCloneAtlased(src, dst interface{}, atl atlas.Atlas) {
if err := CloneAtlased(src, dst, atl); err != nil {
panic(err)
}
}
type Cloner interface {
Clone(src, dst interface{}) error
}
func NewCloner(atl atlas.Atlas) Cloner {
x := &cloner{
marshaller: obj.NewMarshaller(atl),
unmarshaller: obj.NewUnmarshaller(atl),
}
x.pump = shared.TokenPump{x.marshaller, x.unmarshaller}
return x
}
type cloner struct {
marshaller *obj.Marshaller
unmarshaller *obj.Unmarshaller
pump shared.TokenPump
}
func (c cloner) Clone(src, dst interface{}) error {
c.marshaller.Bind(src)
c.unmarshaller.Bind(dst)
return c.pump.Run()
}