-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
handle.go
204 lines (182 loc) · 4.7 KB
/
handle.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
package perm
import (
"github.com/webx-top/echo"
)
func NewHandle() *Handle {
return &Handle{}
}
type Handler interface {
SetGenerator(fn func(ctx echo.Context) (string, error)) Handler
SetChecker(fn func(ctx echo.Context, parsed interface{}, current string) (interface{}, error)) Handler
SetParser(fn func(ctx echo.Context, rule string) (interface{}, error)) Handler
SetItemLister(fn func(ctx echo.Context) ([]interface{}, error)) Handler
OnRender(fn func(ctx echo.Context) error) Handler
SetIsValid(fn func(ctx echo.Context) bool) Handler
SetTmpl(tmpl string, typ ...string) Handler
Tmpl(typ ...string) string
Generate(ctx echo.Context) (string, error)
Check(ctx echo.Context, parsed interface{}, current string) (interface{}, error)
Parse(ctx echo.Context, rule string) (interface{}, error)
ListItems(ctx echo.Context) ([]interface{}, error)
FireRender(ctx echo.Context) error
IsValid(ctx echo.Context) bool
}
type Handle struct {
generator func(ctx echo.Context) (string, error)
checker func(ctx echo.Context, parsed interface{}, current string) (interface{}, error)
parser func(ctx echo.Context, rule string) (interface{}, error)
itemLister func(ctx echo.Context) ([]interface{}, error)
onRender func(ctx echo.Context) error
isValid func(ctx echo.Context) bool
tmpl string
tmplHead string
tmplFoot string
}
func (u *Handle) SetGenerator(fn func(ctx echo.Context) (string, error)) Handler {
u.generator = fn
return u
}
func (u *Handle) SetIsValid(fn func(ctx echo.Context) bool) Handler {
u.isValid = fn
return u
}
func (u *Handle) SetChecker(fn func(ctx echo.Context, parsed interface{}, current string) (interface{}, error)) Handler {
u.checker = fn
return u
}
func (u *Handle) SetParser(fn func(ctx echo.Context, rule string) (interface{}, error)) Handler {
u.parser = fn
return u
}
func (u *Handle) SetItemLister(fn func(ctx echo.Context) ([]interface{}, error)) Handler {
u.itemLister = fn
return u
}
func (u *Handle) OnRender(fn func(ctx echo.Context) error) Handler {
u.onRender = fn
return u
}
func (u *Handle) SetTmpl(tmpl string, typ ...string) Handler {
if len(typ) == 0 {
u.tmpl = tmpl
} else {
switch typ[0] {
case `head`:
u.tmplHead = tmpl
case `foot`:
u.tmplFoot = tmpl
}
}
return u
}
func (u *Handle) Tmpl(typ ...string) string {
if len(typ) == 0 {
return u.tmpl
}
switch typ[0] {
case `head`:
return u.tmplHead
case `foot`:
return u.tmplFoot
default:
return ``
}
}
func (u *Handle) Generate(ctx echo.Context) (string, error) {
if u.generator == nil {
return ``, nil
}
return u.generator(ctx)
}
func (u *Handle) Check(ctx echo.Context, parsed interface{}, current string) (interface{}, error) {
if u.checker == nil {
return nil, nil
}
return u.checker(ctx, parsed, current)
}
func (u *Handle) Parse(ctx echo.Context, rule string) (interface{}, error) {
if u.parser == nil {
return nil, nil
}
return u.parser(ctx, rule)
}
func (u *Handle) ListItems(ctx echo.Context) ([]interface{}, error) {
if u.itemLister == nil {
return []interface{}{}, nil
}
return u.itemLister(ctx)
}
func (u *Handle) FireRender(ctx echo.Context) error {
if u.onRender == nil {
return nil
}
return u.onRender(ctx)
}
func (u *Handle) IsValid(ctx echo.Context) bool {
if u.isValid == nil {
return true
}
return u.isValid(ctx)
}
func HandleFireRender(ctx echo.Context, config *echo.KVData) (err error) {
for _, item := range config.Slice() {
err = item.X.(*Handle).FireRender(ctx)
if err != nil {
break
}
}
return
}
func HandleGenerate(ctx echo.Context, config *echo.KVData) (mp map[string]string, err error) {
mp = map[string]string{}
var rule string
for _, item := range config.Slice() {
rule, err = item.X.(*Handle).Generate(ctx)
if err != nil {
break
}
if len(rule) > 0 {
mp[item.K] = rule
}
}
return
}
func HandleCheck(ctx echo.Context, config *echo.KVData, current string, typ string, permission string, parsed interface{}) (interface{}, error) {
item := config.GetItem(typ)
if item == nil {
return nil, nil
}
h := item.X.(*Handle)
if parsed == nil {
var err error
parsed, err = h.Parse(ctx, permission)
if err != nil {
return nil, err
}
}
return h.Check(ctx, parsed, current)
}
func HandleBatchCheck(ctx echo.Context, config *echo.KVData, current string, rules map[string]string) (mp map[string]interface{}, err error) {
mp = map[string]interface{}{}
for typ, permission := range rules {
item := config.GetItem(typ)
if item == nil {
continue
}
h := item.X.(*Handle)
var (
parsed interface{}
result interface{}
)
parsed, err = h.Parse(ctx, permission)
if err != nil {
break
}
result, err = h.Check(ctx, parsed, current)
if err != nil {
break
}
mp[item.K] = result
}
return
}