-
Notifications
You must be signed in to change notification settings - Fork 1
/
field.go
132 lines (115 loc) · 3.06 KB
/
field.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
package structql
import (
"fmt"
"github.com/viant/xunsafe"
"reflect"
"unsafe"
)
type field struct {
mapKind mapKind
src *xunsafe.Field
dest *xunsafe.Field
aggregate bool
cp func(src, dest unsafe.Pointer)
}
func (f *field) configure() error {
if f.dest.Kind() == f.src.Kind() {
f.mapKind = mapKindDirect
switch f.dest.Kind() {
case reflect.String, reflect.Int, reflect.Int64, reflect.Float64, reflect.Float32, reflect.Bool:
f.mapKind = mapKindDirectPrimitive
}
return nil
}
return f.computeCastedCopy()
}
func (f *field) translateInt(src, dest unsafe.Pointer) {
*(*int)(dest) = *(*int)(src)
}
func (f *field) translateIntToIntPtr(src, dest unsafe.Pointer) {
srcValue := *(*int)(src)
*(**int)(dest) = &srcValue
}
func (f *field) translateIntPtrToInt(src unsafe.Pointer, dest unsafe.Pointer) {
srcValue := *(**int)(src)
if srcValue == nil {
return
}
*(*int)(dest) = *srcValue
}
func (f *field) translateIntPtrToIntPtr(src unsafe.Pointer, dest unsafe.Pointer) {
srcValue := *(**int)(src)
if srcValue == nil {
return
}
*(**int)(dest) = *(**int)(src)
}
func (f *field) translateIntToInts(src unsafe.Pointer, dest unsafe.Pointer) {
srcValue := *(*int)(src)
destSlice := (*[]int)(dest)
*destSlice = append(*destSlice, srcValue)
}
func (f *field) translateStringToStrings(src unsafe.Pointer, dest unsafe.Pointer) {
srcValue := *(*string)(src)
destSlice := (*[]string)(dest)
*destSlice = append(*destSlice, srcValue)
}
func (f *field) translateIntPtrToIntsPtr(src unsafe.Pointer, dest unsafe.Pointer) {
srcValue := (*int)(src)
if srcValue == nil {
return
}
destSlice := (*[]*int)(dest)
*destSlice = append(*destSlice, srcValue)
}
func (f *field) computeCastedCopy() error {
f.mapKind = mapKindTranslate
srcKind := f.src.Kind()
destKind := f.dest.Kind()
isSourcePtr := srcKind == reflect.Ptr
if isSourcePtr {
srcKind = f.src.Elem().Kind()
}
isDestPtr := destKind == reflect.Ptr
if isDestPtr {
destKind = f.dest.Elem().Kind()
}
switch destKind {
case reflect.Slice:
switch f.dest.Type.Elem().Kind() {
case reflect.Int, reflect.Uint, reflect.Int64, reflect.Uint64:
f.cp = f.translateIntToInts
case reflect.String:
f.cp = f.translateStringToStrings
case reflect.Ptr:
switch f.dest.Type.Elem().Elem().Kind() {
case reflect.Int, reflect.Uint, reflect.Int64, reflect.Uint64:
f.cp = f.translateIntPtrToIntsPtr
}
}
case reflect.Int, reflect.Uint, reflect.Int64, reflect.Uint64:
switch srcKind {
case reflect.Int, reflect.Uint, reflect.Int64, reflect.Uint64:
if !isSourcePtr && !isDestPtr {
f.cp = f.translateInt
} else if isDestPtr {
f.cp = f.translateIntToIntPtr
} else if isSourcePtr {
f.cp = f.translateIntPtrToInt
} else {
f.cp = f.translateIntPtrToIntPtr
}
case reflect.Float64:
case reflect.Float32:
case reflect.String:
}
case reflect.String:
case reflect.Bool:
case reflect.Float64:
case reflect.Float32:
}
if f.cp == nil {
return fmt.Errorf("unsupported structology field translation %s -> %s", f.src.Type.String(), f.dest.Type.String())
}
return nil
}