Unset Array type resolves to nil
bessey opened this issue · 1 comments
What did you do?
Given this config:
class AppConfig < Anyway::Config
attr_config(
:extra_valid_hosts,
)
coerce_types(
extra_valid_hosts: { type: :string, array: true },
)
end
And nothing setting the value of APP_EXTRA_VALID_HOSTS
...
What did you expect to happen?
I expect AppConfig.new.extra_valid_hosts
to return []
because I have explicitly told AnywayConfig that its an array type.
[3] pry(main)> ENV["APP_EXTRA_VALID_HOSTS"] = ","
=> ","
[6] pry(main)> AppConfig.new.extra_valid_hosts
=> []
[7] pry(main)> ENV["APP_EXTRA_VALID_HOSTS"] = nil
=> nil
[9] pry(main)> AppConfig.new.extra_valid_hosts
=> [] # <<<<<<<< Good
What actually happened?
It returns nil
[3] pry(main)> ENV["APP_EXTRA_VALID_HOSTS"] = ","
=> ","
[6] pry(main)> AppConfig.new.extra_valid_hosts
=> []
[7] pry(main)> ENV["APP_EXTRA_VALID_HOSTS"] = nil
=> nil
[9] pry(main)> AppConfig.new.extra_valid_hosts
=> nil # <<<<<<<< Bad
Environment
Ruby Version: 3.1
Framework Version (Rails, whatever): Rails 6.1
Anyway Config Version: 2.3.1
I expect AppConfig.new.extra_valid_hosts to return [] because I have explicitly told AnywayConfig that its an array type.
I think, nil
is expected here, since it's the absence of the value, not an empty one. It can have a different meaning (e.g., if you want to implement a custom fallback option). And we coerce provided values; if nothing is provided, we cannot do anything.
In your cases, providing a default value should work as expected:
class AppConfig < Anyway::Config
attr_config(
extra_valid_hosts: []
)
end