From 70056a859bb234c753afcb32e1abdc33f4a73c8d Mon Sep 17 00:00:00 2001 From: Ilmo Raunio Date: Fri, 7 Jun 2024 21:11:15 +0300 Subject: [PATCH] Add support for persistent hash map formatting (#16) --- src/oksa/unparse.cljc | 13 ++++++++++--- test/oksa/alpha/api_test.cljc | 14 ++++++++++++++ test/oksa/core_test.cljc | 11 +++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/oksa/unparse.cljc b/src/oksa/unparse.cljc index af3ecd1..0e04484 100644 --- a/src/oksa/unparse.cljc +++ b/src/oksa/unparse.cljc @@ -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) @@ -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] diff --git a/test/oksa/alpha/api_test.cljc b/test/oksa/alpha/api_test.cljc index 12c5c96..f6ce162 100644 --- a/test/oksa/alpha/api_test.cljc +++ b/test/oksa/alpha/api_test.cljc @@ -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)))))))) diff --git a/test/oksa/core_test.cljc b/test/oksa/core_test.cljc index a4e336f..814ef55 100644 --- a/test/oksa/core_test.cljc +++ b/test/oksa/core_test.cljc @@ -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"