Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment Variable List of Structs #597

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion examples/env-list/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
use config::Config;

#[derive(Debug, Default, serde_derive::Deserialize, PartialEq, Eq)]
struct ListOfStructs {
a: String,
b: String,
}

#[derive(Debug, Default, serde_derive::Deserialize, PartialEq, Eq)]
struct AppConfig {
list: Vec<String>,
structs: Vec<Option<ListOfStructs>>,
}

fn main() {
std::env::set_var("APP_LIST", "Hello World");
std::env::set_var("APP_STRUCTS_0_A", "Hello");
std::env::set_var("APP_STRUCTS_0_B", "World");
std::env::set_var("APP_STRUCTS_2_A", "foo");
std::env::set_var("APP_STRUCTS_2_B", "bar");

let config = Config::builder()
.add_source(
config::Environment::with_prefix("APP")
.try_parsing(true)
.separator("_")
.list_separator(" "),
.list_separator(" ")
.with_list_parse_key("list"),
)
.build()
.unwrap();

let app: AppConfig = config.try_deserialize().unwrap();

assert_eq!(app.list, vec![String::from("Hello"), String::from("World")]);
assert_eq!(
app.structs,
vec![
Some(ListOfStructs {
a: String::from("Hello"),
b: String::from("World")
}),
None,
Some(ListOfStructs {
a: String::from("foo"),
b: String::from("bar")
}),
]
);

std::env::remove_var("APP_LIST");
}
120 changes: 105 additions & 15 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::error::Result;
use crate::map::Map;
use crate::source::Source;
use crate::value::{Value, ValueKind};
use crate::ConfigError;

#[cfg(feature = "convert-case")]
use convert_case::{Case, Casing};
Expand All @@ -12,6 +13,63 @@ use convert_case::{Case, Casing};
/// config Value type. We have to be aware how the config tree is created from the environment
/// dictionary, therefore we are mindful about prefixes for the environment keys, level separators,
/// encoding form (kebab, snake case) etc.
///
/// Environment variables are specified using the path to the key they correspond to. When
/// specifying a value within a list, the index should be specified in the key immediately after
/// the list type.
///
/// ## Example
///
/// ```rust
/// # use config::{Environment, Config};
/// # use serde::Deserialize;
/// # use std::collections::HashMap;
/// # use std::convert::TryInto;
/// #
/// # #[test]
/// # fn test_env_list_of_structs() {
/// #[derive(Deserialize, Debug, PartialEq)]
/// struct Struct {
/// a: Vec<Option<u32>>,
/// b: u32,
/// }
///
/// #[derive(Deserialize, Debug)]
/// struct ListOfStructs {
/// list: Vec<Struct>,
/// }
///
/// let values = vec![
/// ("LIST_0_A_0".to_owned(), "1".to_owned()),
/// ("LIST_0_A_2".to_owned(), "2".to_owned()),
/// ("LIST_0_B".to_owned(), "3".to_owned()),
/// ("LIST_1_A_1".to_owned(), "4".to_owned()),
/// ("LIST_1_B".to_owned(), "5".to_owned()),
/// ];
///
/// let environment = Environment::default()
/// .separator("_")
/// .try_parsing(true)
/// .source(Some(values.into_iter().collect()));
///
/// let config = Config::builder().add_source(environment).build().unwrap();
///
/// let config: ListOfStructs = config.try_deserialize().unwrap();
/// assert_eq!(
/// config.list,
/// vec![
/// Struct {
/// a: vec![Some(1), None, Some(2)],
/// b: 3
/// },
/// Struct {
/// a: vec![None, Some(4)],
/// b: 5
/// },
/// ]
/// );
/// # }
/// ```
#[must_use]
#[derive(Clone, Debug, Default)]
pub struct Environment {
Expand Down Expand Up @@ -230,8 +288,8 @@ impl Source for Environment {
}

fn collect(&self) -> Result<Map<String, Value>> {
let mut m = Map::new();
let uri: String = "the environment".into();
let mut m = Value::new(Some(&uri), ValueKind::Table(Map::new()));

let separator = self.separator.as_deref().unwrap_or("");
#[cfg(feature = "convert-case")]
Expand All @@ -248,24 +306,25 @@ impl Source for Environment {
.as_ref()
.map(|prefix| format!("{prefix}{prefix_separator}").to_lowercase());

let collector = |(key, value): (String, String)| {
let collector = |(key, value): (String, String)| -> Result<()> {
// Treat empty environment variables as unset
if self.ignore_empty && value.is_empty() {
return;
return Ok(());
}

let mut key = key.to_lowercase();

// Check for prefix
let mut kept_prefix = String::new();
if let Some(ref prefix_pattern) = prefix_pattern {
if key.starts_with(prefix_pattern) {
if !self.keep_prefix {
// Remove this prefix from the key
key = key[prefix_pattern.len()..].to_string();
key = key[prefix_pattern.len()..].to_string();
if self.keep_prefix {
kept_prefix = prefix_pattern.clone();
}
} else {
// Skip this key
return;
return Ok(());
}
}

Expand All @@ -277,9 +336,10 @@ impl Source for Environment {
#[cfg(feature = "convert-case")]
if let Some(convert_case) = convert_case {
key = key.to_case(*convert_case);
kept_prefix = kept_prefix.to_case(*convert_case);
}

let value = if self.try_parsing {
let value_kind = if self.try_parsing {
// convert to lowercase because bool parsing expects all lowercase
if let Ok(parsed) = value.to_lowercase().parse::<bool>() {
ValueKind::Boolean(parsed)
Expand All @@ -288,11 +348,10 @@ impl Source for Environment {
} else if let Ok(parsed) = value.parse::<f64>() {
ValueKind::Float(parsed)
} else if let Some(separator) = &self.list_separator {
if let Some(keys) = &self.list_parse_keys {
if let Some(parse_keys) = &self.list_parse_keys {
#[cfg(feature = "convert-case")]
let key = key.to_lowercase();

if keys.contains(&key) {
if parse_keys.contains(&key) {
let v: Vec<Value> = value
.split(separator)
.map(|s| Value::new(Some(&uri), ValueKind::String(s.to_owned())))
Expand All @@ -315,14 +374,45 @@ impl Source for Environment {
ValueKind::String(value)
};

m.insert(key, Value::new(Some(&uri), value));
let value = Value::new(Some(&uri), value_kind);
let mut key_parts = key.split('.');
let key = key_parts
.next()
.ok_or(ConfigError::Message("No key after prefix".to_owned()))?;

let value = key_parts
.rev()
.fold(value, |value, key| match key.parse::<usize>() {
Ok(index) => {
let nil = Value::new(Some(&uri), ValueKind::Nil);
let mut array = vec![nil; index + 1];
array[index] = value;
Value::new(Some(&uri), ValueKind::Array(array))
}
Err(_) => {
let mut map = Map::new();
map.insert(key.to_owned(), value);
Value::new(Some(&uri), ValueKind::Table(map))
}
});

let mut map = Map::new();
kept_prefix.push_str(key);
map.insert(kept_prefix, value);
let table = Value::new(Some(&uri), ValueKind::Table(map));

m.merge(table);
Ok(())
};

match &self.source {
Some(source) => source.clone().into_iter().for_each(collector),
None => env::vars().for_each(collector),
Some(source) => source.clone().into_iter().try_for_each(collector)?,
None => env::vars().try_for_each(collector)?,
}

Ok(m)
match m.kind {
ValueKind::Table(m) => Ok(m),
_ => unreachable!(),
}
}
}
34 changes: 34 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,40 @@ impl Value {
)),
}
}

/// Merge the value with another value recursively.
///
/// In the case of a conflict, the other value (on the right) will take precedence.
pub(crate) fn merge(&mut self, other: Self) {
match (&mut self.kind, other.kind) {
(ValueKind::Table(ref mut table), ValueKind::Table(other_table)) => {
for (k, v) in other_table {
match table.entry(k) {
std::collections::hash_map::Entry::Occupied(mut entry) => {
entry.get_mut().merge(v);
}
std::collections::hash_map::Entry::Vacant(entry) => {
entry.insert(v);
}
}
}
}
(ValueKind::Array(ref mut array), ValueKind::Array(other)) => {
for (i, other) in other.into_iter().enumerate() {
if let Some(s) = array.get_mut(i) {
if other.kind != ValueKind::Nil {
s.merge(other);
}
} else {
array.push(other);
}
}
}
(s, other) => {
*s = other;
}
}
}
}

impl<'de> Deserialize<'de> for Value {
Expand Down
Loading