-
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
6 changed files
with
108 additions
and
17 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 |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
|
||
```rust | ||
[dependencies] | ||
envconfig = "0.10.1" | ||
envconfig = "0.10.2" | ||
``` | ||
|
||
## Usage | ||
|
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "envconfig" | ||
version = "0.10.1" | ||
version = "0.10.2" | ||
authors = ["Sergey Potapov <[email protected]>"] | ||
description = "Build a config structure from environment variables without boilerplate." | ||
categories = ["config", "web-programming"] | ||
|
@@ -15,4 +15,4 @@ edition = "2018" | |
[dev-dependencies] | ||
|
||
[dependencies] | ||
envconfig_derive = { version = "0.10.1", path = "../envconfig_derive" } | ||
envconfig_derive = { version = "0.10.2", path = "../envconfig_derive" } |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "envconfig_derive" | ||
version = "0.10.1" | ||
version = "0.10.2" | ||
authors = ["Sergey Potapov <[email protected]>"] | ||
description = "Build a config structure from environment variables without boilerplate." | ||
categories = ["config", "web-programming"] | ||
|
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); | ||
} |