Skip to content

Instantly share code, notes, and snippets.

@rcanepa
Forked from abp/map-matcher.clj
Created January 3, 2016 21:54
Show Gist options
  • Save rcanepa/960c8c89b5597448c458 to your computer and use it in GitHub Desktop.
Save rcanepa/960c8c89b5597448c458 to your computer and use it in GitHub Desktop.
(require '[schema.core :as s])
(require '[schema.coerce :as coerce])
(require '[schema.utils :as utils])
(defn filter-schema-keys
[m schema-keys extra-keys-walker]
(reduce-kv (fn [m k v]
(if (or (contains? schema-keys k)
(and extra-keys-walker
(not (utils/error? (extra-keys-walker k)))))
m
(dissoc m k)))
m
m))
(defn map-filter-matcher
[s]
(when (or (instance? clojure.lang.PersistentArrayMap s)
(instance? clojure.lang.PersistentHashMap s))
(let [extra-keys-schema (#'s/find-extra-keys-schema s)
extra-keys-walker (when extra-keys-schema (s/walker extra-keys-schema))
explicit-keys (some->> (dissoc s extra-keys-schema)
keys
(mapv s/explicit-schema-key)
(into #{}))]
(when (or extra-keys-walker (seq explicit-keys))
(fn [x]
(if (map? x)
(filter-schema-keys x explicit-keys extra-keys-walker)
x))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment