/
datafiller.go
194 lines (169 loc) · 4.72 KB
/
datafiller.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
// Package datafiller implements function for randomly filling passed
// datastructures by mock sample data.
package datafiller
import (
"math/rand"
"reflect"
"strings"
"time"
"github.com/Pallinder/go-randomdata"
)
func init() {
packagesInit()
}
const (
taggedStructKey = "datafiller"
)
// Function Fill takes a pointer to variable of any type and fills the variable
// by with sample data. It panics if the passed value is not a pointer.
func Fill(i interface{}) {
f := NewFiller()
f.Seed = time.Now().Unix() + rand.Int63n(100)
f.Fill(i)
}
type Filler struct {
Seed int64
randSeed *rand.Rand
ArrayMin int
ArrayMax int
}
type FillerArg interface {
AddArg(*Filler)
}
type Seed int64
func (sed Seed) AddArg(f *Filler) {
f.Seed = int64(sed)
}
type ArrayMin int
func (amn ArrayMin) AddArg(f *Filler) {
f.ArrayMin = int(amn)
}
type ArrayMax int
func (amx ArrayMax) AddArg(f *Filler) {
f.ArrayMax = int(amx)
}
func NewFiller(args ...FillerArg) *Filler {
f := &Filler{
Seed: -1,
ArrayMin: 1,
ArrayMax: 3,
}
for _, a := range args {
a.AddArg(f)
}
return f
}
func (f *Filler) Fill(i interface{}) {
valPtr := reflect.ValueOf(i)
if valPtr.Kind() != reflect.Ptr && valPtr.Kind() != reflect.UnsafePointer {
panic("Passed argument is not a pointer.")
}
val := reflect.Indirect(valPtr)
f.randSeed = rand.New(rand.NewSource(f.Seed))
f.recursiveSet(val)
}
func taggedFieldSet(val reflect.Value, structTag string) {
stringDataMap := make(map[string]func() string)
stringDataMap["address"] = randomdata.Address
stringDataMap["city"] = randomdata.City
stringDataMap["email"] = randomdata.Email
stringDataMap["lastname"] = randomdata.LastName
stringDataMap["paragraph"] = randomdata.Paragraph
stringDataMap["street"] = randomdata.Street
stringDataMap["firstname"] = func() string { return randomdata.FirstName(randomdata.RandomGender) }
stringDataMap["name"] = func() string { return randomdata.FullName(randomdata.RandomGender) }
stringDataMap["country"] = func() string { return randomdata.Country(0) }
stringDataMap["postalcode"] = func() string { return randomdata.PostalCode("US") }
stringDataMap["state"] = func() string { return randomdata.State(1) }
tags := strings.Split(structTag, ",")
// TODO(tvi): Design struct tags ordering.
if val.Kind() == reflect.String {
gen, ok := stringDataMap[tags[0]]
if ok {
val.SetString(gen())
}
}
}
func (self *Filler) recursiveSet(val reflect.Value) {
if val.CanSet() {
var fullPath string
fullPath = val.Type().PkgPath() + "." + val.Type().Name()
pkgVal, ok := packages[fullPath]
if ok {
val.Set(pkgVal)
return
}
if val.Kind() == reflect.Int ||
val.Kind() == reflect.Int8 ||
val.Kind() == reflect.Int16 ||
val.Kind() == reflect.Int32 ||
val.Kind() == reflect.Int64 {
val.SetInt(self.randSeed.Int63n(100))
return
} else if val.Kind() == reflect.Uint ||
val.Kind() == reflect.Uint8 ||
val.Kind() == reflect.Uint16 ||
val.Kind() == reflect.Uint32 ||
val.Kind() == reflect.Uint64 {
val.SetUint(uint64(self.randSeed.Int63n(100)))
return
} else if val.Kind() == reflect.Float32 ||
val.Kind() == reflect.Float64 {
val.SetFloat(float64(self.randSeed.Float32()))
return
} else if val.Kind() == reflect.Complex64 {
cpx := complex128(complex(self.randSeed.Float32(), self.randSeed.Float32()))
val.SetComplex(cpx)
return
} else if val.Kind() == reflect.Complex128 {
cpx := complex128(complex(self.randSeed.Float64(), self.randSeed.Float64()))
val.SetComplex(cpx)
return
} else if val.Kind() == reflect.Bool {
if self.randSeed.Int63n(2) == 0 {
val.SetBool(false)
} else {
val.SetBool(true)
}
return
} else if val.Kind() == reflect.String {
val.SetString("test")
return
} else if val.Kind() == reflect.Struct {
lngth := val.NumField()
strType := val.Type()
for i := 0; i < lngth; i++ {
if strType.Field(i).Tag.Get(taggedStructKey) == "" {
self.recursiveSet(val.Field(i))
} else if strType.Field(i).Tag.Get(taggedStructKey) == "-" {
} else {
advStrTag := strType.Field(i).Tag.Get(taggedStructKey)
taggedFieldSet(val.Field(i), advStrTag)
}
}
return
} else if val.Kind() == reflect.Ptr {
tp := val.Type().Elem()
nw := reflect.New(tp)
val.Set(nw)
self.recursiveSet(reflect.Indirect(val))
return
} else if val.Kind() == reflect.Map {
// nm := reflect.NewMap
// TODO(tvi): Finish.
return
} else if val.Kind() == reflect.Slice {
// TODO(tvi): Fix slice length.
typ := val.Type()
elem := typ.Elem()
nw := reflect.Zero(elem)
m := reflect.MakeSlice(typ, 0, 1)
m = reflect.Append(m, nw)
m = reflect.Append(m, nw)
val.Set(m)
self.recursiveSet(val.Index(0))
self.recursiveSet(val.Index(1))
return
}
}
}