This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-03-18
Channels
- # beginners (123)
- # boot (3)
- # cider (6)
- # clara (1)
- # cljs-dev (139)
- # cljsrn (9)
- # clojure (59)
- # clojure-italy (5)
- # clojure-uk (41)
- # clojured (10)
- # clojurescript (67)
- # community-development (1)
- # component (2)
- # core-async (7)
- # cursive (4)
- # datomic (4)
- # defnpodcast (2)
- # fulcro (23)
- # graphql (1)
- # jobs (2)
- # leiningen (4)
- # off-topic (32)
- # portkey (7)
- # protorepl (5)
- # re-frame (19)
- # reagent (5)
- # reitit (23)
- # shadow-cljs (29)
- # slack-help (1)
- # spacemacs (1)
- # tools-deps (21)
- # unrepl (18)
@ikitommi sure, something like this
(def router
(reitit/router
["/add-user/:tab" {:name ::home-add-user
:coercion reitit.coercion.spec/coercion
:parameters {:path {:tab keyword?}}}]
{:compile reitit.coercion/compile-request-coercers}))
(defn match-by-path-and-coerce! [path]
(when-let [match (reitit/match-by-path router path)]
(assoc match :parameters (reitit.coercion/coerce! match))))
(match-by-path-and-coerce! "/add-user/admin")
;; {:template "/add-user/:tab",
;; :data
;; {:name :transportal.routes/home-add-user,
;; :coercion #object[reitit.coercion.spec.t_reitit$coercion$spec127758],
;; :parameters {:path {:tab #object[cljs$core$keyword_QMARK_]}}},
;; :result {:path #object[Function]},
;; :path-params {:tab "admin"},
;; :path "/add-user/admin",
;; :parameters {:path {:tab :admin}}}
(reitit/match-by-name router ::home-add-user {:tab :admin})
;; {:template "/add-user/:tab",
;; :data
;; {:name :transportal.routes/home-add-user,
;; :coercion #object[reitit.coercion.spec.t_reitit$coercion$spec127758],
;; :parameters {:path {:tab #object[cljs$core$keyword_QMARK_]}}},
;; :result {:path #object[Function]},
;; :path-params {:tab :admin},
;; :path "/add-user/:admin"}
I'm replacing bidi
with reitit
in our project and this is the first thing that doesn't work "out of the box"
@ikitommi tried that:
(defn match-by-name-and-coerce! [name params]
(when-let [match (reitit/match-by-name router name params)]
(assoc match :parameters (reitit.coercion/coerce! match))))
(match-by-name-and-coerce! ::home-add-user {:tab :admin})
;; {:template "/add-user/:tab",
;; :data
;; {:name :transportal.routes/home-add-user,
;; :coercion #object[reitit.coercion.spec.t_reitit$coercion$spec127758],
;; :parameters {:path {:tab #object[cljs$core$keyword_QMARK_]}}},
;; :result {:path #object[Function]},
;; :path-params {:tab :admin},
;; :path "/add-user/:admin",
;; :parameters {:path {:tab :admin}}}
bidi
has this working: https://github.com/juxt/bidi#keywords
oh, I see. One option would be run JSON encoding on the params. Would transform dates etc.
Multimethods (`stringify`) or Protocol (`Stringify`) would also work, but that would make adding new types hard - too many things to extend.
Just added few days ago the support for self-contained (conforming) Specs into spec-tools, maybe just do the same for the other way, would be something like “spec bijections”. Maybe like this?
(require '[spec-tools.core :as st])
(require '[spec-tools.conform :as conform])
(require '[spec-tools.unform :as unform]) ;; new
(def my-keyword?
(st/spec
{:spec keyword?
:description "a bijecting keyword"
::unform/string #(name %2)
::conform/string #(keyword %2)}))
;;
;; string -> keyword
;;
(st/conform my-keyword? "kikka") ; => ::s/invalid
(st/conform my-keyword? "kikka" st/string-conforming) ; => :kikka
;;
;; keyword -> string
;;
(st/unconform my-keyword? :kikka) ; => :kikka
(st/unconform my-keyword? :kikka st/string-conforming) ; => "kikka"
wrote an issue of the bijection to spec-tools anyways already: https://github.com/metosin/spec-tools/issues/112