forked from trapajim/testcraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomize.go
161 lines (143 loc) · 3.91 KB
/
randomize.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
package testcraft
import (
"errors"
"fmt"
"reflect"
)
func randomize[T any](f T, valuer Valuer) (any, error) {
t := reflect.TypeOf(f)
v := reflect.New(t)
fields := reflect.VisibleFields(t)
var errs []error
for _, field := range fields {
sField := v.Elem().FieldByName(field.Name)
if !sField.CanSet() {
continue
}
if field.Type.Kind() == reflect.Slice {
err := randomizeSlice(field, valuer, sField)
errs = appendError(errs, err)
continue
}
sField = createPointerField(sField)
val := getValueForField(valuer, field.Type.Kind(), field)
err := fieldAssign(sField, val)
errs = appendError(errs, err)
}
return v.Interface(), errors.Join(errs...)
}
func randomizeSlice(field reflect.StructField, valuer Valuer, sField reflect.Value) error {
slice, err := createSlice(field, valuer)
if err != nil {
return err
}
return fieldAssign(sField, slice)
}
func fieldAssign(sField reflect.Value, val reflect.Value) error {
if !val.IsValid() {
return fmt.Errorf("cannot set value to field %s", sField.Type().String())
}
vType := sField.Type()
tValue := val.Type()
if tValue.AssignableTo(vType) {
sField.Set(val)
} else {
sField.Set(val.Convert(vType))
}
return nil
}
func createPointerField(sField reflect.Value) reflect.Value {
if sField.Type().Kind() == reflect.Ptr {
if sField.IsNil() {
sField.Set(reflect.New(sField.Type().Elem()))
}
sField = sField.Elem()
}
return sField
}
func getValueForField(valuer Valuer, kind reflect.Kind, field reflect.StructField) reflect.Value {
if v, ok := valuer[kind.String()]; ok {
return v(field)
}
if v, ok := valuer[field.Type.String()]; ok {
return v(field)
}
switch kind {
case reflect.Struct:
return handleStruct(field, valuer)
case reflect.Ptr:
return getValueForField(valuer, field.Type.Elem().Kind(), field)
default:
return reflect.Value{}
}
}
func handleStruct(field reflect.StructField, valuer Valuer) reflect.Value {
value := reflect.Zero(field.Type)
ai := value.Interface()
isPtr := field.Type.Kind() == reflect.Ptr
if isPtr {
ai = reflect.Indirect(reflect.New(field.Type.Elem())).Interface()
}
val, _ := randomize(ai, valuer)
return reflect.ValueOf(val).Elem()
}
func appendError(errs []error, err error) []error {
if err != nil {
errs = append(errs, err)
}
return errs
}
func createSlice(field reflect.StructField, valuer Valuer) (reflect.Value, error) {
k := field.Type.Elem().Kind()
if k == reflect.Ptr {
k = field.Type.Elem().Elem().Kind()
}
if k == reflect.Struct {
return handleStructSlice(field, valuer)
}
return handlePrimitiveSlice(field, valuer)
}
func handleStructSlice(field reflect.StructField, valuer Valuer) (reflect.Value, error) {
value := reflect.Zero(field.Type.Elem())
ai := value.Interface()
isPtr := field.Type.Elem().Kind() == reflect.Ptr
if isPtr {
ai = reflect.Indirect(reflect.New(value.Type().Elem())).Interface()
}
slice := reflect.MakeSlice(reflect.SliceOf(field.Type.Elem()), 0, 3)
var errs []error
for i := 0; i < 3; i++ {
val, err := randomize(ai, valuer)
if err != nil {
errs = appendError(errs, err)
continue
}
if isPtr {
slice = reflect.Append(slice, reflect.ValueOf(val))
} else {
slice = reflect.Append(slice, reflect.Indirect(reflect.ValueOf(val)))
}
}
return slice, errors.Join(errs...)
}
func handlePrimitiveSlice(field reflect.StructField, valuer Valuer) (reflect.Value, error) {
k := field.Type.Elem().Kind()
orig := k
if k == reflect.Ptr {
k = field.Type.Elem().Elem().Kind()
}
slice := reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(reflect.New(field.Type.Elem()).Elem().Interface())), 0, 3)
for i := 0; i < 3; i++ {
val := getValueForField(valuer, k, field)
if !val.IsValid() {
return reflect.Value{}, fmt.Errorf("cannot set value to slice %s", field.Name)
}
if orig == reflect.Ptr {
ptr := reflect.New(reflect.TypeOf(val.Interface()))
ptr.Elem().Set(val)
val = ptr
}
slice = reflect.Append(slice, val)
}
return slice, nil
}