hocon v0.1.3 Hocon View Source
This module paresed and decodes a hocon configuration string.
Example
iex(1)> conf = ~s(animal { favorite : "dog" }, key : """${animal.favorite} is my favorite animal""")
"animal { favorite : \"dog\" }, key : \"\"\"${animal.favorite} is my favorite animal\"\"\""
iex(2)> Hocon.decode(conf)
{:ok,
%{"animal" => %{"favorite" => "dog"}, "key" => "dog is my favorite animal"}}
Units format
The Parser returns a map, because in Elixir it is a common use case to use pattern matching on maps to
extract specific values and keys. Therefore the Hocon.decode/2
function returns a map. To support
interpreting a value with some family of units, you can call some conversion functions like as_bytes/1
.
Example
iex> conf = ~s(limit : "512KB")
iex> {:ok, %{"limit" => limit}} = Hocon.decode(conf)
iex> Hocon.as_bytes(limit)
524288
Link to this section Summary
Functions
Returns the size of the string
by using the power of 2.
Returns the size of the string
by using the power of 10.
Parses and decodes a hocon string and returns a map
Similar to decode/2
except it will unwrap the error tuple and raise
in case of errors.
Returns a value for the keypath
from a map or a successfull parse HOCON string.
Same a get/3
but the value is interpreted like a number by using the power of 2.
Same a get/3
but the value is interpreted like a number by using the power of 10.
Link to this section Functions
Returns the size of the string
by using the power of 2.
Example
iex> Hocon.as_bytes("512kb")
524288
iex> Hocon.as_bytes("125 gigabytes")
134217728000
Returns the size of the string
by using the power of 10.
Example
iex> Hocon.as_size("512kb")
512000
iex> Hocon.as_size("125 gigabytes")
125000000000
Parses and decodes a hocon string and returns a map
options
:convert_numerically_indexed
- if set to true then numerically-indexed objects are converted to arrays:strict_conversion
- if set totrue
then numerically-indexed objects are only converted to arrays if all keys are numbers
Example
iex> conf = ~s(animal { favorite : "dog" }, key : """${animal.favorite} is my favorite animal""")
"animal { favorite : \"dog\" }, key : \"\"\"${animal.favorite} is my favorite animal\"\"\""
iex> Hocon.decode(conf)
{:ok,
%{"animal" => %{"favorite" => "dog"}, "key" => "dog is my favorite animal"}}
Use can use the HOCON-Parser as a Config.Provider
to load configuration during boot:
defmodule HOCONConfigProvider do
@behaviour Config.Provider
require Logger
# Let's pass the path to the HOCON file as config
def init(path) when is_binary(path), do: path
def load(config, path) do
# We need to start any app we may depend on.
{:ok, _} = Application.ensure_all_started(:hocon)
{:ok, _} = Application.ensure_all_started(:logger)
Logger.info("Reading runtime config from #{path}")
conf = path |> File.read!() |> Hocon.decode!()
runtime = [mailer_config(conf)] |> filter_nils()
Config.Reader.merge(config, runtime)
end
defp mailer_config(%{"mailer" => %{"server" => server, "port" => port}}) do
{JobsKliniken.Mailer, [server: server, port: port]}
end
defp mailer_config(%{}) do
{JobsKliniken.Mailer, nil}
end
defp filter_nils(keyword) do
Enum.reject(keyword, fn {_key, value} -> is_nil(value) end)
end
end
Similar to decode/2
except it will unwrap the error tuple and raise
in case of errors.
Returns a value for the keypath
from a map or a successfull parse HOCON string.
Example
iex> conf = Hocon.decode!(~s(a { b { c : "10kb" } }))
%{"a" => %{"b" => %{"c" => "10kb"}}}
iex> Hocon.get(conf, "a.b.c")
"10kb"
iex> Hocon.get(conf, "a.b.d")
nil
iex> Hocon.get(conf, "a.b.d", "1kb")
"1kb"
Same a get/3
but the value is interpreted like a number by using the power of 2.
Example
iex> conf = Hocon.decode!(~s(a { b { c : "10kb" } }))
%{"a" => %{"b" => %{"c" => "10kb"}}}
iex> Hocon.get_bytes(conf, "a.b.c")
10240
iex> Hocon.get_bytes(conf, "a.b.d")
nil
iex> Hocon.get_bytes(conf, "a.b.d", 1024)
1024
Same a get/3
but the value is interpreted like a number by using the power of 10.
Example
iex> conf = Hocon.decode!(~s(a { b { c : "10kb" } }))
%{"a" => %{"b" => %{"c" => "10kb"}}}
iex> Hocon.get_bytes(conf, "a.b.c")
10240
iex> Hocon.get_bytes(conf, "a.b.d")
nil
iex> Hocon.get_bytes(conf, "a.b.d", 1024)
1024