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

Fixed two issues, #2 and #9 #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 10 additions & 9 deletions src/clj_yaml/core.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns clj-yaml.core
(:refer-clojure :exclude [load])
(:import (org.yaml.snakeyaml Yaml DumperOptions DumperOptions$FlowStyle)))

(def ^{:dynamic true} *keywordize* true)
Expand All @@ -16,9 +17,9 @@
(defn make-yaml
[& {:keys [dumper-options]}]
(if dumper-options
(Yaml. (apply make-dumper-options
(mapcat (juxt key val)
dumper-options)))
(Yaml. ^DumperOptions (apply make-dumper-options
(mapcat (juxt key val)
dumper-options)))
(Yaml.)))

(defprotocol YAMLCodec
Expand Down Expand Up @@ -66,13 +67,13 @@
(encode [data] data)
(decode [data] data))

(defn generate-string [data & opts]
(.dump (apply make-yaml opts)
(encode data)))
(defn dump [data & opts]
(.dump ^Yaml (apply make-yaml opts)
^Object (encode data)))

(defn parse-string
(defn load
([string keywordize]
(binding [*keywordize* keywordize]
(parse-string string)))
(load string)))
([string]
(decode (.load (make-yaml) string))))
(decode (.load ^Yaml (make-yaml) ^String string))))
32 changes: 16 additions & 16 deletions test/clj_yaml/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns clj-yaml.core-test
(:use clojure.test)
(:use clj-yaml.core))
(:refer-clojure :exclude [load])
(:use clojure.test clj-yaml.core))

(def nested-hash-yaml
"root:\n childa: a\n childb: \n grandchild: \n greatgrandchild: bar\n")
Expand Down Expand Up @@ -55,67 +55,67 @@ the-bin: !!binary 0101")
? Ken Griff")

(deftest parse-hash
(let [parsed (parse-string "foo: bar")]
(let [parsed (load "foo: bar")]
(is (= "bar" (parsed :foo)))))

(deftest parse-nested-hash
(let [parsed (parse-string nested-hash-yaml)]
(let [parsed (load nested-hash-yaml)]
(is (= "a" ((parsed :root) :childa)))
(is (= "bar" ((((parsed :root) :childb) :grandchild) :greatgrandchild)))))

(deftest parse-list
(let [parsed (parse-string list-yaml)]
(let [parsed (load list-yaml)]
(is (= "Casablanca" (first parsed)))
(is (= "North by Northwest" (nth parsed 1)))
(is (= "The Man Who Wasn't There" (nth parsed 2)))))

(deftest parse-nested-hash-and-list
(let [parsed (parse-string hashes-lists-yaml)]
(let [parsed (load hashes-lists-yaml)]
(is (= "A4786" ((first (parsed :items)) :part_no)))
(is (= "Dorthy" (first ((nth (parsed :items) 1) :owners))))))

(deftest parse-inline-list
(let [parsed (parse-string inline-list-yaml)]
(let [parsed (load inline-list-yaml)]
(is (= "milk" (first parsed)))
(is (= "pumpkin pie" (nth parsed 1)))
(is (= "eggs" (nth parsed 2)))
(is (= "juice" (last parsed)))))

(deftest parse-inline-hash
(let [parsed (parse-string inline-hash-yaml)]
(let [parsed (load inline-hash-yaml)]
(is (= "John Smith" (parsed :name)))
(is (= 33 (parsed :age)))))

(deftest parse-list-of-hashes
(let [parsed (parse-string list-of-hashes-yaml)]
(let [parsed (load list-of-hashes-yaml)]
(is (= "John Smith" ((first parsed) :name)))
(is (= 33 ((first parsed) :age)))
(is (= "Mary Smith" ((nth parsed 1) :name)))
(is (= 27 ((nth parsed 1) :age)))))

(deftest hashes-of-lists
(let [parsed (parse-string hashes-of-lists-yaml)]
(let [parsed (load hashes-of-lists-yaml)]
(is (= "John Smith" (first (parsed :men))))
(is (= "Bill Jones" (last (parsed :men))))
(is (= "Mary Smith" (first (parsed :women))))
(is (= "Susan Williams" (last (parsed :women))))))

(deftest h-set
(is (= #{"Mark McGwire" "Ken Griff" "Sammy Sosa"}
(parse-string set-yaml))))
(load set-yaml))))

(deftest typed-data
(let [parsed (parse-string typed-data-yaml)]
(let [parsed (load typed-data-yaml)]
(is (= (Class/forName "[B") (type (:the-bin parsed))))))

(deftest keywordized
(binding [*keywordize* false]
(is (= "items" (-> hashes-lists-yaml parse-string ffirst))))
(is (= "items" (-> hashes-lists-yaml (parse-string false) ffirst))))
(is (= "items" (-> hashes-lists-yaml load ffirst))))
(is (= "items" (-> hashes-lists-yaml (load false) ffirst))))

(deftest dump-opts
(let [data [{:age 33 :name "jon"} {:age 44 :name "boo"}]]
(is (= "- age: 33\n name: jon\n- age: 44\n name: boo\n"
(generate-string data :dumper-options {:flow-style :block})))
(dump data :dumper-options {:flow-style :block})))
(is (= "[{age: 33, name: jon}, {age: 44, name: boo}]\n"
(generate-string data :dumper-options {:flow-style :flow})))))
(dump data :dumper-options {:flow-style :flow})))))