https://github.com/zookzook/hocon
Parse HOCON configuration files in Elixir following the HOCON specifications.
https://github.com/zookzook/hocon
elixir hocon
Last synced: 4 months ago
JSON representation
Parse HOCON configuration files in Elixir following the HOCON specifications.
- Host: GitHub
- URL: https://github.com/zookzook/hocon
- Owner: zookzook
- License: apache-2.0
- Created: 2019-11-29T14:07:32.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-21T16:45:44.000Z (over 5 years ago)
- Last Synced: 2025-03-21T00:08:24.660Z (11 months ago)
- Topics: elixir, hocon
- Language: Elixir
- Size: 181 KB
- Stars: 12
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# hocon
Parse [HOCON](https://github.com/lightbend/config/blob/master/HOCON.md) configuration files in Elixir following the HOCON specifications.
[](https://travis-ci.org/zookzook/hocon)
[](https://coveralls.io/github/zookzook/hocon?branch=master)
[](https://codebeat.co/projects/github-com-zookzook-hocon-master)
[](https://hex.pm/packages/hocon)
[](https://hex.pm/packages/hocon)
[](https://hex.pm/packages/hocon)
[](https://hex.pm/packages/hocon)
## Basic usage
Assume the file `my-configuration.conf` exists and has the following content:
```hocon
{
home : /path/to/home
timeout : 300
logger {
level = "DEBUG"
}
}
```
Then you can read and parse the HOCON-Configuration file:
```elixir
{:ok, body} = File.read("my-configuration.conf")
result = Hocon.decode(body)
IO.puts inspect result
{:ok, %{"home" => "path/to/home", "logger" => %{"level" => "DEBUG"}, "timeout" => 300}}
```
The HOCON configuration is very powerfull and has a lot of nice features
```hocon
{
// you can use comments
# you can concat arrays like this
dirs += ${PWD}
dirs += /working-folder
# you can concat strings like this
path : ${PWD}
path : ${path}"/working-folder"
# Here are several ways to define `a` to the same array value:
// one array
a : [ 1, 2, 3, 4 ]
// two arrays that are concatenated
a : [ 1, 2 ] [ 3, 4 ]
// with self-referential substitutions
a : [ 1, 2 ]
a : ${a} [ 3, 4 ]
# some nested objects:
foo { bar { baz : 42 } }
# you can build values with substitutions
foo : { a : { c : 1 } }
foo : ${foo.a}
foo : { a : 2 }
}
```
After parsing you get this map as result (where PWD=/Users/micha/projects/elixir/hocon):
```elixir
%{
"dirs" => ["/Users/micha/projects/elixir/hocon", "working-folder"],
"path" => "/Users/micha/projects/elixir/hocon/working-folder"},
"a" => [1, 2, 3, 4],
"foo" => %{"a" => 2, "bar" => %{"baz" => 42}, "c" => 1}
}
```
## Spec Coverage
https://github.com/lightbend/config/blob/master/HOCON.md
- [x] parsing JSON
- [x] comments
- [x] omit root braces
- [x] key-value separator
- [x] commas are optional if newline is present
- [x] whitespace
- [x] duplicate keys and object merging
- [x] unquoted strings
- [x] multi-line strings
- [x] value concatenation
- [x] object concatenation
- [x] array concatenation
- [x] path expressions
- [x] path as keys
- [x] substitutions
- [x] includes
- [x] conversion of numerically-indexed objects to arrays
- [x] allow URL for included files
- [x] duration unit format
- [x] period unit format
- [x] size unit format