forked from ohler55/ojg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinfo.go
210 lines (190 loc) · 5.42 KB
/
finfo.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) 2021, Peter Ohler, All rights reserved.
package alt
import (
"reflect"
"unsafe"
)
const (
strMask = byte(0x01)
omitMask = byte(0x02)
embedMask = byte(0x04)
)
var nilValue reflect.Value
type valFunc func(fi *finfo, rv reflect.Value, addr uintptr) (v interface{}, fv reflect.Value, omit bool)
type finfo struct {
rt reflect.Type
key string
value valFunc
ivalue valFunc
index []int
offset uintptr
}
func valString(fi *finfo, rv reflect.Value, addr uintptr) (interface{}, reflect.Value, bool) {
return rv.FieldByIndex(fi.index).String(), nilValue, false
}
func valStringNotEmpty(fi *finfo, rv reflect.Value, addr uintptr) (interface{}, reflect.Value, bool) {
s := rv.FieldByIndex(fi.index).String()
if len(s) == 0 {
return s, nilValue, true
}
return s, nilValue, false
}
func valJustVal(fi *finfo, rv reflect.Value, addr uintptr) (interface{}, reflect.Value, bool) {
fv := rv.FieldByIndex(fi.index)
return fv.Interface(), fv, false
}
func valPtrNotEmpty(fi *finfo, rv reflect.Value, addr uintptr) (interface{}, reflect.Value, bool) {
fv := rv.FieldByIndex(fi.index)
v := fv.Interface()
return v, fv, (*[2]uintptr)(unsafe.Pointer(&v))[1] == 0
}
func valSliceNotEmpty(fi *finfo, rv reflect.Value, addr uintptr) (interface{}, reflect.Value, bool) {
fv := rv.FieldByIndex(fi.index)
if fv.Len() == 0 {
return nil, nilValue, true
}
return fv.Interface(), fv, false
}
func valSimplifier(fi *finfo, rv reflect.Value, addr uintptr) (interface{}, reflect.Value, bool) {
v := rv.FieldByIndex(fi.index).Interface()
if (*[2]uintptr)(unsafe.Pointer(&v))[1] == 0 {
return nil, nilValue, false
}
return v.(Simplifier).Simplify(), nilValue, false
}
func valSimplifierAddr(fi *finfo, rv reflect.Value, addr uintptr) (interface{}, reflect.Value, bool) {
v := rv.FieldByIndex(fi.index).Addr().Interface()
return v.(Simplifier).Simplify(), nilValue, false
}
func valGenericer(fi *finfo, rv reflect.Value, addr uintptr) (interface{}, reflect.Value, bool) {
v := rv.FieldByIndex(fi.index).Interface()
if (*[2]uintptr)(unsafe.Pointer(&v))[1] == 0 {
return nil, nilValue, false
}
if g, _ := v.(Genericer); g != nil {
if n := g.Generic(); n != nil {
return n.Simplify(), nilValue, false
}
}
return nil, nilValue, false
}
func valGenericerAddr(fi *finfo, rv reflect.Value, addr uintptr) (interface{}, reflect.Value, bool) {
v := rv.FieldByIndex(fi.index).Addr().Interface()
if g, _ := v.(Genericer); g != nil {
if n := g.Generic(); n != nil {
return n.Simplify(), nilValue, false
}
}
return nil, nilValue, false
}
func newFinfo(f *reflect.StructField, key string, fx byte) *finfo {
fi := finfo{
rt: f.Type,
key: key,
index: f.Index,
value: valJustVal, // replace as necessary later
ivalue: valJustVal, // replace as necessary later
offset: f.Offset,
}
// Check for interfaces first since almost any type can implement one of
// the supported interfaces.
vp := reflect.New(fi.rt).Interface()
v := reflect.New(fi.rt).Elem().Interface()
if _, ok := v.(Simplifier); ok {
fi.value = valSimplifier
fi.ivalue = valSimplifier
return &fi
}
if _, ok := vp.(Simplifier); ok {
fi.value = valSimplifierAddr
fi.ivalue = valSimplifierAddr
return &fi
}
if _, ok := v.(Genericer); ok {
fi.value = valGenericer
fi.ivalue = valGenericer
return &fi
}
if _, ok := vp.(Genericer); ok {
fi.value = valGenericerAddr
fi.ivalue = valGenericerAddr
return &fi
}
switch f.Type.Kind() {
case reflect.Bool:
fi.value = boolValFuncs[fx]
fi.ivalue = boolValFuncs[fx|embedMask]
case reflect.Int:
fi.value = intValFuncs[fx]
fi.ivalue = intValFuncs[fx|embedMask]
case reflect.Int8:
fi.value = int8ValFuncs[fx]
fi.ivalue = int8ValFuncs[fx|embedMask]
case reflect.Int16:
fi.value = int16ValFuncs[fx]
fi.ivalue = int16ValFuncs[fx|embedMask]
case reflect.Int32:
fi.value = int32ValFuncs[fx]
fi.ivalue = int32ValFuncs[fx|embedMask]
case reflect.Int64:
fi.value = int64ValFuncs[fx]
fi.ivalue = int64ValFuncs[fx|embedMask]
case reflect.Uint:
fi.value = uintValFuncs[fx]
fi.ivalue = uintValFuncs[fx|embedMask]
case reflect.Uint8:
fi.value = uint8ValFuncs[fx]
fi.ivalue = uint8ValFuncs[fx|embedMask]
case reflect.Uint16:
fi.value = uint16ValFuncs[fx]
fi.ivalue = uint16ValFuncs[fx|embedMask]
case reflect.Uint32:
fi.value = uint32ValFuncs[fx]
fi.ivalue = uint32ValFuncs[fx|embedMask]
case reflect.Uint64:
fi.value = uint64ValFuncs[fx]
fi.ivalue = uint64ValFuncs[fx|embedMask]
case reflect.Float32:
fi.value = float32ValFuncs[fx]
fi.ivalue = float32ValFuncs[fx|embedMask]
case reflect.Float64:
fi.value = float64ValFuncs[fx]
fi.ivalue = float64ValFuncs[fx|embedMask]
case reflect.String:
if (fx & omitMask) != 0 {
fi.value = valStringNotEmpty
fi.ivalue = valStringNotEmpty
} else {
fi.value = valString
fi.ivalue = valString
}
case reflect.Struct:
fi.value = valJustVal
fi.ivalue = valJustVal
case reflect.Ptr:
if (fx & omitMask) != 0 {
fi.value = valPtrNotEmpty
fi.ivalue = valPtrNotEmpty
} else {
fi.value = valJustVal
fi.ivalue = valJustVal
}
case reflect.Interface:
if (fx & omitMask) != 0 {
fi.value = valPtrNotEmpty
fi.ivalue = valPtrNotEmpty
} else {
fi.value = valJustVal
fi.ivalue = valJustVal
}
case reflect.Slice, reflect.Array, reflect.Map:
if (fx & omitMask) != 0 {
fi.value = valSliceNotEmpty
fi.ivalue = valSliceNotEmpty
} else {
fi.value = valJustVal
fi.ivalue = valJustVal
}
}
return &fi
}