Skip to content

Commit 10490f5

Browse files
paulbeskisielk
authored andcommitted
GetQueryTemplates and GetQueryRegexp extraction (#304)
Developers can now extract the query templates and regexps from a router as lists of combined query pairs.
1 parent 9bd9ff2 commit 10490f5

File tree

3 files changed

+394
-260
lines changed

3 files changed

+394
-260
lines changed

README.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,34 @@ func main() {
201201
r.HandleFunc("/products", handler).Methods("POST")
202202
r.HandleFunc("/articles", handler).Methods("GET")
203203
r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
204+
r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
204205
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
205206
t, err := route.GetPathTemplate()
206207
if err != nil {
207208
return err
208209
}
210+
qt, err := route.GetQueriesTemplates()
211+
if err != nil {
212+
return err
213+
}
209214
// p will contain regular expression is compatible with regular expression in Perl, Python, and other languages.
210215
// for instance the regular expression for path '/articles/{id}' will be '^/articles/(?P<v0>[^/]+)$'
211216
p, err := route.GetPathRegexp()
212217
if err != nil {
213218
return err
214219
}
220+
// qr will contain a list of regular expressions with the same semantics as GetPathRegexp,
221+
// just applied to the Queries pairs instead, e.g., 'Queries("surname", "{surname}") will return
222+
// {"^surname=(?P<v0>.*)$}. Where each combined query pair will have an entry in the list.
223+
qr, err := route.GetQueriesRegexp()
224+
if err != nil {
225+
return err
226+
}
215227
m, err := route.GetMethods()
216228
if err != nil {
217229
return err
218230
}
219-
fmt.Println(strings.Join(m, ","), t, p)
231+
fmt.Println(strings.Join(m, ","), strings.Join(qt, ","), strings.Join(qr, ","), t, p)
220232
return nil
221233
})
222234
http.Handle("/", r)
@@ -339,22 +351,34 @@ r.HandleFunc("/", handler)
339351
r.HandleFunc("/products", handler).Methods("POST")
340352
r.HandleFunc("/articles", handler).Methods("GET")
341353
r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
354+
r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
342355
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
343356
t, err := route.GetPathTemplate()
344357
if err != nil {
345358
return err
346359
}
360+
qt, err := route.GetQueriesTemplates()
361+
if err != nil {
362+
return err
363+
}
347364
// p will contain a regular expression that is compatible with regular expressions in Perl, Python, and other languages.
348365
// For example, the regular expression for path '/articles/{id}' will be '^/articles/(?P<v0>[^/]+)$'.
349366
p, err := route.GetPathRegexp()
350367
if err != nil {
351368
return err
352369
}
370+
// qr will contain a list of regular expressions with the same semantics as GetPathRegexp,
371+
// just applied to the Queries pairs instead, e.g., 'Queries("surname", "{surname}") will return
372+
// {"^surname=(?P<v0>.*)$}. Where each combined query pair will have an entry in the list.
373+
qr, err := route.GetQueriesRegexp()
374+
if err != nil {
375+
return err
376+
}
353377
m, err := route.GetMethods()
354378
if err != nil {
355379
return err
356380
}
357-
fmt.Println(strings.Join(m, ","), t, p)
381+
fmt.Println(strings.Join(m, ","), strings.Join(qt, ","), strings.Join(qr, ","), t, p)
358382
return nil
359383
})
360384
```

0 commit comments

Comments
 (0)