FWIW in config/runtime.exs we can use dependencies and if we happen to have Ecto we can use:
config :my_app, :bool_value, Ecto.Type.cast!(:boolean, System.get_env(“”, “false”)) config :my_app, :maybe_int_value, (n = System.get_env("N")) && Ecto.Type.cast!(:integer, n) What I like about this is instead of having System.get_env AND System.get_env_as, we compose System.get_env with another function that can be used for other things. That being said this obviously requires Ecto. (Though looking at Ecto.Type, Saul, Norm, and friends, I’m rooting for lightweight data casting/validation library in core one day.) `TRUE`/`FALSE` values are not currently supported. So there’s an argument to be made for something more shell env vars specific so to speak. > On 18 Feb 2025, at 17:10, dave.lu...@gmail.com <dave.lucia....@gmail.com> > wrote: > > A solution to edge case values could be to allow the user to specify the > truthy values, e.g. > > config :my_app, :bool_value, System.get_env_as!("BOOL_VALUE", "false", as: > :boolean, truthy_values: ["on", "true", "TRUE", "FIRE ME UP"]) > > I could also see separate functions for booleans and integers, but either > works > > On Tuesday, February 18, 2025 at 11:07:50 AM UTC-5 dave.lu > <http://dave.lu/>...@gmail.com <http://gmail.com/> wrote: >> In Elixir applications, config/runtime.exs is often used to parse >> environment variables from System.get_env2/ and friends, and convert into >> Application configuration. >> >> Since environment variables can only be strings, it is often necessary to >> cast from string values into booleans and integer values. E.g. >> >> config :my_app, :bool_value, System.get_env("BOOL_VALUE") == "true" >> >> config :my_app, :int_value, String.to_string(System.get_env("INT_VALUE", >> "5")) >> >> While there are entire libraries that deal with advanced runtime >> configuration, like Vapor <https://github.com/elixir-toniq/vapor> and >> elixir-specify <https://github.com/Qqwy/elixir-specify>, I believe there is >> an opportunity to simplify common patterns and duplicative code in Elixir >> configuration that often leads to confusion and edge case issues. >> >> My proposal is to add parsing for both booleans and integers to environment >> variable parsing. As an example: >> >> config :my_app, :bool_value, System.get_env_as!("BOOL_VALUE", "false", as: >> :boolean) >> >> config :my_app, :int_value, System.get_env_as!("INT_VALUE", "5", as: >> :integer) >> >> The trickiness would be in the acceptable range of edge case values. For >> example, "1" and "0" are often used for boolean values, in addition to >> "TRUE" and "FALSE". I believe there could be a reasonable compromise and >> appropriate documentation for addressing these issues. >> >> More complex parsing, such as floats and custom data types would not be >> supported. > > > -- > You received this message because you are subscribed to the Google Groups > "elixir-lang-core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to elixir-lang-core+unsubscr...@googlegroups.com > <mailto:elixir-lang-core+unsubscr...@googlegroups.com>. > To view this discussion visit > https://groups.google.com/d/msgid/elixir-lang-core/bd3ee17d-28fc-4944-89b5-204eab7e6bfcn%40googlegroups.com > > <https://groups.google.com/d/msgid/elixir-lang-core/bd3ee17d-28fc-4944-89b5-204eab7e6bfcn%40googlegroups.com?utm_medium=email&utm_source=footer>. -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/elixir-lang-core/964BB907-1965-4FF6-908E-CEF550612089%40wojtekmach.pl.