-
Notifications
You must be signed in to change notification settings - Fork 4
/
strategies.go
233 lines (195 loc) · 5.07 KB
/
strategies.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
package factory
import (
"fmt"
"reflect"
)
const (
invalidFieldNameErr = "invalid field name %s to define factory of %s"
invalidFieldValueTypeErr = "cannot use value (type %v) as type %v of field %s to define factory of %s"
undefinedTraitErr = "undefined trait name %s of type %s factory"
)
func newDefaultBlueprint(f *Factory) *blueprint {
return &blueprint{
factory: f,
filedValues: map[string]interface{}{},
}
}
func newDefaultBlueprintForCreate(f *Factory) *blueprint {
bp := newDefaultBlueprint(f)
bp.table = newTable(f)
return bp
}
func newDefaultBlueprintForDelete(f *Factory) *blueprint {
bp := newDefaultBlueprint(f)
bp.table = newTable(f)
return bp
}
type factoryOption func(*blueprint) error
// WithTraits defines which traits the new instance will use.
// It can take multiple traits. These traits will be executed one by one.
// So the later one may override the one before.
//
// For example:
//
// The trait "trait1" set Field1 as "value1", and at the same time, trait "trait2" set Field1 as "value2".
// The WithTraits("trait1", "trait2") will finally set Field1 as "value2".
func WithTraits(traits ...string) factoryOption {
return func(bp *blueprint) error {
for _, trait := range traits {
if _, ok := bp.factory.Traits[trait]; !ok {
return fmt.Errorf(undefinedTraitErr, trait, bp.factory.ModelType.Name())
}
bp.traits = append(bp.traits, trait)
}
return nil
}
}
// WithField sets the value of a specific field.
// This way has the highest priority to set the field value.
func WithField(name string, value interface{}) factoryOption {
return func(bp *blueprint) error {
modelTypeName := bp.factory.ModelType.Name()
if ok := fieldExists(bp.factory.ModelType, name); !ok {
return fmt.Errorf(invalidFieldNameErr, name, modelTypeName)
}
field, _ := structFieldByName(bp.factory.ModelType, name)
if valueType := reflect.TypeOf(value); valueType != field.Type {
return fmt.Errorf(invalidFieldValueTypeErr, valueType, field.Type, name, modelTypeName)
}
bp.filedValues[name] = value
return nil
}
}
// Build creates an instance from a factory
// but won't store it into database.
//
// model := &Model{}
//
// err := Build(FactoryModel,
// WithTrait("Chinese"),
// WithField("Name", "new name"),
// WithField("ID", 123),
// ).To(model)
//
func Build(f *Factory, opts ...factoryOption) to {
bp := newDefaultBlueprint(f)
for _, opt := range opts {
opt(bp)
}
return &buildTo{
blueprint: bp,
}
}
// BuildSlice creates a slice instance from a factory
// but won't store them into database.
//
// modelSlice := []*Model{}
//
// err := Build(FactoryModel,
// WithTrait("Chinese"),
// WithField("Name", "new name"),
// ).To(&modelSlice)
//
func BuildSlice(f *Factory, count int, opts ...factoryOption) to {
bp := newDefaultBlueprint(f)
for _, opt := range opts {
opt(bp)
}
return &buildSliceTo{
blueprint: bp,
count: count,
}
}
// Create creates an instance from a factory
// and stores it into database.
//
// model := &Model{}
//
// err := Create(FactoryModel,
// WithTrait("Chinese"),
// WithField("Name", "new name"),
// WithField("ID", 123),
// ).To(model)
//
func Create(f *Factory, opts ...factoryOption) to {
bp := newDefaultBlueprintForCreate(f)
for _, opt := range opts {
opt(bp)
}
return &createTo{
blueprint: bp,
dbConnection: getDB(),
}
}
// CreateSlice creates a slice of instance from a factory
// and stores them into database.
//
// modelSlice := []*Model{}
//
// err := CreateSlice(FactoryModel,
// WithTrait("Chinese"),
// WithField("Name", "new name"),
// ).To(&modelSlice)
//
func CreateSlice(f *Factory, count int, opts ...factoryOption) to {
bp := newDefaultBlueprintForCreate(f)
for _, opt := range opts {
opt(bp)
}
return &createSliceTo{
blueprint: bp,
count: count,
dbConnection: getDB(),
}
}
// Delete deletes an instance of a factory model from database.
// Example:
// err := Delete(FactoryModel, Model{})
//
func Delete(f *Factory, instance interface{}) error {
bp := newDefaultBlueprintForDelete(f)
return bp.delete(getDB(), instance)
}
// the following code are duplicated with "github.com/nauyey/factory/def"
// TODO: confirm if should handle panic
func fieldExists(typ reflect.Type, name string) bool {
fields := chainedFieldNameToFieldNames(name)
for i, field := range fields {
f, ok := typ.FieldByName(field)
if !ok {
return false
}
if i == len(fields)-1 {
break
}
// TODO: Optimize me for only type struct or *struct is valid
typ = f.Type
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
}
return true
}
// TODO: confirm if should handle panic
func structFieldByName(typ reflect.Type, name string) (*reflect.StructField, bool) {
var field *reflect.StructField
fieldNames := chainedFieldNameToFieldNames(name)
if len(fieldNames) == 0 {
return nil, false
}
for i, fieldName := range fieldNames {
f, ok := typ.FieldByName(fieldName)
if !ok {
return nil, false
}
field = &f
if i == len(fieldNames)-1 {
break
}
typ = f.Type
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
}
return field, true
}