/
copy.go
47 lines (44 loc) 路 1.14 KB
/
copy.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
package collections
import "github.com/1pkg/gopium/gopium"
// CopyField defines helper that
// deep copies provided field
func CopyField(f gopium.Field) gopium.Field {
nf := f
// check that field doc exists
if f.Doc != nil {
nf.Doc = make([]string, len(f.Doc), cap(f.Doc))
copy(nf.Doc, f.Doc)
}
// check that field comment exists
if f.Comment != nil {
nf.Comment = make([]string, len(f.Comment), cap(f.Comment))
copy(nf.Comment, f.Comment)
}
return nf
}
// CopyStruct defines helper that
// deep copies provided struct
func CopyStruct(s gopium.Struct) gopium.Struct {
ns := s
// check that struct doc exists
if s.Doc != nil {
ns.Doc = make([]string, len(s.Doc), cap(s.Doc))
copy(ns.Doc, s.Doc)
}
// check that struct comment exists
if s.Comment != nil {
ns.Comment = make([]string, len(s.Comment), cap(s.Comment))
copy(ns.Comment, s.Comment)
}
// check that struct fields exists
if s.Fields != nil {
ns.Fields = make([]gopium.Field, len(s.Fields), cap(s.Fields))
copy(ns.Fields, s.Fields)
}
// go through struct fields and
// deep copy them one by one
for i, f := range ns.Fields {
ns.Fields[i] = CopyField(f)
}
return ns
}