Skip to content

Commit

Permalink
Changed YAML parser to native Elixir
Browse files Browse the repository at this point in the history
  • Loading branch information
KamilLelonek committed Jul 28, 2015
1 parent d703b91 commit 4485d39
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/figaro_elixir/env.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule FigaroElixir.Env do
)
end

def put_or_delete_env(key, "~"), do: System.delete_env(key)
def put_or_delete_env(key, ""), do: System.delete_env(key)
def put_or_delete_env(key, val), do: System.put_env(key, val)

def upcase_keys(map) do
Expand All @@ -23,7 +23,7 @@ defmodule FigaroElixir.Env do
|> convert_array_to_map
end

defp upcase_keys_transformation({key, value}), do: { String.upcase(key), to_string(value) }
defp upcase_keys_transformation({key, value}), do: { String.upcase(key), value }
defp map_all_key_to_uppercase(map), do: map |> Enum.map(&upcase_keys_transformation/1)
defp convert_array_to_map(array), do: array |> Enum.into(%{})
end
6 changes: 3 additions & 3 deletions lib/figaro_elixir/yaml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ defmodule FigaroElixir.Yaml do

defp parse_file(file_path) do
file_path
|> Yomel.decode_file
|> YamlExt.decode_file
|> extract_map
rescue
FunctionClauseError -> %{}
end

defp extract_map({ :ok, [""] }), do: %{}
defp extract_map({ :ok, [result] }), do: result
defp extract_map(nil), do: %{}
defp extract_map(map), do: map
end
37 changes: 37 additions & 0 deletions lib/figaro_elixir/yaml_ext.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule YamlExt do
def decode_file(file_path) do
case :yamerl_constr.file(file_path, detailed_constr: true) do
[{ _, document }] -> decode(document)
_ -> %{}
end
end

defp decode(document) do
case _to_map(document) do
"" -> %{}
decoded -> decoded
end
end

defp _to_map({ :yamerl_seq, :yamerl_node_seq, _tag, _loc, seq, _n }),
do: Enum.map(seq, &_to_map(&1))

defp _to_map({ :yamerl_map, :yamerl_node_map, _tag, _loc, map_tuples }),
do: _tuples_to_map(map_tuples, %{})

defp _to_map({ _yamler_element, _yamler_node_element, _tag, _loc, elem }),
do: to_string(elem)

defp _to_map({ _yamler_element, _yamler_node_element, _tag, _loc }),
do: ""

defp _tuples_to_map([], map),
do: map

defp _tuples_to_map([{ key, val } | rest], map) do
case key do
{ :yamerl_str, :yamerl_node_str, _tag, _log, name } ->
_tuples_to_map(rest, Dict.put_new(map, to_string(name), _to_map(val)))
end
end
end
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ defmodule FigaroElixir.Mixfile do

defp apps do
[
:logger
:logger,
:yamerl
]
end

defp deps do
[
{ :yomel, "~> 0.2.2" }
{ :yamerl, github: "yakaz/yamerl" }
]
end

Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1 +1 @@
%{"yomel": {:hex, :yomel, "0.2.2"}}
%{"yamerl": {:git, "git://github.com/yakaz/yamerl.git", "ae810a808817d9482b4628ae3e20d746e3729fe0", []}}
2 changes: 1 addition & 1 deletion test/figaro_elixir/env_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule FigaroElixir.EnvTest do
end

test "nullify system variables" do
config = %{"b" => "~"}
config = %{"b" => ""}

System.put_env("B", "B")

Expand Down
8 changes: 4 additions & 4 deletions test/figaro_elixir/yaml_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ defmodule FigaroElixir.YamlTest do
alias FigaroElixir.Yaml

test "parsing flat file contents" do
assert_result "flat", %{ "a" => "a", "b" => 1, "c" => "true" }
assert_result "flat", %{ "a" => "a", "b" => "1", "c" => "true" }
end

test "parsing nested file contents" do
assert_result "nested", %{
"dev" => %{"A" => 1234, "B" => 1234},
"dev" => %{"A" => "1234", "B" => "1234"},
"prod" => %{"A" => "12ab", "B" => "34bc"},
"test" => %{"A" => "abcd", "B" => "abcd"}
}
end

test "parsing nullified file contents" do
assert_result "nullify", %{"a" => 1, "b" => "~"}
assert_result "nullify", %{"a" => "1", "b" => ""}
end

test "parsing multi file contents" do
assert_result "multi", %{"a" => "a", "test" => %{"b" => 1}}
assert_result "multi", %{"a" => "a", "test" => %{"b" => "1"}}
end

test "parsing file with no contents" do
Expand Down

0 comments on commit 4485d39

Please sign in to comment.