-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcolumn.go
163 lines (137 loc) · 4.24 KB
/
column.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
package apifaker
import (
"fmt"
. "github.com/Focinfi/gset"
"github.com/jinzhu/inflection"
"reflect"
"regexp"
"strings"
)
type JsonType string
const (
boolean JsonType = "boolean"
number JsonType = "number"
str JsonType = "string"
array JsonType = "array"
object JsonType = "object"
)
// Name returns JsonType string itself
func (j JsonType) Name() string {
return string(j)
}
// GoType returns the corresponding type in golang
func (j JsonType) GoType() string {
switch j {
case boolean:
return "bool"
case number:
return "float64"
case str:
return "string"
case array:
return "[]interface {}"
case object:
return "map[string]interface {}"
}
return "nil"
}
// jsonTypes contains a list a supportted json types
var jsonTypes = NewSetSimple(boolean, number, str, array, object)
type Column struct {
Name string `json:"name"`
Type string `json:"type"`
Unique bool `json:"unique"`
RegexpPattern string `json:"regexp_pattern"`
uniqueValues *SetThreadSafe
}
func (column *Column) getUniqueValues() *SetThreadSafe {
if column.uniqueValues == nil {
column.uniqueValues = NewSetThreadSafe()
}
return column.uniqueValues
}
// CheckType checks
// 1. Name and Type must be present
// 2. Type must in jsonTypes
// 3. RegexpPattern must valid
func (column Column) CheckMeta() error {
if column.Name == "" {
return ColumnsErrorf("colmun[content=%v] must has a name", column)
}
columnLogName := fmt.Sprintf("column[name=\"%s\"]", column.Name)
if column.Type == "" {
return ColumnsErrorf("%s must has a type", columnLogName)
}
if !jsonTypes.Has(JsonType(column.Type)) {
return ColumnsErrorf("%s use unsupportted type: %s, all supportted types: %v", columnLogName, column.Type, jsonTypes.ToSlice())
}
if column.RegexpPattern != "" {
if _, err := regexp.Compile(column.RegexpPattern); err != nil {
return ColumnsErrorf("%s has wrong regexp pattern format: %s, error: %v", columnLogName, column.RegexpPattern)
}
}
return nil
}
// CheckRelationships checks the if resource exists with the xxx_id
func (column *Column) CheckRelationships(seedVal interface{}, model *Model) error {
if !strings.HasSuffix(column.Name, "_id") {
return nil
}
columnLogName := fmt.Sprintf("column[name=\"%s\"]", column.Name)
resName := strings.TrimSuffix(column.Name, "_id")
resPluralName := inflection.Plural(resName)
router, ok := model.router.apiFaker.Routers[resPluralName]
if ok {
for _, li := range router.Model.ToLineItems() {
id, ok := li.Get("id")
if ok && id == seedVal {
return nil
}
}
}
return ColumnsErrorf("%s has no item[id=%v] of resource[resource_name=\"%s\"]", columnLogName, seedVal, resPluralName)
}
// CheckValue checks the value to insert database
// 1. type
// 2. regexp pattern matching
// 3. uniqueness if unique is true
func (column *Column) CheckValue(seedVal interface{}, model *Model) error {
columnLogName := fmt.Sprintf("column[name=\"%s\"]", column.Name)
goType := JsonType(column.Type).GoType()
jsonType := JsonType(column.Type).Name()
seedType := reflect.TypeOf(seedVal).String()
if seedType != goType {
return ColumnsErrorf("%s has wrong type, expect a %s, but use a %s", columnLogName, jsonType, seedType)
}
if column.RegexpPattern != "" && column.Type == str.Name() {
matched, err := regexp.Match(column.RegexpPattern, []byte(seedVal.(string)))
if err == nil && !matched {
return fmt.Errorf("%s mismatch regexp format, value: %v, format: %s", columnLogName, seedVal, column.RegexpPattern)
}
}
if !column.CheckUniquenessOf(seedVal) {
return ColumnsErrorf("%s item value %v already exists", columnLogName, seedVal)
}
return nil
}
// CheckUniquenessOf checks if the given value exists
func (column *Column) CheckUniquenessOf(value interface{}) bool {
if !column.Unique || column.Name == "id" {
return true
}
return !column.getUniqueValues().Has(T(value))
}
// AddValue add the give value into the Column's uniqueValues
func (column *Column) AddUniquenessOf(value interface{}) {
if !column.Unique {
return
}
column.getUniqueValues().Add(T(value))
}
// RemoveValue remove the given value from Column's uniqueValues
func (column *Column) RemoveUniquenessOf(value interface{}) {
if !column.Unique {
return
}
column.getUniqueValues().Remove(T(value))
}