Skip to content

Commit

Permalink
Merge pull request #39 from clanhr/key-transform-utils
Browse files Browse the repository at this point in the history
Add keys transform util fns
  • Loading branch information
donbonifacio committed Jun 2, 2016
2 parents b46774f + 699d0c3 commit 7ccb1ca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject clanhr/postgres-gateway "1.8.2"
(defproject clanhr/postgres-gateway "1.9.0"
:description "ClanHR postgres-gateway"
:url "https://github.com/clanhr/postgres-gateway"

Expand Down
18 changes: 18 additions & 0 deletions src/clanhr/postgres_gateway/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@
(into [] (flatten query-with-sql)))
query))

(defn- change-keys-case
"Change the case of the keys"
[model from-char to-char]
(when model
(reduce-kv (fn [m k v]
(assoc m (keyword (clojure.string/replace (name k) from-char to-char)) v))
{}
model)))

(defn ->snake-case-keys
"Transforms lisp case keys with - in database keys with _"
[model]
(change-keys-case model "-" "_"))

(defn ->lisp-case-keys
"Transforms database keys with _ in clojure keys with -"
[model]
(change-keys-case model "_" "-"))
6 changes: 6 additions & 0 deletions test/clanhr/postgres_gateway/utils_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@
(deftest add-in-logic
(is (= (utils/add-in-logic ["name in ($1)" ["a" "b"]] {:in-param 1})
["name in ($1,$2)" "a" "b"])))

(deftest snake-case-keys
(is (= {:some_key 1} (utils/->snake-case-keys {:some-key 1}))))

(deftest lisp-case-keys
(is (= {:some-key 1} (utils/->lisp-case-keys {:some_key 1}))))

0 comments on commit 7ccb1ca

Please sign in to comment.