mirror of
https://github.com/metosin/reitit.git
synced 2026-02-10 05:23:13 +00:00
path-update
This commit is contained in:
parent
4d0e40f135
commit
550ea6da58
2 changed files with 60 additions and 0 deletions
|
|
@ -9,6 +9,51 @@
|
||||||
(:import (java.net URLEncoder URLDecoder)
|
(:import (java.net URLEncoder URLDecoder)
|
||||||
(java.util HashMap Map))))
|
(java.util HashMap Map))))
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; path-update
|
||||||
|
;;
|
||||||
|
|
||||||
|
(defn -match [path path-map]
|
||||||
|
(letfn [(match [x f] (if (fn? f) (f x) (= x f)))]
|
||||||
|
(reduce
|
||||||
|
(fn [_ [ps f]]
|
||||||
|
(let [match (loop [[p & pr] path, [pp & ppr] ps]
|
||||||
|
(cond (and p pp (match p pp)) (recur pr ppr)
|
||||||
|
(= nil p pp) true))]
|
||||||
|
(when match (reduced f))))
|
||||||
|
nil path-map)))
|
||||||
|
|
||||||
|
(defn -path-vals [m path-map]
|
||||||
|
(letfn [(-path-vals [l p m]
|
||||||
|
(reduce
|
||||||
|
(fn [l [k v]]
|
||||||
|
(let [p' (conj p k)
|
||||||
|
f (-match p' path-map)]
|
||||||
|
(cond
|
||||||
|
f (cons [p' (f v)] l)
|
||||||
|
(and (map? v) (seq v)) (-path-vals l p' v)
|
||||||
|
:else (cons [p' v] l))))
|
||||||
|
l m))]
|
||||||
|
(reverse (-path-vals [] [] m))))
|
||||||
|
|
||||||
|
(defn -assoc-in-path-vals [c]
|
||||||
|
(reduce (partial apply assoc-in) {} c))
|
||||||
|
|
||||||
|
(defn path-update [m path-map]
|
||||||
|
(-> (-path-vals m path-map)
|
||||||
|
(-assoc-in-path-vals)))
|
||||||
|
|
||||||
|
(defn accumulator? [x]
|
||||||
|
(-> x meta ::accumulator))
|
||||||
|
|
||||||
|
(defn accumulate
|
||||||
|
([x] (if-not (accumulator? x) (with-meta [x] {::accumulator true}) x))
|
||||||
|
([x y] (into (accumulate x) y)))
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; impl
|
||||||
|
;;
|
||||||
|
|
||||||
(defn parse [path opts]
|
(defn parse [path opts]
|
||||||
(let [path #?(:clj (.intern ^String (trie/normalize path opts)) :cljs (trie/normalize path opts))
|
(let [path #?(:clj (.intern ^String (trie/normalize path opts)) :cljs (trie/normalize path opts))
|
||||||
path-parts (trie/split-path path opts)
|
path-parts (trie/split-path path opts)
|
||||||
|
|
|
||||||
|
|
@ -171,3 +171,18 @@
|
||||||
:path-parts ["https://google.com"]
|
:path-parts ["https://google.com"]
|
||||||
:path-params #{}}
|
:path-params #{}}
|
||||||
(impl/parse "https://google.com" nil))))
|
(impl/parse "https://google.com" nil))))
|
||||||
|
|
||||||
|
(deftest path-update-test
|
||||||
|
(is (= {:get {:responses {200 {:body [[:map [:total :int]]]}}
|
||||||
|
:parameters {:query [[:map [:x :int]]]}},
|
||||||
|
:parameters {:query [[:map [:x :int]]]}
|
||||||
|
:post {}}
|
||||||
|
(impl/path-update
|
||||||
|
{:parameters {:query [:map [:x :int]]}
|
||||||
|
:get {:parameters {:query [:map [:x :int]]}
|
||||||
|
:responses {200 {:body [:map [:total :int]]}}}
|
||||||
|
:post {}}
|
||||||
|
[[[:parameters any?] vector]
|
||||||
|
[[any? :parameters any?] vector]
|
||||||
|
[[:responses any? :body] vector]
|
||||||
|
[[any? :responses any? :body] vector]]))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue