-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
251 lines (215 loc) · 6.32 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
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
package lazyaction
import (
"net/url"
"path"
"reflect"
"strings"
"golazy.dev/lazyaction/internal/args"
"golazy.dev/lazysupport"
)
type ResourceOptions struct {
ControllerName string // PostsController
Plural string // posts
Singular string // post
PathNames struct{ New, Edit string }
Path string // "" means default, "/" means empty
Scheme string
Domain string
Port string
ParamName string
Name string
}
type Resource struct {
BaseUrl url.URL
Befores []string
Afters []string
Layout *args.Fn
ResourceOptions
Controller any
Prefix []string
}
func newResource(controller any, opts *ResourceOptions) (*Resource, error) {
r := &Resource{
Controller: controller,
}
if opts != nil {
r.ResourceOptions = *opts
}
if r.PathNames.New == "" {
r.PathNames.New = "new"
}
if r.PathNames.Edit == "" {
r.PathNames.Edit = "edit"
}
cType := reflect.TypeOf(r.Controller)
if r.ControllerName == "" {
r.ControllerName = cType.Elem().Name() // "PostsController"
}
if r.ControllerName == "Controller" {
n := path.Base(cType.Elem().PkgPath())
r.ControllerName = lazysupport.Camelize(n) + "Controller"
}
if r.Name == "" {
r.Name = strings.TrimSuffix(r.ControllerName, "Controller") // "Posts"
}
if r.Plural == "" {
r.Plural = lazysupport.Underscorize(r.Name) // "posts"
}
if r.Singular == "" {
r.Singular = lazysupport.Singularize(lazysupport.Underscorize(r.Name))
}
if r.Path == "" {
r.Path = "/"
r.Prefix = []string{r.Plural}
} else if r.Path != "/" {
r.Prefix = strings.Split(strings.TrimPrefix(r.Path, "/"), "/")
}
if r.ParamName == "" {
r.ParamName = ":" + r.Singular + "_id" // :post_id
} else {
r.ParamName = ":" + strings.TrimPrefix(r.ParamName, ":")
}
return r, nil
}
// func (r *Resource) addSubResources() {
// for _, sr := range r.SubResources {
// resource := NewResource(sr)
// for _, action := range resource.ResourceActions {
// a := *action
// segments := make([]string, len(r.Prefix))
// copy(segments, r.Prefix)
// segments = append(segments, r.ParamName)
//
// a.Path = "/" + strings.Join(segments, "/") + a.Path
// a.RouteName = r.Singular + "_" + a.ResourceName
// if a.ActionName != "" {
// a.RouteName = a.ActionName + "_" + a.RouteName
// }
//
// for i := range a.ParamsPosition {
// a.ParamsPosition[i] += len(segments)
// }
//
// a.ParamsPosition = append([]int{len(segments) - 1}, a.ParamsPosition...)
//
// r.ResourceActions = append(r.ResourceActions, &a)
// }
// }
// }
func (r *Resource) Actions() []*Action {
actions := []*Action{}
cType := reflect.TypeOf(r.Controller)
cVal := reflect.ValueOf(r.Controller)
generators := make(map[string][]args.Gen)
for i := 0; i < cType.NumMethod(); i++ {
methodT := cType.Method(i)
methodV := cVal.Method(i)
name := methodT.Name
switch {
case isAction(name):
actions = append(actions, r.genActionsForMethodN(cVal, i)...)
case strings.HasPrefix(name, "Gen"):
fn := args.NewGen((cVal.Method(i)))
t := fn.Outs[0]
generators[t] = append(generators[t], fn)
case name == "RenderLayout":
r.Layout = args.NewFn(methodV)
case strings.HasPrefix(name, "Before"):
r.Befores = append(r.Befores, name)
case strings.HasPrefix(name, "After"):
r.Afters = append(r.Afters, name)
}
}
// fill actions with the generator
for _, action := range actions {
action.Generators = &generators
action.Layout = r.Layout
}
return actions
}
func (r *Resource) genActionsForMethodN(val reflect.Value, i int) []*Action {
routes := []*Action{}
meth := val.Method(i)
cType := reflect.TypeOf(r.Controller)
methodT := cType.Method(i)
name := methodT.Name
verb, path, methodName, paramName := r.analyzeName(name)
routeName := lazysupport.Underscorize(r.Name) + "#" + lazysupport.Underscorize(methodName)
u, err := url.Parse(path)
if err != nil {
panic(err)
}
u.Scheme = r.Scheme
u.Host = r.Domain
if r.Port != "" {
u.Host = u.Host + ":" + r.Port
}
fn := args.NewFn(meth)
route := &Action{
Verb: verb,
Method: name,
methodI: i,
URL: *u,
Name: routeName,
ins: fn.Ins,
outs: fn.Outs,
ControllerName: r.ControllerName,
Plural: r.Plural,
Singular: r.Singular,
ParamName: paramName,
Controller: r.Controller,
}
routes = append(routes, route)
return routes
}
func isAction(name string) bool {
switch name {
case "Index", "Create", "New", "Show", "Edit", "Update", "Destroy":
return true
}
name = strings.TrimPrefix(name, Member)
return prefixes.HasPrefix(name)
}
func (r *Resource) analyzeName(method string) (verb, path, methodName, paramName string) {
pathSegments := make([]string, len(r.Prefix))
copy(pathSegments, r.Prefix)
switch method {
case "Index":
return "GET", "/" + strings.Join(pathSegments, "/"), method, ""
case "Create":
return "POST", "/" + strings.Join(pathSegments, "/"), method, ""
case "New":
pathSegments = append(pathSegments, r.PathNames.New)
return "GET", "/" + strings.Join(pathSegments, "/"), method, ""
case "Show":
pathSegments = append(pathSegments, r.ParamName)
return "GET", "/" + strings.Join(pathSegments, "/"), method, r.ParamName
case "Edit":
pathSegments = append(pathSegments, r.ParamName)
pathSegments = append(pathSegments, r.PathNames.Edit)
return "GET", "/" + strings.Join(pathSegments, "/"), method, r.ParamName
case "Update":
pathSegments = append(pathSegments, r.ParamName)
return "PUT,PATCH", "/" + strings.Join(pathSegments, "/"), method, r.ParamName
case "Destroy":
pathSegments = append(pathSegments, r.ParamName)
return "DELETE", "/" + strings.Join(pathSegments, "/"), "destroy", r.ParamName
}
// Add param if it is a member function
if strings.HasPrefix(method, Member) {
pathSegments = append(pathSegments, r.ParamName)
method = strings.TrimPrefix(method, Member)
paramName = r.ParamName
}
verb, method = prefixes.TrimPrefix(method)
if method != "" {
pathSegments = append(pathSegments, lazysupport.Underscorize(method))
}
if verb == "" {
verb = "GET"
}
return strings.ToUpper(verb), "/" + strings.Join(pathSegments, "/"), method, paramName
}
var prefixes = lazysupport.NewStringSet("Get", "Post", "Delete", "Patch", "Put", "Options")
var DefaultActions = lazysupport.NewStringSet("Index", "Show", "Create", "Update", "Destroy", "New", "Edit")
const Member = "Member"