owainlewis / yaml

A fast, idiomatic and easy to use Clojure YAML library. Based on Snake YAML
MIT License
77 stars 24 forks source link

from-file can't handle files that contain multiple documents #6

Closed brighid closed 8 years ago

brighid commented 8 years ago

Because #'from-file uses SnakeYAML's .load method, it can only handle input files that contain exactly one YAML document. Files that contain multiple YAML documents are not tremendously common, but are 100% permitted by the spec and definitely exist in the wild — for example, the way that the Jekyll/Octopress CMS stores pages/posts. When #'from-file encounters a multi-document input file, it throws an error like the following.

ComposerException expected a single document in the stream
 in 'string', line 2, column 1:
    layout: post
    ^
but found another document
 in 'string', line 11, column 1:
    ---
    ^
  org.yaml.snakeyaml.composer.Composer.getSingleNode (Composer.java:110)

As a point of information, when SnakeYAML's .loadAll method is called with multi-document input, it returns a lazy sequence of parsed documents.

owainlewis commented 8 years ago

Thanks for this. I wouldn't have noticed the issue with multiple documents.

Sounds like .loadAll might be the way to go.

owainlewis commented 8 years ago

FIxed. Added a parse-documents method to the reader and also parse-string will return a list of parsed documents if it encounters a string with multiple docs. New version here https://clojars.org/io.forward/yaml. Let me know if you have any issues. Thanks

brighid commented 8 years ago

Awesome; thank you. :)