forked from jirenius/go-res
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource.go
219 lines (189 loc) · 7.23 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
package res
import (
"net/url"
)
// Resource represents a resource
type Resource interface {
// Service returns the service instance
Service() *Service
/// Resource returns the resource name.
ResourceName() string
// PathParams returns parameters that are derived from the resource name.
PathParams() map[string]string
// PathParam returns the key placeholder parameter value derived from the resource name.
PathParam(string) string
// Query returns the query part of the resource ID without the question mark separator.
Query() string
// ParseQuery parses the query and returns the corresponding values.
// It silently discards malformed value pairs.
// To check errors use url.ParseQuery(Query()).
ParseQuery() url.Values
// Value gets the resource value as provided from the GetModel or
// GetCollection resource handlers.
// If it fails to get the resource value, or no get handler is
// defined, it returns a nil interface and a *Error type error.
Value() (interface{}, error)
// Event sends a custom event on the resource.
// Will panic if the event is one of the pre-defined or reserved events,
// "change", "add", "remove", "reaccess", or "unsubscribe".
// For pre-defined events, the matching method, ChangeEvent, AddEvent,
// RemoveEvent, or ReaccessEvent should be used instead.
//
// See the protocol specification for more information:
// https://github.com/jirenius/resgate/blob/master/docs/res-service-protocol.md#events
Event(event string, payload interface{})
// ChangeEvents sends a change event with properties that has been changed
// and their new values.
// If props is empty, no event is sent.
// Panics if the resource is not a Model.
// The values must be serializable into JSON primitives, resource references,
// or a delete action objects.
// See the protocol specification for more information:
// https://github.com/jirenius/resgate/blob/master/docs/res-service-protocol.md#model-change-event
ChangeEvent(props interface{})
// AddEvent sends an add event, adding the value at index idx.
// Panics if the resource is not a Collection, or if idx is less than 0.
// The value must be serializable into a JSON primitive or resource reference.
// See the protocol specification for more information:
// https://github.com/jirenius/resgate/blob/master/docs/res-service-protocol.md#collection-add-event
AddEvent(value interface{}, idx int)
// RemoveEvent sends a remove event, removing the value at index idx.
// Panics if the resource is not a Collection, or if idx is less than 0.
// See the protocol specification for more information:
// https://github.com/jirenius/resgate/blob/master/docs/res-service-protocol.md#collection-remove-event
RemoveEvent(idx int)
// ReaccessEvent sends a reaccess event to signal that the resource's access permissions has changed.
// It will invalidate any previous access response sent for the resource.
// See the protocol specification for more information:
// https://github.com/jirenius/resgate/blob/master/docs/res-service-protocol.md#reaccess-event
ReaccessEvent()
}
// resource is the internal implementation of the Resource interface
type resource struct {
rname string
pathParams map[string]string
query string
inGet bool
s *Service
hs *regHandler
}
// Service returns the service instance
func (r *resource) Service() *Service {
return r.s
}
// ResourceName returns the resource name.
func (r *resource) ResourceName() string {
return r.rname
}
// PathParams returns parameters that are derived from the resource name.
func (r *resource) PathParams() map[string]string {
return r.pathParams
}
// PathParam returns the parameter derived from the resource name for the key placeholder.
func (r *resource) PathParam(key string) string {
return r.pathParams[key]
}
// Query returns the query part of the resource ID without the question mark separator.
func (r *resource) Query() string {
return r.query
}
// ParseQuery parses the query and returns the corresponding values.
// It silently discards malformed value pairs.
// To check errors use url.ParseQuery.
func (r *resource) ParseQuery() url.Values {
v, _ := url.ParseQuery(r.query)
return v
}
// Value gets the resource value as provided from the GetModel or
// GetCollection resource handlers.
// If it fails to get the resource value, or no get handler is
// defined, it returns a nil interface and a *Error type error.
// Panics if called from within GetModel or GetCollection handler.
func (r *resource) Value() (interface{}, error) {
// Panic if the getRequest is called within GetModel or GetCollection handler.
if r.inGet {
panic("Value() called from within get handler")
}
gr := &getRequest{resource: r}
gr.executeHandler()
return gr.value, gr.err
}
func isValidPart(p string) bool {
for _, r := range p {
if r == '?' {
return false
}
if r < 33 || r > 126 || r == '?' || r == '*' || r == '>' || r == '.' {
return false
}
}
return true
}
// Event sends a custom event on the resource.
// Will panic if the event is one of the pre-defined or reserved events,
// "change", "delete", "add", "remove", "patch", "reaccess", or "unsubscribe".
// For pre-defined events, the matching method, ChangeEvent, AddEvent,
// RemoveEvent, or ReaccessEvent should be used instead.
//
// This is to ensure compliance with the specifications:
// https://github.com/jirenius/resgate/blob/master/docs/res-service-protocol.md#events
func (r *resource) Event(event string, payload interface{}) {
switch event {
case "change":
panic("res: use ChangeEvent to send change events")
case "delete":
panic(`res: "delete" is a reserved event name`)
case "add":
panic("res: use AddEvent to send add events")
case "remove":
panic("res: use RemoveEvent to send remove events")
case "patch":
panic(`res: "patch" is a reserved event name`)
case "reaccess":
panic("res: use ReaccessEvent to send a reaccess event")
case "unsubscribe":
panic(`res: "unsubscribe" is a reserved event name`)
}
if !isValidPart(event) {
panic(`res: invalid event name`)
}
r.s.event("event."+r.rname+"."+event, payload)
}
// ChangeEvent sends a change event.
// If ev is empty, no event is sent.
// Panics if the resource is not a Model.
func (r *resource) ChangeEvent(ev interface{}) {
if r.hs.typ != rtypeModel {
panic("res: change event only allowed on Models")
}
if ev == nil {
return
}
r.s.event("event."+r.rname+".change", ev)
}
// AddEvent sends an add event, adding the value v at index idx.
// Panics if the resource is not a Collection.
func (r *resource) AddEvent(v interface{}, idx int) {
if r.hs.typ != rtypeCollection {
panic("res: add event only allowed on Collections")
}
if idx < 0 {
panic("res: add event idx less than zero")
}
r.s.event("event."+r.rname+".add", addEvent{Value: v, Idx: idx})
}
// RemoveEvent sends an remove event, removing the value at index idx.
// Panics if the resource is not a Collection.
func (r *resource) RemoveEvent(idx int) {
if r.hs.typ != rtypeCollection {
panic("res: remove event only allowed on Collections")
}
if idx < 0 {
panic("res: remove event idx less than zero")
}
r.s.event("event."+r.rname+".remove", removeEvent{Idx: idx})
}
// ReaccessEvent sends a reaccess event.
func (r *resource) ReaccessEvent() {
r.s.rawEvent("event."+r.rname+".reaccess", nil)
}