forked from qor/admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.go
321 lines (283 loc) · 8.65 KB
/
meta.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
package admin
import (
"database/sql"
"reflect"
"regexp"
"strconv"
"time"
"github.com/jinzhu/gorm"
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/qor/utils"
"github.com/qor/roles"
)
// MetaConfigInterface meta config interface
type MetaConfigInterface interface {
resource.MetaConfigInterface
}
// Meta meta struct definition
type Meta struct {
*resource.Meta
Name string
FieldName string
Label string
Type string
Setter func(record interface{}, metaValue *resource.MetaValue, context *qor.Context)
Valuer func(record interface{}, context *qor.Context) (result interface{})
FormattedValuer func(record interface{}, context *qor.Context) (result interface{})
Permission *roles.Permission
Config MetaConfigInterface
Collection interface{}
Resource *Resource
metas []resource.Metaor
baseResource *Resource
processors []*MetaProcessor
}
// SetPermission set meta's permission
func (meta *Meta) SetPermission(permission *roles.Permission) {
meta.Permission = permission
meta.Meta.Permission = permission
if meta.Resource != nil {
meta.Resource.Permission = permission
}
}
// HasPermission check has permission or not
func (meta Meta) HasPermission(mode roles.PermissionMode, context *qor.Context) bool {
var roles = []interface{}{}
for _, role := range context.Roles {
roles = append(roles, role)
}
if meta.Permission != nil {
return meta.Permission.HasPermission(mode, roles...)
}
if meta.Resource != nil {
return meta.Resource.HasPermission(mode, context)
}
if meta.baseResource != nil {
return meta.baseResource.HasPermission(mode, context)
}
return true
}
// GetResource get resource from meta
func (meta *Meta) GetResource() resource.Resourcer {
if meta.Resource == nil {
return nil
}
return meta.Resource
}
// GetMetas get sub metas
func (meta *Meta) GetMetas() []resource.Metaor {
if len(meta.metas) > 0 {
return meta.metas
} else if meta.Resource == nil {
return []resource.Metaor{}
} else {
return meta.Resource.GetMetas([]string{})
}
}
// MetaProcessor meta processor which will be run each time update Meta
type MetaProcessor struct {
Name string
Handler func(*Meta)
}
// AddProcessor add meta processors, it will be run when add them and each time update Meta
func (meta *Meta) AddProcessor(processor *MetaProcessor) {
if processor != nil && processor.Handler != nil {
processor.Handler(meta)
for idx, p := range meta.processors {
if p.Name == processor.Name {
meta.processors[idx] = processor
return
}
}
meta.processors = append(meta.processors, processor)
}
}
func (meta *Meta) configure() {
if meta.Meta == nil {
meta.Meta = &resource.Meta{
Name: meta.Name,
FieldName: meta.FieldName,
Setter: meta.Setter,
Valuer: meta.Valuer,
FormattedValuer: meta.FormattedValuer,
BaseResource: meta.baseResource,
Resource: meta.Resource,
Permission: meta.Permission,
Config: meta.Config,
}
} else {
meta.Meta.Name = meta.Name
meta.Meta.FieldName = meta.FieldName
meta.Meta.Setter = meta.Setter
meta.Meta.Valuer = meta.Valuer
meta.Meta.FormattedValuer = meta.FormattedValuer
meta.Meta.BaseResource = meta.baseResource
meta.Meta.Resource = meta.Resource
meta.Meta.Permission = meta.Permission
meta.Meta.Config = meta.Config
}
meta.PreInitialize()
if meta.FieldStruct != nil {
if injector, ok := reflect.New(meta.FieldStruct.Struct.Type).Interface().(resource.ConfigureMetaBeforeInitializeInterface); ok {
injector.ConfigureQorMetaBeforeInitialize(meta)
}
}
meta.Initialize()
if meta.Label == "" {
meta.Label = utils.HumanizeString(meta.Name)
}
var fieldType reflect.Type
var hasColumn = meta.FieldStruct != nil
if hasColumn {
fieldType = meta.FieldStruct.Struct.Type
for fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
}
}
// Set Meta Type
if hasColumn {
if meta.Type == "" {
if _, ok := reflect.New(fieldType).Interface().(sql.Scanner); ok {
if fieldType.Kind() == reflect.Struct {
fieldType = reflect.Indirect(reflect.New(fieldType)).Field(0).Type()
}
}
if relationship := meta.FieldStruct.Relationship; relationship != nil {
if relationship.Kind == "has_one" {
meta.Type = "single_edit"
} else if relationship.Kind == "has_many" {
meta.Type = "collection_edit"
} else if relationship.Kind == "belongs_to" {
meta.Type = "select_one"
} else if relationship.Kind == "many_to_many" {
meta.Type = "select_many"
}
} else {
switch fieldType.Kind() {
case reflect.String:
var tags = meta.FieldStruct.TagSettings
if size, ok := tags["SIZE"]; ok {
if i, _ := strconv.Atoi(size); i > 255 {
meta.Type = "text"
} else {
meta.Type = "string"
}
} else if text, ok := tags["TYPE"]; ok && text == "text" {
meta.Type = "text"
} else {
meta.Type = "string"
}
case reflect.Bool:
meta.Type = "checkbox"
default:
if regexp.MustCompile(`^(.*)?(u)?(int)(\d+)?`).MatchString(fieldType.Kind().String()) {
meta.Type = "number"
} else if regexp.MustCompile(`^(.*)?(float)(\d+)?`).MatchString(fieldType.Kind().String()) {
meta.Type = "float"
} else if _, ok := reflect.New(fieldType).Interface().(*time.Time); ok {
meta.Type = "datetime"
} else {
if fieldType.Kind() == reflect.Struct {
meta.Type = "single_edit"
} else if fieldType.Kind() == reflect.Slice {
refelectType := fieldType.Elem()
for refelectType.Kind() == reflect.Ptr {
refelectType = refelectType.Elem()
}
if refelectType.Kind() == reflect.Struct {
meta.Type = "collection_edit"
}
}
}
}
}
} else {
if relationship := meta.FieldStruct.Relationship; relationship != nil {
if (relationship.Kind == "has_one" || relationship.Kind == "has_many") && meta.Meta.Setter == nil && (meta.Type == "select_one" || meta.Type == "select_many") {
meta.SetSetter(func(resource interface{}, metaValue *resource.MetaValue, context *qor.Context) {
scope := &gorm.Scope{Value: resource}
reflectValue := reflect.Indirect(reflect.ValueOf(resource))
field := reflectValue.FieldByName(meta.FieldName)
if field.Kind() == reflect.Ptr {
if field.IsNil() {
field.Set(utils.NewValue(field.Type()).Elem())
}
for field.Kind() == reflect.Ptr {
field = field.Elem()
}
}
primaryKeys := utils.ToArray(metaValue.Value)
if len(primaryKeys) > 0 {
// set current field value to blank and replace it with new value
field.Set(reflect.Zero(field.Type()))
context.GetDB().Where(primaryKeys).Find(field.Addr().Interface())
}
if !scope.PrimaryKeyZero() {
context.GetDB().Model(resource).Association(meta.FieldName).Replace(field.Interface())
field.Set(reflect.Zero(field.Type()))
}
})
}
}
}
}
{ // Set Meta Resource
if hasColumn {
if meta.Resource == nil {
var result interface{}
if fieldType.Kind() == reflect.Struct {
result = reflect.New(fieldType).Interface()
} else if fieldType.Kind() == reflect.Slice {
refelectType := fieldType.Elem()
for refelectType.Kind() == reflect.Ptr {
refelectType = refelectType.Elem()
}
if refelectType.Kind() == reflect.Struct {
result = reflect.New(refelectType).Interface()
}
}
if result != nil {
res := meta.baseResource.NewResource(result)
meta.Resource = res
meta.Meta.Permission = meta.Meta.Permission.Concat(res.Config.Permission)
}
}
if meta.Resource != nil {
permission := meta.Resource.Permission.Concat(meta.Meta.Permission)
meta.Meta.Resource = meta.Resource
meta.Resource.Permission = permission
meta.SetPermission(permission)
}
}
}
meta.FieldName = meta.GetFieldName()
// call meta config's ConfigureMetaInterface
if meta.Config != nil {
meta.Config.ConfigureQorMeta(meta)
}
// call field's ConfigureMetaInterface
if meta.FieldStruct != nil {
if injector, ok := reflect.New(meta.FieldStruct.Struct.Type).Interface().(resource.ConfigureMetaInterface); ok {
injector.ConfigureQorMeta(meta)
}
}
// run meta configors
if baseResource := meta.baseResource; baseResource != nil {
for key, fc := range baseResource.GetAdmin().metaConfigorMaps {
if key == meta.Type {
fc(meta)
}
}
}
for _, processor := range meta.processors {
processor.Handler(meta)
}
}
// DBName get meta's db name, used in index page for sorting
func (meta *Meta) DBName() string {
if meta.FieldStruct != nil {
return meta.FieldStruct.DBName
}
return ""
}