Skip to content

Instantly share code, notes, and snippets.

@raymcdermott
Last active March 24, 2016 23:08
Show Gist options
  • Save raymcdermott/3a11d76fcd286fd889d6 to your computer and use it in GitHub Desktop.
Save raymcdermott/3a11d76fcd286fd889d6 to your computer and use it in GitHub Desktop.
Obtain diff between source data and golden view
(def api-data {:source-data {:fname "Ray", :last "McDermott", :e-mail "foo@bar.com"},
:golden-view {:first "Ray", :last "McDermott", :email "bar@bar.com", :dob "29/04/2004"},
:mapping {:email :e-mail, :first :fname, :last :last}})
;=> #'user/api-data
(defn source-golden-diff [source-data golden-view source-map]
(let [common-keys (let [keys (clojure.set/intersection (set (keys golden-view))
(set (keys source-data)))]
(into {} (map #(vec [% %]) keys)))
all-keys (merge common-keys source-map)
diffs (let [golden-view-of-source (into {} (map #(assoc {} (first %) (get source-data (last %))) all-keys))]
(into {} (clojure.set/difference (set golden-view-of-source) (set golden-view))))]
(into [] (map (fn [diff-key] (let [src-key (get all-keys diff-key)
new-src-value (get golden-view diff-key)
old-src-value (get source-data src-key)]
{:new [src-key new-src-value]
:old [src-key old-src-value]
:golden [diff-key new-src-value]})) (keys diffs)))))
;=> #'user/source-golden-diff
(source-golden-diff (:source-data api-data) (:golden-view api-data) (:mapping api-data))
;=> {:new [:e-mail "bar@bar.com"], :old [:e-mail "foo@bar.com"], :golden [:email "bar@bar.com"]}
@raymcdermott
Copy link
Author

The mapping is the non-matching elements that map between the source and golden view.
In this case the source system does not hold DOB. The :last field is common.
Small fix to use all-keys in the final form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment