mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +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 |
|
| key | description |
|
||||||
| -----------------------------|-------------|
|
| -----------------------------|-------------|
|
||||||
| `:reitit.trie/trie-compiler` | Optional trie-compiler.
|
| `: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]
|
([compiled-routes]
|
||||||
(linear-router compiled-routes {}))
|
(linear-router compiled-routes {}))
|
||||||
([compiled-routes opts]
|
([compiled-routes opts]
|
||||||
|
|
@ -176,7 +176,7 @@
|
||||||
| key | description |
|
| key | description |
|
||||||
| -----------------------------|-------------|
|
| -----------------------------|-------------|
|
||||||
| `:reitit.trie/trie-compiler` | Optional trie-compiler.
|
| `: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]
|
([compiled-routes]
|
||||||
(trie-router compiled-routes {}))
|
(trie-router compiled-routes {}))
|
||||||
([compiled-routes opts]
|
([compiled-routes opts]
|
||||||
|
|
|
||||||
|
|
@ -288,17 +288,19 @@
|
||||||
;; Managing Tries
|
;; Managing Tries
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
(defn- map-parameters [keys]
|
||||||
|
(zipmap keys (repeat nil)))
|
||||||
|
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(def record-parameters
|
(def record-parameters
|
||||||
"Memoized function to transform parameters into runtime generated Record."
|
"Memoized function to transform parameters into runtime generated Record."
|
||||||
(memoize
|
(memoize
|
||||||
(fn [params]
|
(fn [keys]
|
||||||
(let [fields (keys params)]
|
(if (some qualified-keyword? keys)
|
||||||
(if (some qualified-keyword? fields)
|
(map-parameters keys)
|
||||||
params
|
(let [sym (gensym "PathParams")
|
||||||
(let [sym (gensym "PathParams")
|
ctor (symbol (str "map->" sym))]
|
||||||
ctor (symbol (str "map->" sym))]
|
(eval `(do (defrecord ~sym ~(mapv (comp symbol name) keys)) (~ctor {})))))))))
|
||||||
(eval `(do (defrecord ~sym ~(mapv (comp symbol name) fields)) (~ctor {}))))))))))
|
|
||||||
|
|
||||||
(defn insert
|
(defn insert
|
||||||
"Returns a trie with routes added to it."
|
"Returns a trie with routes added to it."
|
||||||
|
|
@ -311,9 +313,9 @@
|
||||||
node routes))
|
node routes))
|
||||||
([node path data]
|
([node path data]
|
||||||
(insert node path data nil))
|
(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)
|
(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))))
|
(-insert (or node (-node {})) (split-path path) path params data))))
|
||||||
|
|
||||||
(defn compiler
|
(defn compiler
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue