Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/load all #22

Merged
merged 7 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/clojure/clj_yaml/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
(org.yaml.snakeyaml.representer Representer)
(org.yaml.snakeyaml.error Mark)
(clj_yaml MarkedConstructor)
(java.util LinkedHashMap)))
(java.util LinkedHashMap)
(java.io StringReader)))

(def flow-styles
{:auto DumperOptions$FlowStyle/AUTO
Expand Down Expand Up @@ -153,14 +154,20 @@
(.dump ^Yaml (apply make-yaml opts)
(encode data)))

(defn- load-stream [^Yaml yaml ^java.io.Reader input load-all? keywords]
(if load-all?
(map #(decode % keywords) (.loadAll yaml input))
(decode (.load yaml input) keywords)))

(defn parse-string
[^String string & {:keys [unsafe mark keywords max-aliases-for-collections allow-recursive-keys allow-duplicate-keys] :or {keywords true}}]
(decode (.load (make-yaml :unsafe unsafe
:mark mark
:max-aliases-for-collections max-aliases-for-collections
:allow-recursive-keys allow-recursive-keys
:allow-duplicate-keys allow-duplicate-keys)
string) keywords))
[^String string & {:keys [unsafe mark keywords max-aliases-for-collections allow-recursive-keys allow-duplicate-keys load-all?] :or {keywords true}}]
(load-stream (make-yaml :unsafe unsafe
:mark mark
:max-aliases-for-collections max-aliases-for-collections
:allow-recursive-keys allow-recursive-keys
:allow-duplicate-keys allow-duplicate-keys)
(StringReader. string)
load-all? keywords))

;; From https://github.com/metosin/muuntaja/pull/94/files
(defn generate-stream
Expand All @@ -169,6 +176,7 @@
(.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))
[^java.io.Reader reader & {:keys [keywords load-all?] :or {keywords true} :as opts}]
(load-stream (apply make-yaml (into [] cat opts))
reader
load-all? keywords))
45 changes: 42 additions & 3 deletions test/clj_yaml/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
[clojure.string :as string]
[clojure.java.io :as io]
[clj-yaml.core :refer [parse-string unmark generate-string
parse-stream generate-stream]])
parse-stream generate-stream]]
[flatland.ordered.map :refer [ordered-map]])
(:import [java.util Date]
(java.io ByteArrayOutputStream OutputStreamWriter ByteArrayInputStream)
java.nio.charset.StandardCharsets
(org.yaml.snakeyaml.error YAMLException)
(org.yaml.snakeyaml.constructor DuplicateKeyException)))
(org.yaml.snakeyaml.constructor DuplicateKeyException)
[org.yaml.snakeyaml.composer ComposerException]))

(def nested-hash-yaml
"root:\n childa: a\n childb: \n grandchild: \n greatgrandchild: bar\n")
Expand Down Expand Up @@ -276,6 +278,43 @@ foo/bar: 42
(is (roundtrip list-yaml))
(is (roundtrip nested-hash-yaml))))

(defn- ->stream [string]
(io/reader (.getBytes ^String string)))

(def multi-doc-yaml "
---
foo: true
---
bar: false")

(def single-doc-yaml "
---
lol: yolo")

(deftest load-all-test
(testing "Without load-all?"
(is (= (ordered-map {:lol "yolo"})
(parse-string single-doc-yaml)))
(is (= (ordered-map {:lol "yolo"})
(parse-stream (->stream single-doc-yaml))))
(is (thrown-with-msg? ComposerException #"expected a single document in the stream\n"
(parse-stream (->stream multi-doc-yaml))))
(is (thrown-with-msg? ComposerException #"expected a single document in the stream\n"
(parse-string multi-doc-yaml))))

(testing "With load-all?=true on single docs"
(is (= [(ordered-map {:lol "yolo"})]
(parse-string single-doc-yaml :load-all? true)))
(is (= [(ordered-map {:lol "yolo"})]
(parse-stream (->stream single-doc-yaml) :load-all? true))))

(testing "With load-all?=true on multi docs"
(is (= [(ordered-map {:foo true}) (ordered-map {:bar false})]
(parse-string multi-doc-yaml :load-all? true)))
(is (= [(ordered-map {:foo true}) (ordered-map {:bar false})]
(parse-stream (->stream multi-doc-yaml) :load-all? true))))
)

(def indented-yaml "todo:
- name: Fix issue
responsible:
Expand All @@ -290,4 +329,4 @@ foo/bar: 42
(generate-string (parse-string indented-yaml)
:dumper-options {:indent 5
:indicator-indent 2
:flow-style :block})))))
:flow-style :block})))))