mirror of
https://github.com/metosin/reitit.git
synced 2025-12-16 16:01:11 +00:00
Add faster keywordize-keys implementation for clj
This commit is contained in:
parent
5589328a3c
commit
7ab6021630
2 changed files with 53 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
|||
(ns reitit.coercion
|
||||
(:require [clojure.walk :as walk]
|
||||
(:require [#?(:clj reitit.walk :cljs clojure.walk) :as walk]
|
||||
[reitit.impl :as impl])
|
||||
#?(:clj
|
||||
(:import (java.io Writer))))
|
||||
|
|
|
|||
52
modules/reitit-core/src/reitit/walk.clj
Normal file
52
modules/reitit-core/src/reitit/walk.clj
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
(ns ^:no-doc reitit.walk)
|
||||
|
||||
(defprotocol Walkable
|
||||
(-walk [coll f]))
|
||||
|
||||
(extend-protocol Walkable
|
||||
nil
|
||||
(-walk [_ _] nil)
|
||||
Object
|
||||
(-walk [x _] x)
|
||||
clojure.lang.IMapEntry
|
||||
(-walk [e f] (clojure.lang.MapEntry. (f (.key e)) (f (.val e))))
|
||||
clojure.lang.ISeq
|
||||
(-walk [coll f] (map f coll))
|
||||
clojure.lang.PersistentList
|
||||
(-walk [coll f] (apply list (map f coll)))
|
||||
clojure.lang.PersistentList$EmptyList
|
||||
(-walk [x _] x)
|
||||
clojure.lang.IRecord
|
||||
(-walk [r f] (reduce (fn [r x] (conj r (f x))) r r)))
|
||||
|
||||
(defn- -walk-default
|
||||
[coll f]
|
||||
(into (empty coll) (map f) coll))
|
||||
|
||||
(doseq [type [clojure.lang.PersistentArrayMap
|
||||
clojure.lang.PersistentHashMap
|
||||
clojure.lang.PersistentHashSet
|
||||
clojure.lang.PersistentVector
|
||||
clojure.lang.PersistentQueue
|
||||
clojure.lang.PersistentStructMap
|
||||
clojure.lang.PersistentTreeMap
|
||||
clojure.lang.PersistentTreeSet]]
|
||||
(extend type Walkable {:-walk -walk-default}))
|
||||
|
||||
(defn walk
|
||||
[inner outer form]
|
||||
(outer (-walk form inner)))
|
||||
|
||||
(defn postwalk [f form] (walk (partial postwalk f) f form))
|
||||
|
||||
(defn- keywordize
|
||||
[m]
|
||||
(persistent!
|
||||
(reduce-kv
|
||||
(fn [m k v] (if (string? k) (assoc! m (keyword k) v) m))
|
||||
(transient {})
|
||||
m)))
|
||||
|
||||
(defn keywordize-keys
|
||||
[m]
|
||||
(postwalk (fn [x] (if (map? x) (keywordize m) x)) m))
|
||||
Loading…
Reference in a new issue