From 8efd8c0a44a3232e0649570bdfbf06c97b050ad1 Mon Sep 17 00:00:00 2001 From: Nikolaos Vlassopoulos Date: Sun, 5 Feb 2023 18:29:00 +0200 Subject: [PATCH 1/2] Add pr-to-writer in edn.clj, Add test for lazy sequences --- modules/muuntaja/src/muuntaja/format/edn.clj | 18 ++++++++++++++++-- .../ring_middleware/format_response_test.clj | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/modules/muuntaja/src/muuntaja/format/edn.clj b/modules/muuntaja/src/muuntaja/format/edn.clj index c063480..8c57520 100644 --- a/modules/muuntaja/src/muuntaja/format/edn.clj +++ b/modules/muuntaja/src/muuntaja/format/edn.clj @@ -11,13 +11,27 @@ (decode [_ data charset] (edn/read options (PushbackReader. (InputStreamReader. ^InputStream data ^String charset))))))) +(defn- pr-to-writer + ([w] w) + ([w x] + (print-method x w) + w) + ([w x & more] + (print-method x w) + (.append w \space) + (if-let [nmore (next more)] + (recur w (first more) nmore) + (apply pr-to-writer more)))) + (defn encoder [_] (reify core/EncodeToBytes (encode-to-bytes [_ data charset] (.getBytes - (pr-str data) - ^String charset)) + (let [w (new java.io.StringWriter)] + (pr-to-writer w data) + (.toString w)) + ^String charset)) core/EncodeToOutputStream (encode-to-output-stream [_ data charset] (fn [^OutputStream output-stream] diff --git a/test/muuntaja/ring_middleware/format_response_test.clj b/test/muuntaja/ring_middleware/format_response_test.clj index 4b2574b..343af1b 100644 --- a/test/muuntaja/ring_middleware/format_response_test.clj +++ b/test/muuntaja/ring_middleware/format_response_test.clj @@ -133,6 +133,25 @@ ;; we do not set the "Content-Length" #_(is (< 2 (Integer/parseInt (get-in resp [:headers "Content-Length"])))))) +(defn- produce-element-with-log [n] + (prn "reading from db" n) + {:element n}) + +(defn- dummy-handler-with-lazy-seq [_] + {:status 200 + :body (map produce-element-with-log (range 4))}) + +(deftest lazy-sequences-with-logs + (let [handler (wrap-api-response + dummy-handler-with-lazy-seq + (-> m/default-options + (m/select-formats ["application/edn"]))) + resp (handler {}) + body (slurp (:body resp))] + ;; Lazy sequence realization interferes with `with-out-str` + #_(is (= body "(\"reading from db\" 0\n\"reading from db\" 1\n\"reading from db\" 2\n\"reading from db\" 3\n{:element 0} {:element 1} {:element 2} {:element 3})")) + (is (= body "({:element 0} {:element 1} {:element 2} {:element 3})")))) + (def yaml-echo (wrap-api-response identity From 4027da7956c9cbb5e2918e9c28cfd30537c3a9a5 Mon Sep 17 00:00:00 2001 From: Nikolaos Vlassopoulos Date: Thu, 9 Jan 2025 08:54:01 +0200 Subject: [PATCH 2/2] replace pr-to-writer with print-method --- modules/muuntaja/src/muuntaja/format/edn.clj | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/modules/muuntaja/src/muuntaja/format/edn.clj b/modules/muuntaja/src/muuntaja/format/edn.clj index 8c57520..eb4a93b 100644 --- a/modules/muuntaja/src/muuntaja/format/edn.clj +++ b/modules/muuntaja/src/muuntaja/format/edn.clj @@ -11,25 +11,13 @@ (decode [_ data charset] (edn/read options (PushbackReader. (InputStreamReader. ^InputStream data ^String charset))))))) -(defn- pr-to-writer - ([w] w) - ([w x] - (print-method x w) - w) - ([w x & more] - (print-method x w) - (.append w \space) - (if-let [nmore (next more)] - (recur w (first more) nmore) - (apply pr-to-writer more)))) - (defn encoder [_] (reify core/EncodeToBytes (encode-to-bytes [_ data charset] (.getBytes (let [w (new java.io.StringWriter)] - (pr-to-writer w data) + (print-method data w) (.toString w)) ^String charset)) core/EncodeToOutputStream