palkan / anyway_config

Configuration library for Ruby gems and applications
MIT License
778 stars 52 forks source link

Environment variable lists with zero / one element are parsed as strings #82

Closed jtuttle closed 3 years ago

jtuttle commented 3 years ago

What did you do?

When loading a config value that's supposed to be a list through an environment variable, the type of the parsed variable differs based on the number of elements in the list (zero/one vs many). That is:

MY_LIST=one is parsed as a string MY_LIST=one,two,three is parsed as a list

This can be tricky to handle in the code that consumes the configuration value. For example, if trying to write a validation in the on_load method of a configuration class, you have to make sure it handles both types appropriately (or converts one to the other).

What did you expect to happen?

An environment variable with a comma-separated list is parsed as a string.

What actually happened?

An environment variable with a comma-separated list is parsed as a list.

Additional context

Environment

Ruby Version: Ruby 2.5.8p224

Framework Version (Rails, whatever): Rails 5.2.6

Anyway Config Version: 2.1.0

jtuttle commented 3 years ago

I know this is a feature since it's mentioned in the README but I think it might be better to remove the auto-parsing of comma-separated strings. Either that or have some way to specify the type you're expecting. Or maybe infer it from the default value?

palkan commented 3 years ago

Or maybe infer it from the default value?

I like it. Sounds like a good option to reduce breaking changes in the existing configs.

Will try to play with it next week.