Strip nil routes from all positions

This commit is contained in:
Tommi Reiman 2018-06-03 17:48:47 +03:00
parent d53bbdfdf4
commit d48515e084
3 changed files with 31 additions and 12 deletions

View file

@ -1,5 +1,17 @@
## 0.1.2-SNAPSHOT
### `reitit-core`
* Better handling of `nil` routes - they filtered away from route syntax before routes are expanded:
```clj
(testing "nil routes are allowed ans stripped"
(is (= [] (r/routes (r/router nil))))
(is (= [] (r/routes (r/router [nil [nil] [[nil nil nil]]]))))
(is (= [["/ping" {} nil]] (r/routes (r/router [nil [nil] ["/ping"]]))))
(is (= [["/ping" {} nil]] (r/routes (r/router [[[nil [nil] ["/ping"]]]])))))
```
### `reitit-schema`
* updated dependencies:

View file

@ -37,16 +37,17 @@
[(walk-many [p m r]
(reduce #(into %1 (walk-one p m %2)) [] r))
(walk-one [pacc macc routes]
(if (vector? (first routes))
(walk-many pacc macc routes)
(let [[path & [maybe-arg :as args]] routes
[data childs] (if (vector? maybe-arg)
[{} args]
[maybe-arg (rest args)])
macc (into macc (expand data opts))]
(if (seq childs)
(walk-many (str pacc path) macc childs)
[[(str pacc path) macc]]))))]
(if-let [routes (seq (keep identity routes))]
(if (vector? (first routes))
(walk-many pacc macc routes)
(let [[path & [maybe-arg :as args]] routes
[data childs] (if (vector? maybe-arg)
[{} args]
[maybe-arg (rest args)])
macc (into macc (expand data opts))]
(if (seq childs)
(walk-many (str pacc path) macc childs)
[[(str pacc path) macc]])))))]
(walk-one path (mapv identity data) raw-routes)))
(defn map-data [f routes]
@ -87,10 +88,10 @@
(conflicts-str conflicts)
{:conflicts conflicts})))
(defn name-lookup [[_ {:keys [name]}] opts]
(defn name-lookup [[_ {:keys [name]}] _]
(if name #{name}))
(defn find-names [routes opts]
(defn find-names [routes _]
(into [] (keep #(-> % second :name)) routes))
(defn- compile-route [[p m :as route] {:keys [compile] :as opts}]

View file

@ -108,6 +108,12 @@
r/segment-router :segment-router
r/mixed-router :mixed-router))
(testing "nil routes are allowed ans stripped"
(is (= [] (r/routes (r/router nil))))
(is (= [] (r/routes (r/router [nil [nil] [[nil nil nil]]]))))
(is (= [["/ping" {} nil]] (r/routes (r/router [nil [nil] ["/ping"]]))))
(is (= [["/ping" {} nil]] (r/routes (r/router [[[nil [nil] ["/ping"]]]])))))
(testing "route coercion & compilation"
(testing "custom compile"