-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
426 lines (360 loc) · 9.92 KB
/
model.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package apifaker
import (
"encoding/json"
"github.com/Focinfi/gset"
"github.com/Focinfi/gtester"
"github.com/gin-gonic/gin"
"github.com/jinzhu/inflection"
"io/ioutil"
"os"
"sort"
"sync"
)
type Model struct {
Name string `json:"resource_name"`
// Seeds acts as a snapshot of the whole database
Seeds []map[string]interface{} `json:"seeds"`
Columns []*Column `json:"columns"`
// relationships
HasMany []string `json:"has_many"`
HasOne []string `json:"has_one"`
// Set contains runtime data
Set *gset.SetThreadSafe `json:"-"`
// currentId records the max of id
currentId float64
// dataChanged signs if differs between Set and Seeds
dataChanged bool
sync.RWMutex
router *Router
}
//------Model CURD------//
// NewModel allocates and returns a new Model
func NewModel(router *Router) *Model {
return &Model{
Seeds: []map[string]interface{}{},
Columns: []*Column{},
Set: gset.NewSetThreadSafe(),
router: router,
}
}
// NewModelWithPath allocates and returns a new Model,
// using the given path as it's json file path
func NewModelWithPath(path string, router *Router) (*Model, error) {
// open file
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
model := NewModel(router)
bytes := []byte{}
err = gtester.NewInspector().
Check(func() error { bytes, err = ioutil.ReadAll(file); return err }).
Check(func() error { return json.Unmarshal(bytes, model) }).
Check(model.CheckRelationshipsMeta).
Check(model.CheckColumnsMeta).
Check(model.ValidateSeedsValue).
Then(func() {
model.initSet()
})
return model, err
}
// updateId updates currentId if the given id is bigger
func (model *Model) updateId(id float64) {
if id > model.currentId {
model.currentId = id
}
}
// nextId pluses 1 to Model.currentId and returns it
func (model *Model) nextId() float64 {
model.currentId++
return model.currentId
}
// Len returns the length of Model's Set
func (model *Model) Len() int {
return model.Set.Len()
}
// Has returns if Model has LineItem with the given id
func (model *Model) Has(id float64) bool {
return model.Set.Has(gset.T(id))
}
// Get gets and returns element with id param and the existence of it
func (model *Model) Get(id float64) (li LineItem, ok bool) {
var element interface{}
if element, ok = model.Set.Get(id); ok {
li, ok = element.(LineItem)
}
return
}
// Add add a LineItem to Model.Set
func (model *Model) Add(li LineItem) error {
model.Lock()
defer model.Unlock()
// set id if the given LineItem has no id
if _, ok := li.Get("id"); !ok {
li.Set("id", model.nextId())
}
if err := model.Validate(li.ToMap()); err != nil {
return err
} else {
model.Set.Add(li)
model.dataChanged = true
model.addUniqueValues(li)
}
return nil
}
// Update updates the LineItem with the given id by the given LineItem
func (model *Model) Update(id float64, li *LineItem) error {
oldLi, ok := model.Get(id)
if !ok {
return SeedsErrorf("model %s[id:%d] does not exsit", model.Name, id)
}
model.Lock()
defer model.Unlock()
// set id if the given LineItem has no id
if _, ok := li.Get("id"); !ok {
li.Set("id", id)
}
if err := model.Validate(li.dataMap); err != nil {
return err
} else {
model.Set.Add(*li)
model.dataChanged = true
model.removeUniqueValues(oldLi)
model.addUniqueValues(*li)
}
return nil
}
// Delete deletes the LineItem and its related data with the given id
func (model *Model) Delete(id float64) {
model.Lock()
defer model.Unlock()
li, ok := model.Get(id)
if !ok {
return
}
li.DeleteRelatedLis(id, model)
model.Set.Remove(gset.T(id))
model.dataChanged = true
model.removeUniqueValues(li)
}
// UpdateWithAttrsInGinContext finds a LineItem with id param,
// updates it with attrs from gin.Contex.PostForm(),
// returns the edited LineItem
func (model *Model) UpdateWithAttrs(id float64, ctx *gin.Context) (LineItem, error) {
// check if element does exsit
li, ok := model.Get(id)
if !ok {
return li, SeedsErrorf("model %s[id:%d] does not exsit", model.Name, id)
}
// update model
for _, column := range model.Columns {
value := ctx.PostForm(column.Name)
if value == "" || column.Name == "id" {
continue
}
formatVal, err := FormatValue(column.Type, value)
if err == nil {
err = column.CheckValue(formatVal, model)
}
if err != nil {
return li, err
} else {
oldValue, _ := li.Get(column.Name)
column.RemoveUniquenessOf(oldValue)
li.Set(column.Name, formatVal)
column.AddUniquenessOf(formatVal)
}
}
return li, nil
}
//------End Model CURD------//
//------Columns Uniqueness------//
// addUniqueValues adds values of the Lineitem into corresponding Column's uniqueValues
func (model *Model) addUniqueValues(lis ...LineItem) {
for _, li := range lis {
for _, column := range model.Columns {
if value, ok := li.Get(column.Name); ok {
column.AddUniquenessOf(value)
}
}
}
}
// removeUniqueValues reomves values of Lineitem into corresponding Column's uniqueValues
func (model *Model) removeUniqueValues(lis ...LineItem) {
for _, li := range lis {
for _, column := range model.Columns {
if value, ok := li.Get(column.Name); ok {
column.RemoveUniquenessOf(value)
}
}
}
}
//------End Columns Uniqueness------//
//------Check------//
// CheckRelationshipsMeta check the uniqueness of every element in HasOne and HasMany
func (model *Model) CheckRelationshipsMeta() error {
set := gset.NewSetSimple()
for _, resName := range model.HasMany {
if set.Has(resName) {
return HasManyErrorf("%s has been used", resName)
} else {
set.Add(resName)
}
}
for _, resName := range model.HasOne {
if set.Has(resName) {
return HasOneErrorf("%s has been used", resName)
} else {
set.Add(resName)
}
}
return nil
}
// checkColumnsMeta checks columns:
// 1. id must be the first column, its type must be number
// 2. CheckMeta
func (model *Model) CheckColumnsMeta() error {
if len(model.Columns) < 1 ||
model.Columns[0].Name != "id" ||
model.Columns[0].Type != number.Name() {
return ColumnsErrorf("The first colmun must be id with number type in file: %s", model.router.filePath)
}
for _, column := range model.Columns {
if err := column.CheckMeta(); err != nil {
return err
}
}
return nil
}
// CheckRelationship
// 1. checks if every resource in HasOne and HasMany exists
// 2. CheckRelationships
func (model *Model) CheckRelationship(seed map[string]interface{}) error {
for _, resoureName := range model.HasOne {
if _, ok := model.router.apiFaker.Routers[inflection.Plural(resoureName)]; !ok {
return HasOneErrorf("use unknown reource %s in file: %s", resoureName, model.router.filePath)
}
}
for _, resoureName := range model.HasMany {
if _, ok := model.router.apiFaker.Routers[inflection.Plural(resoureName)]; !ok {
return HasManyErrorf("use unknown reource \"%s\" in file: %s", resoureName, model.router.filePath)
}
}
for _, column := range model.Columns {
if err := column.CheckRelationships(seed[column.Name], model); err != nil {
return err
}
}
return nil
}
// CheckRelationships
func (model *Model) CheckRelationships() error {
for _, seed := range model.Seeds {
if err := model.CheckRelationship(seed); err != nil {
return err
}
}
return nil
}
// ValidateValue checks specific seed
func (model *Model) ValidateValue(seed map[string]interface{}) error {
columns := model.Columns
if len(seed) != len(columns) {
return SeedsErrorf("has wrong number of columns: %v", seed)
}
for _, column := range columns {
if seedVal, ok := seed[column.Name]; !ok {
return SeedsErrorf("has no column \"%s\" in seed: %v", column.Name, seed)
} else {
if err := column.CheckValue(seedVal, model); err != nil {
return err
}
}
}
return nil
}
// ValidateSeedsValue
func (model *Model) ValidateSeedsValue() error {
for _, seed := range model.Seeds {
if err := model.ValidateValue(seed); err != nil {
return err
}
}
return nil
}
// Validate ValidateValue and CheckRelationships
func (model *Model) Validate(seed map[string]interface{}) error {
return gtester.NewCheckQueue().Add(func() error {
return model.ValidateValue(seed)
}).Add(func() error {
return model.CheckRelationship(seed)
}).Run()
}
// CheckUniqueness check uniqueness for initialization for ApiFaker
func (model *Model) CheckUniqueness() error {
// check id
if !model.dataChanged && len(model.Seeds) != model.Len() {
return SeedsErrorf("model[name=\"%s\"] has same id", model.Name)
}
// check other unique columns
for _, column := range model.Columns {
if column.Unique && column.uniqueValues.Len() != model.Len() {
return SeedsErrorf("column[name=\"%s\"] in model[name=\"%s\"] has same values", column.Name, model.Name)
}
}
return nil
}
//------End Check------//
//------Seeds and Set------//
// initSet adds all LineItem into Set, addUniqueValues and updateId
func (model *Model) initSet() {
if model.Set == nil {
model.Set = gset.NewSetThreadSafe()
}
for _, seed := range model.Seeds {
li := NewLineItemWithMap(seed)
model.Set.Add(li)
model.addUniqueValues(li)
model.updateId(li.ID())
}
}
// backfillSeeds
func (model *Model) backfillSeeds() {
model.RLock()
defer model.RUnlock()
models := model.ToLineItems()
sort.Sort(models)
model.Seeds = models.ToSlice()
model.dataChanged = false
}
//------End Seeds and Set------//
// ToLineItems allocate a new LineItems filled with Model elements slice
func (model *Model) ToLineItems() LineItems {
lis := []LineItem{}
elements := model.Set.ToSlice()
for _, element := range elements {
if li, ok := element.(LineItem); ok {
newLi := li.InsertRelatedData(model)
lis = append(lis[:], newLi)
}
}
return LineItems(lis)
}
// SaveToFile save model to file with the given path
func (model *Model) SaveToFile(path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
if model.dataChanged {
model.backfillSeeds()
}
bytes, err := json.Marshal(model)
if err != nil {
return err
}
_, err = file.WriteString(string(bytes))
return err
}