feat: upper each env name

This commit is contained in:
Dmitriy Pleshevskiy 2019-12-25 15:31:10 +03:00
parent 1acd0aa557
commit 3b520a2b33
2 changed files with 30 additions and 12 deletions

View file

@ -262,7 +262,6 @@ macro_rules! __itconfig_parse_variables {
current_variable = { current_variable = {
name = $name, name = $name,
ty = $ty, ty = $ty,
env_name = stringify!($name),
default = $default, default = $default,
}, },
tokens = [$($rest)*], tokens = [$($rest)*],
@ -282,7 +281,6 @@ macro_rules! __itconfig_parse_variables {
current_variable = { current_variable = {
name = $name, name = $name,
ty = $ty, ty = $ty,
env_name = stringify!($name),
}, },
tokens = [$($rest)*], tokens = [$($rest)*],
$($args)* $($args)*
@ -379,8 +377,8 @@ macro_rules! __itconfig_impl {
$(__itconfig_variable! { $(__itconfig_variable! {
name = $ns_var_name, name = $ns_var_name,
env_name = concat!(stringify!($ns_name), "_", stringify!($ns_var_name)),
$($ns_variables)* $($ns_variables)*
env_prefix = concat!(stringify!($ns_name), "_"),
})* })*
} }
)* )*
@ -393,8 +391,8 @@ macro_rules! __itconfig_impl {
$(__itconfig_variable! { $(__itconfig_variable! {
name = $var_name, name = $var_name,
env_name = stringify!($var_name),
$($variable)* $($variable)*
env_prefix = "",
})* })*
} }
}; };
@ -412,13 +410,12 @@ macro_rules! __itconfig_variable {
// Add method with default value // Add method with default value
( (
name = $name:ident, name = $name:ident,
ty = $ty:ty,
env_name = $env_name:expr, env_name = $env_name:expr,
ty = $ty:ty,
default = $default:expr, default = $default:expr,
env_prefix = $env_prefix:expr,
) => { ) => {
pub fn $name() -> $ty { pub fn $name() -> $ty {
env::var(concat!($env_prefix, $env_name)) env::var($env_name.to_uppercase())
.map(|val| EnvValue::from(val).into()) .map(|val| EnvValue::from(val).into())
.unwrap_or_else(|_| $default) .unwrap_or_else(|_| $default)
} }
@ -427,16 +424,15 @@ macro_rules! __itconfig_variable {
// Add method without default value // Add method without default value
( (
name = $name:ident, name = $name:ident,
ty = $ty:ty,
env_name = $env_name:expr, env_name = $env_name:expr,
env_prefix = $env_prefix:expr, ty = $ty:ty,
) => { ) => {
pub fn $name() -> $ty { pub fn $name() -> $ty {
env::var(concat!($env_prefix, $env_name)) env::var($env_name.to_uppercase())
.map(|val| EnvValue::from(val).into()) .map(|val| EnvValue::from(val).into())
.unwrap_or_else(|_| { .unwrap_or_else(|_| {
panic!(format!(r#"Cannot read "{}" environment variable"#, panic!(format!(r#"Cannot read "{}" environment variable"#,
concat!($env_prefix, $env_name))) $env_name.to_uppercase()))
}) })
} }

View file

@ -128,16 +128,38 @@ fn change_config_module_name() {
#[test] #[test]
fn config_namespace() { fn config_namespace() {
env::set_var("POSTGRES_HOST", "t");
config! { config! {
DEBUG: bool => true, DEBUG: bool => true,
POSTGRES { POSTGRES {
HOST: bool => true, HOST: bool,
PORT: bool => true, PORT: bool => true,
USERNAME: bool => true, USERNAME: bool => true,
} }
APP {}
} }
assert_eq!(cfg::DEBUG(), true); assert_eq!(cfg::DEBUG(), true);
assert_eq!(cfg::POSTGRES::HOST(), true); assert_eq!(cfg::POSTGRES::HOST(), true);
} }
#[test]
fn config_in_lowercase() {
env::set_var("DEBUG", "t");
env::set_var("NAMESPACE_FOO", "t");
config! {
debug: bool,
namespace {
foo: bool,
}
}
assert_eq!(cfg::debug(), true);
assert_eq!(cfg::namespace::foo(), true);
}