From 8af782b1c22e7f7459bc63f212a4b7fe658e1982 Mon Sep 17 00:00:00 2001 From: psteinroe Date: Fri, 19 Dec 2025 14:43:04 +0100 Subject: [PATCH] fix: improve env parsing --- configuration/base.yml | 1 + src/config/load.rs | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/configuration/base.yml b/configuration/base.yml index 3bd7955..c773f42 100644 --- a/configuration/base.yml +++ b/configuration/base.yml @@ -5,6 +5,7 @@ stream: port: 5432 name: "postgres" username: "postgres" + password: null tls: enabled: false trusted_root_certs: "" diff --git a/src/config/load.rs b/src/config/load.rs index 8cbde96..fd86da7 100644 --- a/src/config/load.rs +++ b/src/config/load.rs @@ -208,12 +208,11 @@ where let mut environment_source = config::Environment::with_prefix(ENV_PREFIX) .prefix_separator(ENV_PREFIX_SEPARATOR) - .separator(ENV_SEPARATOR); + .separator(ENV_SEPARATOR) + .try_parsing(true); if !T::LIST_PARSE_KEYS.is_empty() { - environment_source = environment_source - .try_parsing(true) - .list_separator(LIST_SEPARATOR); + environment_source = environment_source.list_separator(LIST_SEPARATOR); for key in ::LIST_PARSE_KEYS { environment_source = environment_source.with_list_parse_key(key); @@ -444,4 +443,28 @@ mod tests { }, ); } + + #[test] + fn test_env_vars_parse_numbers() { + let temp_dir = TempDir::new().unwrap(); + let config_dir = temp_dir.path().join("configuration"); + fs::create_dir(&config_dir).unwrap(); + + let base_content = "value: \"base\"\nnumber: 1\n"; + fs::write(config_dir.join("base.yml"), base_content).unwrap(); + + with_vars( + vec![ + ("APP_ENVIRONMENT", Some("prod")), + ("APP_CONFIG_DIR", Some(config_dir.to_str().unwrap())), + ("APP_VALUE", Some("from_env")), + ("APP_NUMBER", Some("5432")), // String that should parse to i32 + ], + || { + let config: TestConfig = load_config().unwrap(); + assert_eq!(config.value, "from_env"); + assert_eq!(config.number, 5432); + }, + ); + } }