Created
May 1, 2014 17:09
-
-
Save holyjak/74b2c6ea20b25d020c8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[cheshire.core :refer [parse-string]]) | |
(defn- json->data [key m] | |
(update-in m [key] #(parse-string % true))) | |
(defn- select-campaign [car+campaigns] | |
(first car+campaigns)) | |
(defn- jdbc-array-to-set | |
[key m] | |
(update-in m [key] #(apply hash-set (.getArray %)))) | |
(defn refine-cars-simple | |
"Process get-cars query result set - derive additional data, transform values into better ones | |
There is one row per car and campaign, a car may have more campaigns - we pick the best one. | |
" | |
[cars-raw] | |
(->> | |
cars-raw | |
(map (partial json->data :json)) ;; <- this I originally forgot | |
;; group rows for the same car => [[car1 ..][car2 ..]] | |
(group-by :id) | |
(vals) | |
;; join all car1 vectors into one car .. | |
(map select-campaign) | |
(map (fn [car] | |
(->> | |
car | |
(jdbc-array-to-set :category_ref) | |
(jdbc-array-to-set :keywords)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[cheshire.core :refer [parse-string]]) | |
(require '[clojure.set :refer [subset? difference]]) | |
(defn- car? [{:keys [id] :as car}] | |
(and (map? car) id)) | |
(defn- json->data [key m] | |
{:pre [(contains? m key) (string? (get m key))], :post [(map? (get % key))]} | |
(update-in m [key] parse-string true)) | |
(defn- select-campaign [[first-car :as all]] | |
{:pre [(sequential? all) (car? first-car)], :post [(car? %)]} | |
first-car) | |
(defn- jdbc-array-to-set | |
[key m] | |
{:pre [(contains? m key) (instance? java.sql.Array (get m key))], :post [(set? (get % key))]} | |
(update-in m [key] #(apply hash-set (.getArray %)))) | |
(defn group-rows-by-car [cars-raw] | |
{:pre [(sequential? cars-raw) (car? (first cars-raw))] | |
:post [(sequential? %) (vector? (first %))]} | |
(vals (group-by :id cars-raw))) | |
(defn refine-car [car] | |
{:pre [(car? car) (:keywords car) (:category_ref car)]} | |
(->> car | |
(jdbc-array-to-set :category_ref) | |
(jdbc-array-to-set :keywords))) | |
(defn refine-cars-simple | |
"Process get-cars query result set - derive additional data, transform values into better ones | |
There is one row per car and campaign, a car may have more campaigns - we pick the best one. | |
" | |
[cars-raw] | |
(->> | |
cars-raw | |
(map (partial json->data :json)) ;; <- this I originally forgot | |
(group-rows-by-car) | |
(map select-campaign) | |
(map refine-car))) | |
(defn empty-array [] (reify java.sql.Array (getArray [_] (object-array [])))) | |
(refine-cars-simple [{:id 1, :json "{\"discount\":5000}", :campaign_discount 3000, :category_ref (empty-array), :keywords (empty-array)}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simplified example for http://theholyjava.wordpress.com/2014/04/30/clojure-how-to-prevent-expected-map-got-vector-and-similar-errors/