Skip to content

Commit

Permalink
add support for url resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
zookzook committed Dec 19, 2019
1 parent d2700a1 commit 5fd54c7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.5
* Enhancements
* added support for url resolvers

## 0.1.4

* Enhancements
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ https://github.com/lightbend/config/blob/master/HOCON.md
- [x] substitutions
- [x] includes
- [x] conversion of numerically-indexed objects to arrays
- [ ] allow URL for included files
- [x] allow URL for included files
- [x] duration unit format
- [x] period unit format
- [x] size unit format
37 changes: 25 additions & 12 deletions lib/hocon/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ defmodule Hocon.Parser do
##
# loads and parse the content of the `file`
##
defp load_configuration_file(file, opts) do
defp load_configuration_file(schema, file, opts) do

included_files = Keyword.get(opts, :included_files, [])

with :ok <- check_recursion(file, included_files),
{:ok, conf} <- load_contents_of_file(file, opts),
{:ok, conf} <- load_contents_of_file(schema, file, opts),
{:ok, ast} <- Tokenizer.decode(conf),
{[], result } <- ast
|> contact_rule([])
Expand All @@ -134,13 +134,26 @@ defmodule Hocon.Parser do
##
# loads possible extension in combination with the filename
##
defp load_contents_of_file(file, opts) do
defp load_contents_of_file(:file, file, opts) do
extenions = case Path.extname(file) do
"" -> [".conf", ".json", ".properties"]
ext -> [ext]
end

resolver = Keyword.get(opts, :resolver, Hocon.FileResolver)
resolver = Keyword.get(opts, :file_resolver, Hocon.FileResolver)
file = Path.rootname(file)
case Enum.find(extenions, fn ext -> resolver.exists?(file <> ext) end) do
nil -> {:error, :not_found}
ext -> resolver.load(file <> ext)
end
end
defp load_contents_of_file(:url, file, opts) do
extenions = case Path.extname(file) do
"" -> [".conf", ".json", ".properties"]
ext -> [ext]
end

resolver = Keyword.get(opts, :url_resolver, Hocon.FileResolver)
file = Path.rootname(file)
case Enum.find(extenions, fn ext -> resolver.exists?(file <> ext) end) do
nil -> {:error, :not_found}
Expand Down Expand Up @@ -212,16 +225,16 @@ defmodule Hocon.Parser do
# * file location
##
defp parse_include([:required, :open_round | rest], result, root, opts) do
{rest, file} = parse_file_location(rest)
result = case load_configuration_file(file, opts) do
{rest, schema, file} = parse_file_location(rest)
result = case load_configuration_file(schema, file, opts) do
{:ok, doc} -> Document.merge(result, doc)
_ -> throw {:error, "file #{file} was not found"}
end
parse_required_close(rest, result, root, opts)
end
defp parse_include(rest, result, root, opts) do
{rest, file} = parse_file_location(rest)
result = case load_configuration_file(file, opts) do
{rest, schema, file} = parse_file_location(rest)
result = case load_configuration_file(schema, file, opts) do
{:ok, doc} -> Document.merge(result, doc)
_ -> result
end
Expand All @@ -243,16 +256,16 @@ defmodule Hocon.Parser do
# * /path/to/somewhere
##
defp parse_file_location([:file, :open_round, {:string, file}, :close_round | rest]) do
{rest, file}
{rest, :file, file}
end
defp parse_file_location([:url, :open_round, {:string, file}, :close_round | rest]) do
{rest, file}
{rest, :url, file}
end
defp parse_file_location([{:string, file} | rest]) do
{rest, file}
{rest, :file, file}
end
defp parse_file_location([{:unquoted_string, file} | rest]) do
{rest, file}
{rest, :file, file}
end
defp parse_file_location(_rest) do
throw {:error, "syntax error: file location required"}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Hocon.MixProject do
use Mix.Project

@version "0.1.4"
@version "0.1.5"

def project do
[
Expand Down

0 comments on commit 5fd54c7

Please sign in to comment.