Skip to content

Commit

Permalink
Adding col mapping possibility for transform
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsbarbosa committed Jan 9, 2023
1 parent 0d05d53 commit fe0252d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
9 changes: 5 additions & 4 deletions doc/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
- k : key from entry, not used
- v : value from entry to be converted
- returns value converted if `uuid?`
- <a id='transform-keys'></a> **transform** [placeholder-map m]
- <a id='transform'></a> **transform** [placeholder-map m]
- Recursively transforms map `m` following patterns defined at `placeholder-map`.
```clojure
(transform {:a :a-name
:b "Fixed value"} {:a-name "Aay"})
; => {:a "Aay", :b "Fixed value"}
(transform {:a :a-name
:b "Fixed value"
:c-name [:c :name]} {:a-name "Aay" :c {:name "C"}})
; => {:a "Aay", :b "Fixed value", :c-name "C"}

(transform {:a {:name :a-name
:surname (fn [m k] "the Letter")}
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject org.clojars.majorcluster/clj-data-adapter "0.4.1"
(defproject org.clojars.majorcluster/clj-data-adapter "0.5.1"
:description "A Clojure data adapter library to convert your data between your layers"
:url "https://github.com/mtsbarbosa/clj-data-adapter"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
Expand Down
5 changes: 3 additions & 2 deletions src/clj/clj_data_adapter/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@
(defn transform
"Recursively transform map m using placeholder-map as a source
for extracting values and building it
ex. (transform {:a {:name :a-name} :b \"Fixed value\"} {:a-name \"Aaay\"})
=> {:a {:name \"Aaay\"} :b \"Fixed value\"}"
ex. (transform {:a {:name :a-name} :b \"Fixed value\" :c-name [:c :name]} {:a-name \"Aaay\" :c {:name \"C\"}})
=> {:a {:name \"Aaay\"} :b \"Fixed value\" :c-name \"C\"}"
([acc-m placeholder-map m]
(reduce (fn [acc-m [k v]]
(cond (keyword? v) (assoc acc-m k (get m v v))
(map? v) (assoc acc-m k (transform {} v m))
(coll? v) (assoc acc-m k (get-in m v v))
(fn? v) (assoc acc-m k (v m k))
:else (assoc acc-m k v)))
acc-m placeholder-map))
Expand Down
25 changes: 24 additions & 1 deletion test/clj_data_adapter/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,27 @@
:fixed-val 10}}
{:new-name "Padoca"
:new-street "Geologicka"
:old-name "Pekarstvi"})))))
:old-name "Pekarstvi"}))))
(testing "deep transform with keywords, fixed value and cols"
(is (= {:bakery {:new {:name "Padoca"
:main-baker "Maria"
:other-bakers ["Joao","Martin","Chico"]
:first-other-baker "Joao"
:second-other-baker "Martin"
:last-other-baker "Chico"
:fixed-col [1 2]}
:old {:name "Pekarstvi"}
:fixed-val 10}}
(transform {:bakery {:new {:name :new-name
:main-baker [:bakers :main]
:other-bakers [:bakers :others]
:first-other-baker [:bakers :others 0]
:second-other-baker [:bakers :others 1]
:last-other-baker [:bakers :others 2]
:fixed-col [1 2]}
:old {:name :old-name}
:fixed-val 10}}
{:new-name "Padoca"
:new-street "Geologicka"
:old-name "Pekarstvi"
:bakers {:main "Maria" :others ["Joao","Martin","Chico"]}})))))

0 comments on commit fe0252d

Please sign in to comment.