/
resource.go
129 lines (111 loc) · 4.04 KB
/
resource.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
package resource
import (
"reflect"
"github.com/jinzhu/gorm"
"github.com/qor/qor"
"github.com/qor/qor/utils"
"github.com/qor/roles"
)
// Resourcer interface
type Resourcer interface {
GetResource() *Resource
GetMetas([]string) []Metaor
CallFindMany(interface{}, *qor.Context) error
CallFindOne(interface{}, *MetaValues, *qor.Context) error
CallSave(interface{}, *qor.Context) error
CallDelete(interface{}, *qor.Context) error
NewSlice() interface{}
NewStruct() interface{}
}
// ConfigureResourceBeforeInitializeInterface if a struct implemented this interface, it will be called before everything when create a resource with the struct
type ConfigureResourceBeforeInitializeInterface interface {
ConfigureQorResourceBeforeInitialize(Resourcer)
}
// ConfigureResourceInterface if a struct implemented this interface, it will be called after configured by user
type ConfigureResourceInterface interface {
ConfigureQorResource(Resourcer)
}
// Resource is a struct that including basic definition of qor resource
type Resource struct {
Name string
Value interface{}
FindManyHandler func(interface{}, *qor.Context) error
FindOneHandler func(interface{}, *MetaValues, *qor.Context) error
SaveHandler func(interface{}, *qor.Context) error
DeleteHandler func(interface{}, *qor.Context) error
Permission *roles.Permission
validators []func(interface{}, *MetaValues, *qor.Context) error
processors []func(interface{}, *MetaValues, *qor.Context) error
primaryField *gorm.Field
}
// New initialize qor resource
func New(value interface{}) *Resource {
var (
name = utils.HumanizeString(utils.ModelType(value).Name())
res = &Resource{Value: value, Name: name}
)
res.FindOneHandler = res.findOneHandler
res.FindManyHandler = res.findManyHandler
res.SaveHandler = res.saveHandler
res.DeleteHandler = res.deleteHandler
return res
}
// GetResource return itself to match interface `Resourcer`
func (res *Resource) GetResource() *Resource {
return res
}
// AddValidator add validator to resource, it will invoked when creating, updating, and will rollback the change if validator return any error
func (res *Resource) AddValidator(fc func(interface{}, *MetaValues, *qor.Context) error) {
res.validators = append(res.validators, fc)
}
// AddProcessor add processor to resource, it is used to process data before creating, updating, will rollback the change if it return any error
func (res *Resource) AddProcessor(fc func(interface{}, *MetaValues, *qor.Context) error) {
res.processors = append(res.processors, fc)
}
// NewStruct initialize a struct for the Resource
func (res *Resource) NewStruct() interface{} {
return reflect.New(reflect.Indirect(reflect.ValueOf(res.Value)).Type()).Interface()
}
// NewSlice initialize a slice of struct for the Resource
func (res *Resource) NewSlice() interface{} {
sliceType := reflect.SliceOf(reflect.TypeOf(res.Value))
slice := reflect.MakeSlice(sliceType, 0, 0)
slicePtr := reflect.New(sliceType)
slicePtr.Elem().Set(slice)
return slicePtr.Interface()
}
// GetMetas get defined metas, to match interface `Resourcer`
func (res *Resource) GetMetas([]string) []Metaor {
panic("not defined")
}
// HasPermission check permission of resource
func (res *Resource) HasPermission(mode roles.PermissionMode, context *qor.Context) bool {
if res == nil || res.Permission == nil {
return true
}
return res.Permission.HasPermission(mode, context.Roles...)
}
// PrimaryField return gorm's primary field
func (res *Resource) PrimaryField() *gorm.Field {
if res.primaryField == nil {
scope := gorm.Scope{Value: res.Value}
res.primaryField = scope.PrimaryField()
}
return res.primaryField
}
// PrimaryDBName return db column name of the resource's primary field
func (res *Resource) PrimaryDBName() (name string) {
field := res.PrimaryField()
if field != nil {
name = field.DBName
}
return
}
// PrimaryFieldName return struct column name of the resource's primary field
func (res *Resource) PrimaryFieldName() (name string) {
field := res.PrimaryField()
if field != nil {
name = field.Name
}
return
}