-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add "env_prefix" attribute for struct derive
This attribute allows to add an annotation like so `#[envconfig(env_prefix = "...")]` on top of a struct. The `env_prefix` will be applied to every config attribute in the struct.
- Loading branch information
Showing
2 changed files
with
101 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
extern crate envconfig; | ||
|
||
use envconfig::{Envconfig}; | ||
use std::env; | ||
|
||
#[derive(Envconfig)] | ||
#[envconfig(env_prefix = "TEST_")] | ||
pub struct ConfigWithoutFrom { | ||
pub db_host: String, | ||
pub db_port: u16, | ||
} | ||
|
||
fn setup() { | ||
env::remove_var("TEST_DB_HOST"); | ||
env::remove_var("TEST_DB_PORT"); | ||
} | ||
|
||
#[test] | ||
fn test_init_from_env_with_env_prefix_and_no_envconfig_on_attributes() { | ||
setup(); | ||
|
||
env::set_var("TEST_DB_HOST", "localhost"); | ||
env::set_var("TEST_DB_PORT", "5432"); | ||
|
||
|
||
let config = ConfigWithoutFrom::init_from_env().unwrap(); | ||
assert_eq!(config.db_host, "localhost"); | ||
assert_eq!(config.db_port, 5432u16); | ||
} | ||
|
||
#[derive(Envconfig)] | ||
#[envconfig(env_prefix = "TEST_")] | ||
pub struct ConfigWithFrom { | ||
#[envconfig(from = "DB_HOST")] | ||
pub db_host: String, | ||
|
||
#[envconfig(from = "DB_PORT")] | ||
pub db_port: u16, | ||
} | ||
|
||
#[test] | ||
fn test_init_from_env_with_env_prefix_and_envconfig_on_attributes() { | ||
setup(); | ||
|
||
env::set_var("TEST_DB_HOST", "localhost"); | ||
env::set_var("TEST_DB_PORT", "5433"); | ||
|
||
|
||
let config = ConfigWithFrom::init_from_env().unwrap(); | ||
assert_eq!(config.db_host, "localhost"); | ||
assert_eq!(config.db_port, 5433u16); | ||
} |