-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
querystorehandler.go
365 lines (326 loc) · 8.99 KB
/
querystorehandler.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package store
import (
"fmt"
"net/url"
res "github.com/jirenius/go-res"
)
// QueryHandler is a res.Service handler for get requests that fetches the
// results from an underlying QueryStore. It also listens to changes in the
// QueryStore, and sends events if the change affects the query.
type QueryHandler struct {
// A QueryStore from where to fetch the resource references.
QueryStore QueryStore
// QueryRequestHandler transforms an external query request and path param
// values into a query that can be handled by the QueryStore. The
// QueryHandler will respond with a query resource.
//
// A non-empty normalized query string based on the url.Values must be
// returned. See:
// https://resgate.io/docs/specification/res-service-protocol/#get-request
//
// Must not be set if RequestHandler is set.
QueryRequestHandler func(rname string, pathParams map[string]string, q url.Values) (url.Values, string, error)
// RequestHandler transforms an external request and path param values into
// a query that can be handled by the QueryStore.
//
// Must not be set if QueryRequestHandler is set.
RequestHandler func(rname string, pathParams map[string]string) (url.Values, error)
// Transformer transforms the internal query results and events into an
// external resource, and events for the external resource.
Transformer QueryTransformer
// AffectedResources is called on query change, and should return a list of
// resources affected by the change.
//
// This is only required if the resource contains path parameters.
//
// Example
//
// If a the handler listens for requests on: library.books.$firstLetter A
// change of a book's name from "Alpha" to "Beta" should have
// AffectedResources return the following:
//
// []string{"library.books.a", "library.books.b"}
AffectedResources func(res.Pattern, QueryChange) []string
}
var _ res.Option = QueryHandler{}
type queryHandler struct {
s *res.Service
pattern res.Pattern
typ res.ResourceType
qs QueryStore
qrh func(string, map[string]string, url.Values) (url.Values, string, error)
rh func(string, map[string]string) (url.Values, error)
trans QueryTransformer
ar func(res.Pattern, QueryChange) []string
isQuery bool
}
// WithQueryStore returns a new QueryHandler value with QueryStore set to
// qstore.
func (qh QueryHandler) WithQueryStore(qstore QueryStore) QueryHandler {
qh.QueryStore = qstore
return qh
}
// WithQueryRequestHandler returns a new QueryHandler value with
// QueryRequestHandler set to f.
func (qh QueryHandler) WithQueryRequestHandler(f func(rname string, pathParams map[string]string, q url.Values) (url.Values, string, error)) QueryHandler {
qh.QueryRequestHandler = f
return qh
}
// WithRequestHandler returns a new QueryHandler value with RequestHandler set
// to f.
func (qh QueryHandler) WithRequestHandler(f func(rname string, pathParams map[string]string) (url.Values, error)) QueryHandler {
qh.RequestHandler = f
return qh
}
// WithTransformer returns a new QueryHandler value with Tranformer set to
// transformer.
func (qh QueryHandler) WithTransformer(transformer QueryTransformer) QueryHandler {
qh.Transformer = transformer
return qh
}
// WithAffectedResources returns a new QueryHandler value with IDToRID set to f.
func (qh QueryHandler) WithAffectedResources(f func(res.Pattern, QueryChange) []string) QueryHandler {
qh.AffectedResources = f
return qh
}
// SetOption is to implement the res.Option interface.
func (qh QueryHandler) SetOption(h *res.Handler) {
if qh.QueryStore == nil {
panic("no QueryStore is set")
}
if qh.QueryRequestHandler != nil && qh.RequestHandler != nil {
panic("both RequestHandler and QueryRequestHandler is set")
}
o := queryHandler{
qs: qh.QueryStore,
qrh: qh.QueryRequestHandler,
rh: qh.RequestHandler,
trans: qh.Transformer,
ar: qh.AffectedResources,
isQuery: qh.QueryRequestHandler != nil,
}
h.Option(res.OnRegister(o.onRegister))
// Set conditional handler methods depending if we have
// an ordinary resource or a query resource.
if o.isQuery {
h.Option(res.GetResource(o.getQueryResource))
o.qs.OnQueryChange(o.queryChangeHandler)
} else {
h.Option(res.GetResource(o.getResource))
o.qs.OnQueryChange(o.changeHandler)
}
}
func (o *queryHandler) onRegister(s *res.Service, p res.Pattern, h res.Handler) {
if res.Pattern(p).IndexWildcard() >= 0 {
if o.ar == nil {
panic("QueryHandler requires an AffectedResources callback when handling resources with tags or wildcards: " + string(p))
}
}
if h.Type == res.TypeUnset {
panic("no Type is set")
}
if h.Type != res.TypeModel && h.Type != res.TypeCollection {
panic("Type must be set to TypeModel or TypeCollection")
}
o.s = s
o.pattern = p
o.typ = h.Type
}
func (o *queryHandler) getResource(r res.GetRequest) {
var err error
var q url.Values
if o.rh != nil {
q, err = o.rh(r.ResourceName(), r.PathParams())
if err != nil {
r.Error(err)
return
}
}
result, err := o.getResult(q)
if err != nil {
r.Error(err)
return
}
switch o.typ {
case res.TypeModel:
r.Model(result)
case res.TypeCollection:
r.Collection(result)
default:
panic("invalid type")
}
}
func (o *queryHandler) getQueryResource(r res.GetRequest) {
q, norm, err := o.qrh(r.ResourceName(), r.PathParams(), r.ParseQuery())
if err != nil {
r.Error(err)
return
}
result, err := o.getResult(q)
if err != nil {
r.Error(err)
return
}
if norm == "" {
panic("QueryResourceHandler returned an empty normalized query string for resource: " + r.ResourceName())
}
switch o.typ {
case res.TypeModel:
r.QueryModel(result, norm)
case res.TypeCollection:
r.QueryCollection(result, norm)
default:
panic("invalid type")
}
}
func (o *queryHandler) queryChangeHandler(qc QueryChange) {
if o.ar != nil {
qrids := o.ar(o.pattern, qc)
for _, qrid := range qrids {
o.queryEvent(qrid, qc)
}
} else {
o.queryEvent(string(o.pattern), qc)
}
}
func (o *queryHandler) changeHandler(qc QueryChange) {
if o.ar != nil {
rids := o.ar(o.pattern, qc)
for _, rid := range rids {
if err := o.resourceEvent(rid, qc); err != nil {
o.errorf("QueryHandler encountered error generating events for resource %s: %s", rid, err)
return
}
}
} else {
if err := o.resourceEvent(string(o.pattern), qc); err != nil {
o.errorf("QueryHandler encountered error generating events for resource %s: %s", o.pattern, err)
}
}
}
func (o *queryHandler) resourceEvent(rid string, qc QueryChange) error {
r, err := o.s.Resource(rid)
if err != nil {
return fmt.Errorf("error getting resource: %s", err)
}
var q url.Values
if o.rh != nil {
q, err = o.rh(r.ResourceName(), r.PathParams())
if err != nil {
return fmt.Errorf("error calling RequestHandler: %s", err)
}
}
// Get events. We assume Events will will by itself determine if
// the query is affected or not, by returning a no events.
evs, reset, err := qc.Events(q)
if err != nil {
return fmt.Errorf("error getting events: %s", err)
}
if reset {
r.ResetEvent()
return nil
}
if len(evs) == 0 {
return nil
}
if o.trans != nil {
evs, err = o.trans.TransformEvents(evs)
if err != nil {
return fmt.Errorf("error transforming events: %s", err)
}
}
for _, ev := range evs {
switch ev.Name {
case "add":
r.AddEvent(ev.Value, ev.Idx)
case "remove":
r.RemoveEvent(ev.Idx)
case "change":
r.ChangeEvent(ev.Changed)
default:
return fmt.Errorf("invalid event name: %s", ev.Name)
}
}
return nil
}
func (o *queryHandler) queryEvent(qrid string, qc QueryChange) {
qcr, err := o.s.Resource(qrid)
if err != nil {
panic(err)
}
qcr.QueryEvent(func(qreq res.QueryRequest) {
// Nil means end of query event.
if qreq == nil {
return
}
q, _, err := o.qrh(qreq.ResourceName(), qreq.PathParams(), qreq.ParseQuery())
if err != nil {
qreq.Error(err)
return
}
// Get events for the query
evs, reset, err := qc.Events(q)
if err != nil {
qreq.Error(err)
return
}
// Handle reset
if reset {
result, err := o.getResult(q)
if err != nil {
qreq.Error(err)
return
}
switch o.typ {
case res.TypeModel:
qreq.Model(result)
case res.TypeCollection:
qreq.Collection(result)
default:
panic("invalid type")
}
return
}
if len(evs) == 0 {
return
}
if o.trans != nil {
evs, err = o.trans.TransformEvents(evs)
if err != nil {
panic(err)
}
}
for _, ev := range evs {
switch ev.Name {
case "add":
qreq.AddEvent(ev.Value, ev.Idx)
case "remove":
qreq.RemoveEvent(ev.Idx)
case "change":
qreq.ChangeEvent(ev.Changed)
default:
panic("invalid event name: " + ev.Name)
}
}
})
}
func (o *queryHandler) getResult(q url.Values) (interface{}, error) {
result, err := o.qs.Query(q)
if err != nil {
return nil, err
}
// Transform results
if o.trans != nil {
result, err = o.trans.TransformResult(result)
if err != nil {
return nil, err
}
}
return result, nil
}
func (o *queryHandler) errorf(format string, v ...interface{}) {
l := o.s.Logger()
if l != nil {
l.Errorf(format, v...)
}
}