yamldom.cr
YAML DOM
IMPORTANT!!! This project has been shit-canned because Crystal can't polymorph enumeration over Array and Hash. That makes this kind of API, while already difficult (because Crystal is a strongly typed language), essentially impossible. Sorry kids, you are stuck with pull parsing.
Intermediate Representation
The YAML DOM library provides a YAML composer that the models YAML Intermediate Representation. This model preserves information lost when using the current Crystal "parser". In particular tag
information is preserved and accessible.
Installation
Add this to your application's shard.yml
:
dependencies:
yamldom:
github: trans/yamldom.cr
Usage
First, lets see how we can get the intermediate representation of a YAML document.
require "yamldom"
yaml = <<-YAML
--- !foo
- EXAMPLE
- 100
YAML
doc = YAML.compose(yaml)
doc.tag #=> "!foo"
doc.value #=> ["EXAMPLE", "100"]
doc.class #=> YAML::Sequence
There are a few things to notice here. First we were able to get the tag. Second, the class of object compose retuirns a YAML::Node
and there are three types of such nodes, YAML::Scalar
, YAML::Sequence
and YAML::Mapping
. If we look at the elements inside the sequence we will see it is made up of other nodes.
doc.value.node(0).to_s #=> "EXAMPLE"
doc.value.node(0).class #=> YAML::Scalar
We use #node
to access the node itself. For convenience the usual #[]
method accesses the underlying value.
doc.value[0] #=> "EXAMPLE"
doc.value[0].class #=> String
This makes it fairly easy to work with the DOM API.
Lastly note that the value 100 in the document is still a String.
doc.value[0] #=> "100"
It isn't an Integer because composition is a stage before construction in which the node would be converted to a native data type (and extra information like tag lost).
Development
If you'd like to help this project improve, familiarize yourself with the YAML sepcification and have at it.
Contributing
- Fork it ( https://github.com/trans/yamldom/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- trans trans - creator, maintainer
yamldom.cr
- 2
- 3
- 0
- 0
- 0
- almost 8 years ago
- November 4, 2016
MIT License
Thu, 21 Nov 2024 21:16:37 GMT