forked from flyingmutant/rapid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.go
196 lines (175 loc) · 4.76 KB
/
make.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
// Copyright 2022 Gregory Petrosyan <gregory.petrosyan@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid
import (
"fmt"
"reflect"
)
// Make creates a generator of values of type V, using reflection to infer the required structure.
// Currently, Make may be unable to terminate generation of values of some recursive types, thus using
// Make with recursive types requires extra care.
func Make[V any]() *Generator[V] {
var zero V
gen := newMakeGen(reflect.TypeOf(zero))
return newGenerator[V](&makeGen[V]{
gen: gen,
})
}
type makeGen[V any] struct {
gen *Generator[any]
}
func (g *makeGen[V]) String() string {
var zero V
return fmt.Sprintf("Make[%T]()", zero)
}
func (g *makeGen[V]) value(t *T) V {
return g.gen.value(t).(V)
}
func newMakeGen(typ reflect.Type) *Generator[any] {
gen, mayNeedCast := newMakeKindGen(typ)
if !mayNeedCast || typ.String() == typ.Kind().String() {
return gen // fast path with less reflect
}
return newGenerator[any](&castGen{gen, typ})
}
type castGen struct {
gen *Generator[any]
typ reflect.Type
}
func (g *castGen) String() string {
return fmt.Sprintf("cast(%v, %v)", g.gen, g.typ.Name())
}
func (g *castGen) value(t *T) any {
v := g.gen.value(t)
return reflect.ValueOf(v).Convert(g.typ).Interface()
}
func newMakeKindGen(typ reflect.Type) (gen *Generator[any], mayNeedCast bool) {
switch typ.Kind() {
case reflect.Bool:
return Bool().AsAny(), true
case reflect.Int:
return Int().AsAny(), true
case reflect.Int8:
return Int8().AsAny(), true
case reflect.Int16:
return Int16().AsAny(), true
case reflect.Int32:
return Int32().AsAny(), true
case reflect.Int64:
return Int64().AsAny(), true
case reflect.Uint:
return Uint().AsAny(), true
case reflect.Uint8:
return Uint8().AsAny(), true
case reflect.Uint16:
return Uint16().AsAny(), true
case reflect.Uint32:
return Uint32().AsAny(), true
case reflect.Uint64:
return Uint64().AsAny(), true
case reflect.Uintptr:
return Uintptr().AsAny(), true
case reflect.Float32:
return Float32().AsAny(), true
case reflect.Float64:
return Float64().AsAny(), true
case reflect.Array:
return genAnyArray(typ), false
case reflect.Map:
return genAnyMap(typ), false
case reflect.Pointer:
return Deferred(func() *Generator[any] { return genAnyPointer(typ) }), false
case reflect.Slice:
return genAnySlice(typ), false
case reflect.String:
return String().AsAny(), true
case reflect.Struct:
return genAnyStruct(typ), false
default:
panic(fmt.Sprintf("unsupported type kind for Make: %v", typ.Kind()))
}
}
func genAnyPointer(typ reflect.Type) *Generator[any] {
elem := typ.Elem()
elemGen := newMakeGen(elem)
const pNonNil = 0.5
return Custom[any](func(t *T) any {
if flipBiasedCoin(t.s, pNonNil) {
val := elemGen.value(t)
ptr := reflect.New(elem)
ptr.Elem().Set(reflect.ValueOf(val))
return ptr.Interface()
} else {
return reflect.Zero(typ).Interface()
}
})
}
func genAnyArray(typ reflect.Type) *Generator[any] {
count := typ.Len()
elemGen := newMakeGen(typ.Elem())
return Custom[any](func(t *T) any {
a := reflect.Indirect(reflect.New(typ))
if count == 0 {
t.s.drawBits(0)
} else {
for i := 0; i < count; i++ {
e := reflect.ValueOf(elemGen.value(t))
a.Index(i).Set(e)
}
}
return a.Interface()
})
}
func genAnySlice(typ reflect.Type) *Generator[any] {
elemGen := newMakeGen(typ.Elem())
return Custom[any](func(t *T) any {
repeat := newRepeat(-1, -1, -1, elemGen.String())
sl := reflect.MakeSlice(typ, 0, repeat.avg())
for repeat.more(t.s) {
e := reflect.ValueOf(elemGen.value(t))
sl = reflect.Append(sl, e)
}
return sl.Interface()
})
}
func genAnyMap(typ reflect.Type) *Generator[any] {
keyGen := newMakeGen(typ.Key())
valGen := newMakeGen(typ.Elem())
return Custom[any](func(t *T) any {
label := keyGen.String() + "," + valGen.String()
repeat := newRepeat(-1, -1, -1, label)
m := reflect.MakeMapWithSize(typ, repeat.avg())
for repeat.more(t.s) {
k := reflect.ValueOf(keyGen.value(t))
v := reflect.ValueOf(valGen.value(t))
if m.MapIndex(k).IsValid() {
repeat.reject()
} else {
m.SetMapIndex(k, v)
}
}
return m.Interface()
})
}
func genAnyStruct(typ reflect.Type) *Generator[any] {
numFields := typ.NumField()
fieldGens := make([]*Generator[any], numFields)
for i := 0; i < numFields; i++ {
fieldGens[i] = newMakeGen(typ.Field(i).Type)
}
return Custom[any](func(t *T) any {
s := reflect.Indirect(reflect.New(typ))
if numFields == 0 {
t.s.drawBits(0)
} else {
for i := 0; i < numFields; i++ {
f := reflect.ValueOf(fieldGens[i].value(t))
s.Field(i).Set(f)
}
}
return s.Interface()
})
}