Skip to content

Instantly share code, notes, and snippets.

@ir4y
Created June 4, 2013 16:57
Show Gist options
  • Save ir4y/5707603 to your computer and use it in GitHub Desktop.
Save ir4y/5707603 to your computer and use it in GitHub Desktop.
(ns schema
(:use clojure.test))
(defn apply-schema [schema data]
(if (vector? data)
(vec (map #(apply-schema (first schema) %) data))
(reduce merge (map (fn [item] (if (keyword? item)
{item (data item)}
(let [[[k v]] (vec item)]
{k (apply-schema v (data k))})))
schema))))
(with-test
(def test-data {
:aaa "22"
:aaaa "New 22"
:bbb "434"
:ccc {:ddd "abc" :ddddd "Not needed" :ggg "Needed" :hard_to_believe []}
:zzz [ {:hhh 126 :hhhh "Don't need" :kkk "Existing key"} {:hhh "DoobyDo" :kkk "Needed" :mmm "Existing key"} ]})
(def test-schema [
:aaa
:bbb
:ooo
{:ccc [:ddd :ggg]}
{:zzz [[:hhh :kkk :mmm]]}])
(is (= (apply-schema test-schema test-data)
{ :zzz [{:mmm nil, :kkk "Existing key", :hhh 126} {:mmm "Existing key", :kkk "Needed", :hhh "DoobyDo"}]
:ccc {:ggg "Needed", :ddd "abc"}
:ooo nil
:bbb "434"
:aaa "22"}))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment