gmtprime/skogsra

Getting the value of env var returns {:ok, nil} if required is false

Closed this issue · 3 comments

Hi,
I recently started using Skogsra in one of my projects and I went through a couple of unexpected situations.

According to the README, every function generated by Skogsra has a spec like @spec my_port() :: {:ok, integer()} | {:error, binary()}. However, when I try to get an environment variable which wasn't set and which is not defined as required, the function returns {:ok, nil} (which isn't covered by the spec).
In the same situation moreover, the bang version of the generated function doesn't raise but simply returns nil.

I'd like to discuss with you about this behavior and, in case, I'm available for submitting a PR to tackle the problem.

Let me know if you need further information.

Hi @matt-mazzucato,

You're absolutely right! That's an uncovered case, so this is a bug (or unexpected feature haha).

Assuming the type is set to :integer explicitly (type: :integer) or implicitly (default: 4000), the specs should follow the following rules:

Required Has default Spec
true true @spec my_port() :: {:ok, integer()} | {:error, binary()}
true false @spec my_port() :: {:ok, integer()} | {:error, binary()}
false true @spec my_port() :: {:ok, integer()} | {:error, binary()}
false false @spec my_port() :: {:ok, nil | integer()} | {:error, binary()}

In other words, when there is no default and is not required then the spec should be as the one you'd expect e.g. for the following declaration:

defmodule Config do
  use Skogsra

  app_env :my_port, :myapp, :port 
end

we would expect the following spec:

@spec my_port() :: {:ok, nil | integer()} | {:error, binary()}

I've just wrote a small patch to fix this issue. I'll be publishing the new version today.

Thanks for report this bug!

Done! New version v2.2.3 is published :)

Hexdiff

Thanks! :)