-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.builder.go
442 lines (373 loc) · 11.2 KB
/
tree.builder.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package dstruct
import (
"fmt"
"reflect"
"sort"
"strings"
"unsafe"
"github.com/MartinSimango/dstruct/dreflect"
)
type treeBuilderImpl struct {
root *Node[StructField]
setValues bool
}
var _ Builder = &treeBuilderImpl{}
func createRoot() *Node[StructField] {
return &Node[StructField]{
data: &StructField{
name: "#root",
value: reflect.ValueOf(nil),
qualifiedName: "#root",
},
children: make(map[string]*Node[StructField]),
}
}
func NewBuilder() *treeBuilderImpl {
return newBuilderFromNode(createRoot(), false)
}
func canExtend(val any) bool {
if val == nil {
return false
}
ptrValue, _ := getPtrValue(reflect.ValueOf(val), 0)
if !ptrValue.IsValid() {
return false
}
return ptrValue.Type().Kind() == reflect.Struct
}
func ExtendStruct(val any) *treeBuilderImpl {
// TODO check if val is a struct
b := NewBuilder()
value := reflect.ValueOf(val)
if !canExtend(val) {
panic(fmt.Sprintf("Cannot extend struct value of type %s", value.Type()))
}
switch value.Kind() {
case reflect.Struct:
b.root.data.dstructType = b.addStructFields(value, b.root, 0)
case reflect.Ptr:
b.root.data.dstructType = b.addPtrField(value, b.root)
}
return b
}
func newBuilderFromNode(node *Node[StructField], resetFQN bool) *treeBuilderImpl {
if resetFQN {
resetNodeFieldsFQN(node)
}
return &treeBuilderImpl{
setValues: true,
root: node,
}
}
func resetNodeFieldsFQN(node *Node[StructField]) *Node[StructField] {
for _, v := range node.children {
v.data.qualifiedName = getQualifiedName(node.data.qualifiedName, v.data.name)
resetNodeFieldsFQN(v)
}
return node
}
func (dsb *treeBuilderImpl) AddField(name string, value interface{}, tag string) Builder {
if dsb.root.HasChild(name) {
panic(fmt.Sprintf("Field '%s' already exists", name))
}
dsb.root.data.dstructType = dsb.addFieldToTree(
name,
value,
"",
false,
reflect.StructTag(tag),
dsb.root,
)
return dsb
}
func (dsb *treeBuilderImpl) AddEmbeddedField(value interface{}, tag string) Builder {
ptrValue, _ := getPtrValue(reflect.ValueOf(value), 0)
name := reflect.TypeOf(ptrValue.Interface()).Name()
dsb.addFieldToTree(name, value, "", true, reflect.StructTag(tag), dsb.root)
return dsb
}
func (dsb *treeBuilderImpl) RemoveField(name string) Builder {
fields := strings.Split(name, ".")
node := dsb.root
for i := 0; i < len(fields)-1; i++ {
node = node.GetNode(fields[i])
}
if node.children[fields[len(fields)-1]] != nil {
node.DeleteNode(fields[len(fields)-1])
*node.data.numberOfSubFields--
}
return dsb
}
// GetField implements Builder.GetField
func (dsb *treeBuilderImpl) GetField(field string) Builder {
if node := dsb.getNode(field); node != nil {
if node.data.ptrDepth > 0 {
if node.data.ptrKind != reflect.Struct {
panic(
fmt.Sprintf(
"Cannot get field '%s' because it a points to a non-struct value.",
field,
),
)
}
} else if node.data.dstructType.Kind() != reflect.Struct {
panic(fmt.Sprintf("Cannot get field '%s' because it is not a struct or does not fully derefence to a struct value", field))
}
// if node.data.ptrKind != reflect.Struct {
// node. /
return newBuilderFromNode(node, false)
}
panic(fmt.Sprintf("Field '%s' does not exist", field))
}
func (dsb *treeBuilderImpl) NewBuilderFromField(field string) Builder {
copyNode := dsb.getNode(field).Copy()
copyNode.data.name = "#root"
copyNode.data.qualifiedName = "#root"
return newBuilderFromNode(copyNode, true)
}
func (dsb *treeBuilderImpl) GetFieldCopy(field string) Builder {
copyNode := dsb.getNode(field).Copy()
return newBuilderFromNode(copyNode, false)
}
func (dsb *treeBuilderImpl) getNode(field string) *Node[StructField] {
fields := strings.Split(field, ".")
node := dsb.root
for i := 0; i < len(fields); i++ {
if node = node.GetNode(fields[i]); node == nil {
return nil
}
}
return node
}
func (db *treeBuilderImpl) Build() DynamicStructModifier {
rootCopy := db.root.Copy()
// Ensure that the current node is treated as root when struct is built
if db.root.parent != nil {
rootCopy.data.name = "#root"
rootCopy.data.qualifiedName = "#root"
rootCopy = resetNodeFieldsFQN(rootCopy)
}
return newStruct(db.buildStruct(rootCopy), rootCopy)
}
func (db *treeBuilderImpl) buildStruct(tree *Node[StructField]) any {
structValue := reflect.ValueOf(dreflect.GetPointerOfValueType(treeToStruct(tree)))
tree.data.value = structValue
// set the value of the struct fields. Currently the tree structure contains the values of the fields
// so we need to copy the values to the struct fields
if db.setValues {
setValues(structValue.Elem(), tree)
}
return structValue.Interface()
}
func setValues(value reflect.Value, tree *Node[StructField]) {
if value.Kind() == reflect.Ptr {
setPointerFieldValue(value, tree)
} else {
setStructFieldValues(value, tree)
}
}
func (dsb *treeBuilderImpl) addFieldToTree(
name string,
typ interface{},
pkgPath string,
anonymous bool,
tag reflect.StructTag,
root *Node[StructField],
) reflect.Type {
value := reflect.ValueOf(typ)
if !value.IsValid() {
panic(fmt.Sprintf("Cannot determine type of field '%s'", name))
}
if root.data.numberOfSubFields == nil {
root.data.numberOfSubFields = new(int)
} else {
*root.data.numberOfSubFields++
}
var goType string
if goType = tag.Get("goType"); goType == "" {
goType = value.Type().Name()
}
field := &StructField{
name: name,
value: value, // this will initally be unaddressable until the struct is built
tag: tag,
dstructType: value.Type(),
goType: goType,
pkgPath: pkgPath,
anonymous: anonymous,
jsonName: strings.Split(tag.Get("json"), ",")[0],
}
field.structIndex = new(int)
*field.structIndex = *root.data.numberOfSubFields
field.qualifiedName = getQualifiedName(root.data.GetQualifiedName(), field.name)
root.AddNode(name, field)
// don't add struct fields if special kind
switch value.Kind() {
case reflect.Struct:
field.dstructType = dsb.addStructFields(value, root.children[name], 0)
case reflect.Ptr:
field.dstructType = dsb.addPtrField(value, root.children[name])
}
return field.dstructType
}
func sortKeys(root *Node[StructField]) (keys []string) {
for key := range root.children {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
return *root.children[keys[i]].data.structIndex < *root.children[keys[j]].data.structIndex
})
return
}
// this only allocates memory for the struct and its fields and does not set any values
// so the returned value will be an uninitialized struct
func treeToStruct(root *Node[StructField]) any {
var structFields []reflect.StructField
// sort the keys to ensure type of struct produced is always the same
keys := sortKeys(root)
for _, fieldName := range keys {
var typ reflect.Type
field := root.GetNode(fieldName)
g, _ := getPtrValue(field.data.value, 0)
if g.Kind() == reflect.Struct {
typ = reflect.TypeOf(treeToStruct(field))
} else {
typ = field.data.value.Type()
}
structFields = append(structFields, reflect.StructField{
Name: fieldName,
PkgPath: field.data.pkgPath,
Type: typ,
Tag: field.data.tag,
Anonymous: field.data.anonymous,
})
}
strct := reflect.New(reflect.StructOf(structFields)).Elem()
for i := 0; i < root.data.ptrDepth; i++ {
strct = reflect.New(reflect.TypeOf(strct.Interface()))
}
return strct.Interface()
}
func setStructFieldValues(strct reflect.Value, root *Node[StructField]) {
for i := 0; i < strct.NumField(); i++ {
field := strct.Field(i)
fieldName := strct.Type().Field(i).Name
currentNode := root.GetNode(fieldName)
switch field.Kind() {
case reflect.Struct:
setStructFieldValues(field, currentNode)
case reflect.Ptr:
setPointerFieldValue(field, currentNode)
default:
// field.Set(currentNode.data.value)
// Support for setting values from non exported fields
reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).
Elem().
Set(currentNode.data.value)
}
// make the tree node value point to the struct field - ensuring that when the node value changes the struct field value changes
// also update the node value to actually have a memory address
currentNode.data.value = field
// if currentNode.data.anonymous {
// db.addAnonymousSubfields(currentNode)
// }
}
}
func setPointerFieldValue(field reflect.Value, currentNode *Node[StructField]) {
if currentNode.data.value.IsNil() {
return
}
f := field
for i := 0; i < currentNode.data.ptrDepth; i++ {
// We don't use f.Set(reflect.New(f.Type().Elem())) because it panics when the field is unexported
// se we need to access the memory address of the field and set the value which bypasses the panic
reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).
Elem().
Set(reflect.New(f.Type().Elem()))
// f.Set(reflect.New(f.Type().Elem()))
f = f.Elem()
}
switch f.Kind() {
case reflect.Struct:
setStructFieldValues(f, currentNode)
default:
field.Set(currentNode.data.value)
}
currentNode.data.value = field
}
func (dsb *treeBuilderImpl) addStructFields(
strct reflect.Value,
root *Node[StructField],
ptrDepth int,
) reflect.Type {
var structFields []reflect.StructField
// Create pointer to struct to allow unexported field values to be read in order
// to obtain their types
pointerToStruct := reflect.New(reflect.TypeOf(strct.Interface()))
pointerToStruct.Elem().Set(strct)
for i := 0; i < strct.NumField(); i++ {
fieldName := strct.Type().Field(i).Name
fieldTag := strct.Type().Field(i).Tag
if fieldTag.Get("goType") == "" {
fieldTag = (reflect.StructTag)(strings.TrimSpace(fmt.Sprintf(
"%s goType:\"%s\"",
fieldTag,
strct.Field(i).Type(),
)))
}
var fieldValue any
if strct.Type().Field(i).IsExported() {
fieldValue = strct.Field(i).Interface()
} else {
f := pointerToStruct.Elem().Field(i)
fieldValue = reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Elem().Interface()
}
pkgPath := strct.Type().Field(i).PkgPath
anonymous := strct.Type().Field(i).Anonymous
fieldType := dsb.addFieldToTree(fieldName, fieldValue, pkgPath, anonymous, fieldTag, root)
if anonymous {
pkgPath = ""
}
structFields = append(structFields, reflect.StructField{
Name: fieldName,
PkgPath: pkgPath,
Type: fieldType,
Tag: fieldTag,
Anonymous: anonymous,
})
}
retStruct := reflect.New(reflect.StructOf(structFields)).Elem()
for i := 0; i < ptrDepth; i++ {
retStruct = reflect.New(retStruct.Type())
}
return reflect.TypeOf(retStruct.Interface())
}
func getPtrValue(value reflect.Value, ptrDepth int) (reflect.Value, int) {
switch value.Kind() {
case reflect.Ptr:
return getPtrValue(value.Elem(), ptrDepth+1)
}
return value, ptrDepth
}
func (dsb *treeBuilderImpl) addPtrField(
value reflect.Value,
node *Node[StructField],
) reflect.Type {
if value.IsNil() {
return reflect.TypeOf(value.Interface())
}
ptrValue, ptrDepth := getPtrValue(value, 0)
node.data.ptrDepth = ptrDepth
node.data.ptrKind = ptrValue.Kind()
switch ptrValue.Kind() {
case reflect.Struct:
return dsb.addStructFields(ptrValue, node, ptrDepth)
}
return reflect.TypeOf(value.Interface())
}
func getQualifiedName(root, name string) string {
if root != "#root" && root != "" {
return fmt.Sprintf("%s.%s", root, name)
}
return name
}