diff --git a/CHANGELOG.md b/CHANGELOG.md index e56d2723..98500a5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/modules/reitit-core/src/reitit/core.cljc b/modules/reitit-core/src/reitit/core.cljc index 35d2c87b..3a98393e 100644 --- a/modules/reitit-core/src/reitit/core.cljc +++ b/modules/reitit-core/src/reitit/core.cljc @@ -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}] diff --git a/test/cljc/reitit/core_test.cljc b/test/cljc/reitit/core_test.cljc index e8144e75..f5d23bc4 100644 --- a/test/cljc/reitit/core_test.cljc +++ b/test/cljc/reitit/core_test.cljc @@ -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"