-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstruct_copier.go
More file actions
36 lines (31 loc) · 850 Bytes
/
struct_copier.go
File metadata and controls
36 lines (31 loc) · 850 Bytes
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
package deepcopy
import (
"reflect"
"unsafe"
"github.com/modern-go/reflect2"
)
type structCopier struct {
fieldTypes []reflect2.StructField
copiers []Copier
numField int
}
func (s *structCopier) Copy(src, ptr unsafe.Pointer) {
for i := 0; i < s.numField; i++ {
fieldPtr := s.fieldTypes[i].UnsafeGet(ptr)
s.copiers[i].Copy(s.fieldTypes[i].UnsafeGet(src), fieldPtr)
}
}
func createCopierOfStruct(typ reflect2.Type) Copier {
if typ.Kind() != reflect.Struct {
return nil
}
structType := typ.(*reflect2.UnsafeStructType)
numField := structType.NumField()
FieldTypes := make([]reflect2.StructField, numField)
copiers := make([]Copier, numField)
for i := 0; i < numField; i++ {
FieldTypes[i] = structType.Field(i)
copiers[i] = CopierOf(structType.Field(i).Type())
}
return &structCopier{FieldTypes, copiers, numField}
}