forked from trapajim/testcraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaluer.go
50 lines (42 loc) · 1.4 KB
/
valuer.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
package testcraft
import (
"github.com/trapajim/testcraft/datagen"
"reflect"
)
type Valuer map[string]ValuerFunc
type ValuerFunc func(reflect.StructField) reflect.Value
func valueString(field reflect.StructField) reflect.Value {
return reflect.ValueOf(datagen.Words(2))
}
func valueInt(field reflect.StructField) reflect.Value {
return reflect.ValueOf(datagen.Rand().Int(100)).Convert(field.Type)
}
func valueFloat(field reflect.StructField) reflect.Value {
return reflect.ValueOf(datagen.Rand().Float64(100)).Convert(field.Type)
}
func valueBool(field reflect.StructField) reflect.Value {
return reflect.ValueOf(datagen.Bool())
}
func valueTime(field reflect.StructField) reflect.Value {
return reflect.ValueOf(datagen.Time())
}
func defaultValuer() Valuer {
return map[string]ValuerFunc{
reflect.String.String(): valueString,
reflect.Int.String(): valueInt,
reflect.Int8.String(): valueInt,
reflect.Int16.String(): valueInt,
reflect.Int32.String(): valueInt,
reflect.Int64.String(): valueInt,
reflect.Uint.String(): valueInt,
reflect.Uint8.String(): valueInt,
reflect.Uint16.String(): valueInt,
reflect.Uint32.String(): valueInt,
reflect.Uint64.String(): valueInt,
reflect.Float32.String(): valueFloat,
reflect.Float64.String(): valueFloat,
reflect.Bool.String(): valueBool,
"time.Time": valueTime,
"*time.Time": valueTime,
}
}