Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for custom function names #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,23 @@ defmodule MyApp.Env do
end
```

## Setting custom function names

You can use the `fun` option to use a custom function name instead of the default one.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a more descriptive information in the docs.

"By default the lowercase string version of the environment variable names are used to generate the corresponding function names. However you can override this behaviour by using the fun option to use a custom function name instead of the default one."


```elixir
defmodule MyApp.Env do
use Mahaul,
MY_ENV: [type: :str, fun: :hello, default: "Hello World"]
end
```

```
iex -S mix
iex> MyApp.Env.hello()
Hello World
```

## Setting documentation

There is a default documentation added for each of the compile time generated function equivalents for the environment variables. However you may use the `doc` option to add a custom documentation with more details and explanations as per your needs.
Expand Down
7 changes: 6 additions & 1 deletion lib/mahaul.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ defmodule Mahaul do

Enum.each(opts, fn {key, val} ->
str_key = Atom.to_string(key)
fn_name = str_key |> String.downcase() |> String.to_atom()
fn_name_default = str_key |> String.downcase() |> String.to_atom()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this statement down in the else block to keep the function name generation inside a single block and avoid computation in unused case.

      fn_name =
          if Keyword.keyword?(val) && Keyword.get(val, :fun, "") |> is_atom(),
            do: Keyword.get(val, :fun),
            else: str_key |> String.downcase() |> String.to_atom()


fn_name =
if Keyword.keyword?(val) && Keyword.get(val, :fun, "") |> is_atom(),
do: Keyword.get(val, :fun),
else: fn_name_default

type = if Keyword.keyword?(val), do: val[:type]
doc = if Keyword.keyword?(val), do: val[:doc]
Expand Down
7 changes: 7 additions & 0 deletions lib/mahaul/configs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ defmodule Mahaul.Configs do
end
end

defp validate_opt!({:fun, fun}, name) do
unless is_atom(fun) do
raise ArgumentError,
"#{name}: expected :fun to be an atom, got: #{inspect(fun)}"
end
end

defp validate_opt!({:choices, choices}, name) do
unless is_list(choices) and not Enum.empty?(choices) do
raise ArgumentError,
Expand Down
37 changes: 37 additions & 0 deletions test/mahaul_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ defmodule MahaulTest do
{"MOCK__ENV: expected :default_dev to be a string, got: false",
[MOCK__ENV: [type: :str, default_dev: false]]},
{"MOCK__ENV: expected :doc to be a string, got: []", [MOCK__ENV: [type: :str, doc: []]]},
{"MOCK__ENV: expected :fun to be an atom, got: \"fun_name\"",
[MOCK__ENV: [type: :str, fun: "fun_name"]]},
{"MOCK__ENV: unknown option provided {:invalid_option, \"__MOCK__\"}",
[MOCK__ENV: [type: :str, invalid_option: "__MOCK__"]]}
]
Expand Down Expand Up @@ -408,4 +410,39 @@ defmodule MahaulTest do
assert 7 = Env.Choices3.mock__env__choices()
end
end

describe "fun option" do
test "should define a function with custom name" do
Config.Reader.read!("test/support/config/custom.exs")
|> Application.put_all_env()

defmodule Env.Fun1 do
use Mahaul,
MOCK__ENV__STR: [type: :str, fun: :custom_str],
MOCK__ENV__STR2: [
type: :str,
fun: :custom_str2,
default: "VAL1",
defaults: [custom: "CUSTOM_VAL"]
],
MOCK__ENV__ENUM: [type: :enum, fun: :custom_enum],
MOCK__ENV__NUM: [type: :num, fun: :custom_num],
MOCK__ENV__INT: [type: :int, fun: :custom_int],
MOCK__ENV__BOOL: [type: :bool, fun: :custom_bool],
MOCK__ENV__PORT: [type: :port, fun: :custom_port],
MOCK__ENV__HOST: [type: :host, fun: :custom_host],
MOCK__ENV__URI: [type: :uri, fun: :custom_uri]
end

assert "__MOCK__VAL1__" == Env.Fun1.custom_str()
assert "CUSTOM_VAL" == Env.Fun1.custom_str2()
assert :__MOCK__VAL2__ = Env.Fun1.custom_enum()
assert 10.10 = Env.Fun1.custom_num()
assert 10 = Env.Fun1.custom_int()
assert true = Env.Fun1.custom_bool()
assert 8080 = Env.Fun1.custom_port()
assert "//example.com" = Env.Fun1.custom_host()
assert "https://example.com/something" = Env.Fun1.custom_uri()
end
end
end