Skip to content

Commit

Permalink
Add support for persistent hash map formatting (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmoraunio authored Jun 7, 2024
1 parent 52ac0f0 commit 70056a8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/oksa/unparse.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns oksa.unparse
(:require [clojure.string :as str]
[oksa.alpha.protocol :as protocol])
#?(:clj (:import (clojure.lang Keyword PersistentVector PersistentArrayMap))))
#?(:clj (:import (clojure.lang Keyword PersistentHashMap PersistentVector PersistentArrayMap))))

(defmulti format-value type)

Expand Down Expand Up @@ -71,13 +71,20 @@
:cljs cljs.core/PersistentVector)
[x]
(str "[" (clojure.string/join " " (mapv format-value x)) "]"))
(defmethod format-value #?(:clj PersistentArrayMap
:cljs cljs.core/PersistentArrayMap)
(defn format-map
[x]
(str "{"
(str/join ", " (map (fn [[object-field-name object-field-value]]
(str (name object-field-name) ":" (format-value object-field-value))) x))
"}"))
(defmethod format-value #?(:clj PersistentArrayMap
:cljs cljs.core/PersistentArrayMap)
[x]
(format-map x))
(defmethod format-value #?(:clj PersistentHashMap
:cljs cljs.core/PersistentHashMap)
[x]
(format-map x))

(defn -format-argument
[name value]
Expand Down
14 changes: 14 additions & 0 deletions test/oksa/alpha/api_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@
(api/argument :g {:frob {:foo 1
:bar 2}})
(api/argument :h :$fooVar)))))))
(t/is (= "{foo(args:{e:5, g:7, c:3, h:8, b:2, d:4, f:6, i:9, a:1})}"
(unparse-and-validate (api/select (api/field :foo
(api/opts
(api/arguments :args {:a 1
:b 2
:c 3
:d 4
:e 5
:f 6
:g 7
:h 8
:i 9}))))))
"hash maps")

#?(:clj (t/is (= "{foo(a:0.3333333333333333)}"
(unparse-and-validate (api/select (api/field :foo (api/opts (api/argument :a 1/3)))))
(unparse-and-validate (api/select (api/field :foo (api/opts (api/arguments :a 1/3))))))))
Expand Down
11 changes: 11 additions & 0 deletions test/oksa/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@
:g {:frob {:foo 1
:bar 2}}
:h :$fooVar}}]])))
(t/is (= "{foo(args:{e:5, g:7, c:3, h:8, b:2, d:4, f:6, i:9, a:1})}"
(unparse-and-validate [[:foo {:arguments {:args {:a 1
:b 2
:c 3
:d 4
:e 5
:f 6
:g 7
:h 8
:i 9}}}]]))
"hash maps")
#?(:clj (t/is (= "{foo(a:0.3333333333333333)}"
(unparse-and-validate [[:foo {:arguments {:a 1/3}}]]))))
(t/testing "escaping special characters"
Expand Down

0 comments on commit 70056a8

Please sign in to comment.