Fix expand, fixes #201

This commit is contained in:
Tommi Reiman 2019-01-14 10:07:16 +02:00
parent a1be00842a
commit 35ff62a1da
3 changed files with 26 additions and 10 deletions

View file

@ -1,3 +1,10 @@
## UNRELEASED
### `reitit-core`
* `reitit.core/Expand` can be extended, fixes [#201](https://github.com/metosin/reitit/issues/201).
## 0.2.10 (2018-12-30)
### `reitit-core`

View file

@ -112,10 +112,10 @@
(f conflicts)
{:conflicts conflicts})))
(defn name-lookup [[_ {:keys [name]}] _]
(defn- name-lookup [[_ {:keys [name]}] _]
(if name #{name}))
(defn find-names [routes _]
(defn- find-names [routes _]
(into [] (keep #(-> % second :name)) routes))
(defn- compile-route [[p m :as route] {:keys [compile] :as opts}]
@ -164,13 +164,6 @@
([match query-params]
(some-> match :path (cond-> query-params (str "?" (impl/query-string query-params))))))
(def default-router-options
{:lookup name-lookup
:expand expand
:coerce (fn [route _] route)
:compile (fn [[_ {:keys [handler]}] _] handler)
:conflicts (partial throw-on-conflicts! path-conflicts-str)})
(defn linear-router
"Creates a linear-router from resolved routes and optional
expanded options. See [[router]] for available options."
@ -415,6 +408,13 @@
(or (match-by-name mixed-router name path-params)
(match-by-name linear-router name path-params)))))))
(defn ^:no-doc default-router-options []
{:lookup name-lookup
:expand expand
:coerce (fn [route _] route)
:compile (fn [[_ {:keys [handler]}] _] handler)
:conflicts (partial throw-on-conflicts! path-conflicts-str)})
(defn router
"Create a [[Router]] from raw route data and optionally an options map.
Selects implementation based on route details. The following options
@ -435,7 +435,7 @@
([raw-routes]
(router raw-routes {}))
([raw-routes opts]
(let [{:keys [router] :as opts} (merge default-router-options opts)
(let [{:keys [router] :as opts} (merge (default-router-options) opts)
routes (resolve-routes raw-routes opts)
path-conflicting (path-conflicting-routes routes)
name-conflicting (name-conflicting-routes routes)

View file

@ -314,3 +314,12 @@
(-> ["/api"
(list "/ipa")]
(r/router))))))
(defrecord Named [n]
r/Expand
(r/expand [_ _] {:name n}))
(deftest default-expand-test
(let [router (r/router ["/endpoint" (->Named :kikka)])]
(is (= [["/endpoint" {:name :kikka}]]
(r/routes router)))))