Skip to content

Bump SnakeYAML and Expose Parse Options #13

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

Merged
merged 4 commits into from
Aug 31, 2020
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
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
:java-source-paths ["src/java"]
:javac-options ["-target" "1.7" "-source" "1.7" "-Xlint:-options"]
:dependencies
[[org.yaml/snakeyaml "1.25"]
[[org.yaml/snakeyaml "1.26"]
[org.flatland/ordered "1.5.9"]]
:profiles {:provided {:dependencies [[org.clojure/clojure "1.10.1"]]}})
40 changes: 32 additions & 8 deletions src/clojure/clj_yaml/core.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns clj-yaml.core
(:require [flatland.ordered.map :refer (ordered-map)]
[flatland.ordered.set :refer (ordered-set)])
(:import (org.yaml.snakeyaml Yaml DumperOptions DumperOptions$FlowStyle)
(:import (org.yaml.snakeyaml Yaml DumperOptions DumperOptions$FlowStyle LoaderOptions)
(org.yaml.snakeyaml.constructor Constructor SafeConstructor BaseConstructor)
(org.yaml.snakeyaml.representer Representer)
(org.yaml.snakeyaml.error Mark)
Expand All @@ -27,17 +27,37 @@
(doto (default-dumper-options)
(.setDefaultFlowStyle (flow-styles flow-style))))

(defn ^LoaderOptions default-loader-options
[]
(LoaderOptions.))

(defn ^LoaderOptions make-loader-options
[& {:keys [max-aliases-for-collections allow-recursive-keys]}]
(let [loader (default-loader-options)]
(when max-aliases-for-collections
(.setMaxAliasesForCollections loader max-aliases-for-collections))
(when allow-recursive-keys
(.setAllowRecursiveKeys loader allow-recursive-keys))
loader))

(defn ^Yaml make-yaml
"Make a yaml encoder/decoder with some given options."
[& {:keys [dumper-options unsafe mark]}]
(let [^BaseConstructor constructor
(if unsafe (Constructor.)
(if mark (MarkedConstructor.) (SafeConstructor.)))
[& {:keys [dumper-options unsafe mark max-aliases-for-collections allow-recursive-keys]}]
(let [loader (make-loader-options :max-aliases-for-collections max-aliases-for-collections
:allow-recursive-keys allow-recursive-keys)
^BaseConstructor constructor
(if unsafe (Constructor. loader)
(if mark
;; construct2ndStep isn't implemented by MarkedConstructor,
;; causing an exception to be thrown before loader options are
;; used
(MarkedConstructor.)
(SafeConstructor. loader)))
;; TODO: unsafe marked constructor
dumper (if dumper-options
(make-dumper-options :flow-style (:flow-style dumper-options))
(default-dumper-options))]
(Yaml. constructor (Representer.) dumper)))
(Yaml. constructor (Representer.) dumper loader)))

(defrecord Marked
[start end unmark])
Expand Down Expand Up @@ -126,5 +146,9 @@
(encode data)))

(defn parse-string
[^String string & {:keys [unsafe mark keywords] :or {keywords true}}]
(decode (.load (make-yaml :unsafe unsafe :mark mark) string) keywords))
[^String string & {:keys [unsafe mark keywords max-aliases-for-collections allow-recursive-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)
string) keywords))
23 changes: 22 additions & 1 deletion test/clj_yaml/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require [clojure.test :refer (deftest testing is)]
[clojure.string :as string]
[clj-yaml.core :refer [parse-string unmark generate-string]])
(:import [java.util Date]))
(:import [java.util Date]
(org.yaml.snakeyaml.error YAMLException)))

(def nested-hash-yaml
"root:\n childa: a\n childb: \n grandchild: \n greatgrandchild: bar\n")
Expand Down Expand Up @@ -199,3 +200,23 @@ the-bin: !!binary 0101")
(testing "emoji in comments are OK too"
(let [yaml "# 💣 emoji in a comment\n42"]
(is (= 42 (parse-string yaml))))))

(def too-many-aliases
(->> (range 51)
(map #(str "b" % ": *a"))
(cons "a: &a [\"a\",\"a\"]")
(string/join "\n")))

(deftest max-aliases-for-collections-works
(is (thrown-with-msg? YAMLException #"Number of aliases" (parse-string too-many-aliases)))
(is (parse-string too-many-aliases :max-aliases-for-collections 51)))

(def recursive-yaml "
---
&A
- *A: *A
")

(deftest allow-recursive-works
(is (thrown-with-msg? YAMLException #"Recursive" (parse-string recursive-yaml)))
(is (parse-string recursive-yaml :allow-recursive-keys true)))