Skip to content

Commit

Permalink
:reitit.trie/parameters option takes sequence of keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi committed Mar 7, 2019
1 parent 38cbb5d commit 36634ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions modules/reitit-core/src/reitit/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
| key | description |
| -----------------------------|-------------|
| `:reitit.trie/trie-compiler` | Optional trie-compiler.
| `:reitit.trie/parameters` | Optional function to transform path-parameters at creation time (default `identity`)."
| `:reitit.trie/parameters` | Optional function to create empty map(-like) path parameters value from sequence of keys."
([compiled-routes]
(linear-router compiled-routes {}))
([compiled-routes opts]
Expand Down Expand Up @@ -176,7 +176,7 @@
| key | description |
| -----------------------------|-------------|
| `:reitit.trie/trie-compiler` | Optional trie-compiler.
| `:reitit.trie/parameters` | Optional function to transform path-parameters at creation time (default `identity`)."
| `:reitit.trie/parameters` | Optional function to create empty map(-like) path parameters value from sequence of keys."
([compiled-routes]
(trie-router compiled-routes {}))
([compiled-routes opts]
Expand Down
20 changes: 11 additions & 9 deletions modules/reitit-core/src/reitit/trie.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -288,17 +288,19 @@
;; Managing Tries
;;

(defn- map-parameters [keys]
(zipmap keys (repeat nil)))

#?(:clj
(def record-parameters
"Memoized function to transform parameters into runtime generated Record."
(memoize
(fn [params]
(let [fields (keys params)]
(if (some qualified-keyword? fields)
params
(let [sym (gensym "PathParams")
ctor (symbol (str "map->" sym))]
(eval `(do (defrecord ~sym ~(mapv (comp symbol name) fields)) (~ctor {}))))))))))
(fn [keys]
(if (some qualified-keyword? keys)
(map-parameters keys)
(let [sym (gensym "PathParams")
ctor (symbol (str "map->" sym))]
(eval `(do (defrecord ~sym ~(mapv (comp symbol name) keys)) (~ctor {})))))))))

(defn insert
"Returns a trie with routes added to it."
Expand All @@ -311,9 +313,9 @@
node routes))
([node path data]
(insert node path data nil))
([node path data {::keys [parameters] :or {parameters identity}}]
([node path data {::keys [parameters] :or {parameters map-parameters}}]
(let [parts (split-path path)
params (parameters (zipmap (->> parts (remove string?) (map :value)) (repeat nil)))]
params (parameters (->> parts (remove string?) (map :value)))]
(-insert (or node (-node {})) (split-path path) path params data))))

(defn compiler
Expand Down

0 comments on commit 36634ab

Please sign in to comment.