Skip to content

Commit

Permalink
[Fix #3] Add parse stream and generate stream (#20)
Browse files Browse the repository at this point in the history
  Following #3,
  we followed the inline implementation of
  https://github.com/metosin/muuntaja/pull/94/files
  to add the suggested feature.

Co-authored-by: David Pham <davidpham87@gmail.com>
  • Loading branch information
markus-wa and davidpham87 authored Mar 25, 2021
1 parent ee4e1cf commit 5371421
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject clj-commons/clj-yaml (or (System/getenv "PROJECT_VERSION") "0.7.2")
(defproject clj-commons/clj-yaml (or (System/getenv "PROJECT_VERSION") "0.7.3")
:description "YAML encoding and decoding for Clojure using SnakeYAML"
:url "https://github.com/clj-commons/clj-yaml"
:license {:name "Eclipse Public License - v 1.0"
Expand Down
11 changes: 11 additions & 0 deletions src/clojure/clj_yaml/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,14 @@
:allow-recursive-keys allow-recursive-keys
:allow-duplicate-keys allow-duplicate-keys)
string) keywords))

;; From https://github.com/metosin/muuntaja/pull/94/files
(defn generate-stream
"Dump the content of data as yaml into writer."
[writer data & opts]
(.dump ^Yaml (apply make-yaml opts) (encode data) writer))

(defn parse-stream
[^java.io.Reader reader & {:keys [keywords] :or {keywords true} :as opts}]
(decode (.load ^Yaml (apply make-yaml (into [] cat opts))
reader) keywords))
37 changes: 36 additions & 1 deletion test/clj_yaml/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
(ns clj-yaml.core-test
(:require [clojure.test :refer (deftest testing is)]
[clojure.string :as string]
[clj-yaml.core :refer [parse-string unmark generate-string]])
[clojure.java.io :as io]
[clj-yaml.core :refer [parse-string unmark generate-string
parse-stream generate-stream]])
(:import [java.util Date]
(java.io ByteArrayOutputStream OutputStreamWriter ByteArrayInputStream)
java.nio.charset.StandardCharsets
(org.yaml.snakeyaml.error YAMLException)
(org.yaml.snakeyaml.constructor DuplicateKeyException)))

Expand Down Expand Up @@ -241,3 +245,34 @@ foo/bar: 42
parse-string
generate-string
parse-string)))))

(defn to-bytes
"Converts a string to a byte array."
[data]
(.getBytes ^String data StandardCharsets/UTF_8))

(defn roundtrip
"Testing roundtrip of string and stream parser, and checking their equivalence."
[data-as-string]
(let [data (parse-string data-as-string)
data-stream (parse-stream (io/reader (ByteArrayInputStream. (to-bytes data-as-string))))
output-stream (ByteArrayOutputStream.)
writer (OutputStreamWriter. output-stream)
_ (generate-stream writer data)
reader (ByteArrayInputStream. (.toByteArray output-stream))]
(= data ;; string -> edn
(parse-string (generate-string data)) ;; edn -> string -> edn
(parse-stream (io/reader reader)) ;; edn -> stream -> edn
;; stream -> edn
data-stream)))

(deftest roundtrip-test
(testing "Roundtrip test"
(is (roundtrip duplicate-keys-yaml))
(is (roundtrip hashes-of-lists-yaml))
(is (roundtrip inline-hash-yaml))
(is (roundtrip inline-list-yaml))
(is (roundtrip list-of-hashes-yaml))
(is (roundtrip list-yaml))
(is (roundtrip nested-hash-yaml))))

0 comments on commit 5371421

Please sign in to comment.