Skip to content

Commit

Permalink
made hash keys keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
lancepantz committed Feb 20, 2010
1 parent 5ea9e26 commit 19284f5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 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-yaml "0.1.0-SNAPSHOT"
(defproject clj-yaml "0.2.0-SNAPSHOT"
:description "YAML encoding(eventually) and decoding for Clojure using SnakeYAML"
:url "http://github.com/lancepantz/clj-yaml"
:source-path "src"
Expand Down
4 changes: 2 additions & 2 deletions src/clj_yaml.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(ns clj-yaml
(:import (org.yaml.snakeyaml Yaml))
(:use (clojure.contrib [def :only (defvar-)])))
(:use (clojure.contrib [def :only (defvar-)])))

(defvar- yaml (Yaml.))

(defmulti to-seq class)

(defmethod to-seq java.util.LinkedHashMap [data]
(into {} (for [[k v] data]
[k (to-seq v)])))
[(keyword k) (to-seq v)])))

(defmethod to-seq java.util.ArrayList [data]
(map #(to-seq %) data))
Expand Down
36 changes: 18 additions & 18 deletions test/clj_yaml_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ women:

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

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

(deftest parse-list
(let [parsed (clj-yaml/parse-string list-yaml)]
Expand All @@ -57,31 +57,31 @@ women:

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

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

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

(deftest parse-list-of-hashes
(let [parsed (clj-yaml/parse-string 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")))))
(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 (clj-yaml/parse-string 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"))))))
(is (= "John Smith" (first (parsed :men))))
(is (= "Bill Jones" (last (parsed :men))))
(is (= "Mary Smith" (first (parsed :women))))
(is (= "Susan Williams" (last (parsed :women))))))

0 comments on commit 19284f5

Please sign in to comment.