Giter Club home page Giter Club logo

yaml-elixir's Introduction



Yaml parser for Elixir

Build Status

How does it work?

This is a wrapper for yamerl - a native Erlang YAML parser which brings all of the functionalities to Elixir language.

Installation

Add yaml_elixir as a dependency in your mix.exs file.

defp deps do
  [
     # ...
    {:yaml_elixir, "~> x.x.x"},
  ]
end

Where x.x.x equals the version in mix.exs. Always make sure to use the latest version.

Once you've done that, run mix deps.get in your command line to fetch the dependency.

Usage

With Yaml Elixir you have an access to two methods, one for parsing a string and an another one for parsing a file.

Run iex -S mix in your terminal to try how their works.

Parsing a string

iex(1)> yaml = """
...(1)>   a: a
...(1)>   b: 1
...(1)>   c: true
...(1)>   d: ~
...(1)>   e: nil
...(1)> """
"  a: a\n  b: 1\n  c: true\n  d: ~\n  e: nil\n"
iex(2)> YamlElixir.read_from_string yaml
%{"a" => "a", "b" => 1, "c" => true, "d" => nil, "e" => "nil"}

Parsing a file

iex(1)> path = File.cwd! |> Path.join("test/fixtures/flat.yml")
"/Users/squixy/Desktop/yaml-elixir/test/fixtures/flat.yml"
iex(2)> YamlElixir.read_from_file path
%{"a" => "a", "b" => 1, "c" => true, "d" => nil, "e" => []}

Support for atoms

By default, all map keys are processed as strings, as are all bareword or quoted values.

If you prefer to autodetect keys and values that begin with : as atoms, this can be accomplished by passing atoms: true as an option to any of the read_* functions.

iex(1)> yaml = """
...(1)>   a: a
...(1)>   b: 1
...(1)>   c: true
...(1)>   d: ~
...(1)>   e: nil,
...(1)>   :f: :atom
...(1)> """
"  a: a\n  b: 1\n  c: true\n  d: ~\n  e: nil\n"
iex(2)> YamlElixir.read_from_string yaml, atoms: true
%{:f => :atom, "a" => "a", "b" => 1, "c" => true, "d" => nil, "e" => "nil"}

Atoms are not garbage collected by BEAM, so be careful with this option, and don't use it with user-supplied input.

If you enable autodetection of atoms, any string values entered (e.g. ":not_really_an_atom") will be converted to atoms, as well. If you only need to support a few atom values, it might be better to enable yamerl's custom tag for atoms:

:yamerl_app.set_param(:node_mods, [:yamerl_node_erlang_atom])

and then using the somewhat inconvenient syntax for it:

atom_key: !<tag:yamerl,2012:atom> atom_value

Elixir Sigil

The YamlElixir.Sigil module provides the ~y sigil that can be useful for example for keeping short configurations or other inlined yaml.

import YamlElixir.Sigil

@config ~y"""
debug: false
port:  9200
files:
  - some/file.csv
  - another/file.csv
"""

Use the a sigil modifier to turn on atom values from yaml:

~y":answer: yes"a

You can find more examples in test directory.

Mix tasks

Sometimes, you may want to use yaml_elixir in your mix tasks. To do that, you must ensure that the application has started.

Application.ensure_all_started(:yaml_elixir)

After that, you will be able to use yaml-elixir in your mix tasks.

Contribution

In case of any problems or suggestions do not hesitate and create a pull request.

Credits

yaml-elixir's People

Contributors

kamillelonek avatar samb1729 avatar ernie avatar hajto avatar vic avatar amorphid avatar andyl avatar sibsibsib avatar fahrradflucht avatar rothsberg avatar kyleaa avatar lowks avatar

Watchers

James Cloos avatar  avatar  avatar Jason Monroe avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.