mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
:reitit.trie/parameters option takes sequence of keys
This commit is contained in:
parent
38cbb5d2f3
commit
36634abd39
2 changed files with 13 additions and 11 deletions
|
|
@ -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]
|
||||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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."
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue