Hi! Is it possible to write my router data to get qualified keywords out of path parameters? I've currently got this:
(-> @!last :reitit.core/match :path-params)
;; => {:slug "enklere-7"}
But I want :doc/slug, not :slug, as that's the well known attribute the rest of my app talks about.
Also, thanks for making Reitit! It has just worked for me for several years! 🙌
Edit: moved details to thread.More details: The router part of my app is:
["/doc/:slug" {:get #'serve-page
:name :page-registry/doc}]
I was hoping I could give the slug a qualified keyword. So far, my reading of the documentation has been unfruitful. I hoped for something like:
[["doc" :doc/slug] {:get #'serve-page
:name :page-registry/doc}]
;; or, more vebosely, but keep string on the left,
["/doc/:slug" {:get #'serve-page
:name :page-registry/doc
:rename-params {:slug :doc/slug}}]
When I do get it as [:doc/slug "enklere-7"], I can do nice stuff like automatically provide Datomic entities for the referenced document, check whether it actually exists, etc, with generic page machinery instead of muddling individual pages with error handling.If qualified keywords for parameters isn't supported, I'd love advice on how to "plug this in" for myself.
Ultimately, I want a request model like this:
{:request/id #uuid "85b45e6d-a617-4286-9c99-594f1c3a0c6c"
:session/id #uuid "2be72578-6d49-4685-88b5-1a789f2c9a84"
:doc/slug "enklere-7"}
, and quickly parse the ring request into my request model as close to the edge of my system as possible.You should be able to use "/doc/{doc/slug}" as the pattern and get out a qualified kw
Documented here: https://cljdoc.org/d/metosin/reitit/0.10.0/doc/basics/route-syntax
That solves my problem exactly. Thank you!
(def handle identity)
(-> {:request-method :get
:uri "/doc/blabla"}
((reitit.ring/ring-handler
(reitit.ring/router
["/doc/{doc/slug}" {:get #'handle}])))
:path-params)
;; => {:doc/slug "blabla"}I think "your keywords can have namespaces" would be worth an explicit mention on the doc page, I didn't understand I could do just that.
on the page I just linked? or somewhere more prominent?
At least on that page, that's what I intended. Personally, I believe parsing straight into qualified keywords is a great feature. If I was writing a router library, I'd want namespaced attributes all the way out to the edge of my system. So I'd want to see them in early Reitit examples too.
I was just wondering because the start of that page says > Parameters can also be wrapped in brackets, enabling use of qualified keywords {user/id}, {*user/path} Maybe some of the examples could have qualified kws?
You're right! I missed that. Examples would definitely have helped 👍
how's this? https://github.com/metosin/reitit/pull/781/changes
NB. the {:foo/bar} version of the syntax is not yet released, it was implemented recently in https://github.com/metosin/reitit/pull/770
That looks excellent to me! Glad you put a visual example of :user/id in the introduction test as well. Easy to reconize when looking for qualified keywords!
thanks for the feedback! I'll try to get a new release out this week so you can use the colon version 🙂
Thank you! Both for the prompt help and the prompt documentation improvements 🙏