-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
202 lines (157 loc) · 3.93 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
package box
import (
"net/http"
"net/url"
"reflect"
"runtime"
"strings"
)
// R stands for Resource
type R struct {
Attr
// Path is a literal or placeholder that matches with a portion of the path
Path string
// Parent is a reference to parent resource
Parent *R
// Children is the list of descendent resources
Children []*R
// Interceptors is the list of actions that will be executed before each
// action or resource under this resource
Interceptors []I
// actions contains all actions (bound and unbound)
actionsByName map[string]*A
// bound contains only bound actions
actionsByHttp map[string]*A
}
func NewResource() *R {
return &R{
Attr: Attr{},
Path: "",
Children: []*R{},
actionsByName: map[string]*A{},
actionsByHttp: map[string]*A{},
Interceptors: []I{},
}
}
// TODO: maybe parameters should be a type `P`
func (r *R) Match(path string, parameters map[string]string) (result *R) {
parts := strings.SplitN(path, "/", 2)
current, err := url.QueryUnescape(parts[0])
if nil != err {
// TODO: maybe log debug "unescape" error ??
return
}
if strings.HasPrefix(r.Path, "{") && strings.HasSuffix(r.Path, "}") {
// Match with pattern
parameter := r.Path
parameter = strings.TrimPrefix(parameter, "{")
parameter = strings.TrimSuffix(parameter, "}")
parameters[parameter] = current
} else if r.Path == current {
// Exact match
} else if r.Path == "*" {
// Delegation match
parameters["*"] = path
return r
} else {
// No match
return nil // TODO: maybe return error no match ?¿?¿?
}
if len(parts) == 1 {
return r
}
for _, c := range r.Children {
if result := c.Match(parts[1], parameters); nil != result {
return result
}
}
return
}
func (r *R) resourceParts(parts []string) *R {
if len(parts) == 0 {
return r
}
part := parts[0]
for _, child := range r.Children {
if child.Path == part {
return child.resourceParts(parts[1:])
}
}
child := NewResource()
child.Path = part
child.Parent = r
r.Children = append(r.Children, child)
return child.resourceParts(parts[1:])
}
// Resource defines a new resource below current resource
func (r *R) Resource(locator string) *R {
if locator == "" {
return r
}
locator = strings.TrimPrefix(locator, "/")
parts := strings.Split(locator, "/")
return r.resourceParts(parts)
}
// Group is an alias of Resource
func (r *R) Group(path string) *R {
return r.Resource(path)
}
// Add action to this resource
func (r *R) WithActions(action ...*A) *R {
for _, a := range action {
if "" == a.Name {
a.Name = getFunctionName(a.handler)
a.Name = actionNameNormalizer(a.Name)
}
a.resource = r
r.actionsByName[a.Name] = a
h := a.HttpMethod + " "
if !a.Bound {
h += a.Name
}
r.actionsByHttp[h] = a
}
return r
}
// GetActions retrieve the slice of actions defined in this resource
func (r *R) GetActions() []*A {
var actions []*A
for _, a := range r.actionsByName {
actions = append(actions, a)
}
return actions
}
// Add interceptor to this resource
func (r *R) WithInterceptors(interceptor ...I) *R {
for _, i := range interceptor {
r.Interceptors = append(r.Interceptors, i)
}
return r
}
func (r *R) WithAttribute(key string, value interface{}) *R {
r.SetAttribute(key, value)
return r
}
func (r *R) HandleFunc(method string, path string, handler http.HandlerFunc) *A {
return r.Handle(method, path, handler)
}
func (r *R) Handle(method string, path string, handler interface{}) *A {
a := actionBound(handler, method)
r.Resource(path).WithActions(a)
return a
}
// Use is an alias of WithInterceptors
func (r *R) Use(interceptor ...I) *R {
return r.WithInterceptors(interceptor...)
}
func actionNameNormalizer(u string) string {
if len(u) == 0 {
return u
}
return strings.ToLower(u[0:1]) + u[1:]
}
func getFunctionName(i interface{}) string {
name := runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
parts := strings.Split(name, ".")
return parts[len(parts)-1]
}