Skip to content

Commit

Permalink
fix: Fix env_prefix inheritance.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jan 4, 2024
1 parent 303e161 commit 21145cc
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Unreleased

#### 🐞 Fixes

- Fixed an issue where environment variables for `SchemaField` weren't being populated from
`env_prefix`.

## 0.13.0

#### 💥 Breaking
Expand Down
14 changes: 13 additions & 1 deletion crates/macros/src/common/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct Field<'l> {
pub name: &'l Ident,
pub value: &'l Type,
pub value_type: FieldValue<'l>,
pub env_prefix: Option<String>,
}

impl<'l> Field<'l> {
Expand All @@ -70,6 +71,7 @@ impl<'l> Field<'l> {
},
args,
serde_args,
env_prefix: None,
};

if field.args.default.is_some() {
Expand Down Expand Up @@ -117,6 +119,16 @@ impl<'l> Field<'l> {
}
}

pub fn get_env_var(&self) -> Option<String> {
if let Some(env_name) = &self.args.env {
Some(env_name.to_owned())
} else {
self.env_prefix
.as_ref()
.map(|env_prefix| format!("{}{}", env_prefix, self.get_name(None)).to_uppercase())
}
}

pub fn get_serde_meta(&self) -> Option<TokenStream> {
let mut meta = vec![];

Expand Down Expand Up @@ -164,7 +176,7 @@ impl<'l> Field<'l> {
let nullable = map_bool_quote("nullable", self.is_optional());
let description = map_option_quote("description", extract_comment(&self.attrs));
let deprecated = map_option_quote("deprecated", extract_deprecated(&self.attrs));
let env_var = map_option_quote("env_var", self.args.env.as_ref());
let env_var = map_option_quote("env_var", self.get_env_var());

let value = self.value;
let mut type_of = if self.is_nested() {
Expand Down
10 changes: 9 additions & 1 deletion crates/macros/src/common/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ impl<'l> Macro<'l> {
let config_type = match &input.data {
Data::Struct(data) => match &data.fields {
Fields::Named(fields) => Container::NamedStruct {
fields: fields.named.iter().map(Field::from).collect::<Vec<_>>(),
fields: fields
.named
.iter()
.map(|f| {
let mut field = Field::from(f);
field.env_prefix = args.env_prefix.clone();
field
})
.collect::<Vec<_>>(),
},
Fields::Unnamed(_) => {
panic!("Unnamed structs are not supported.");
Expand Down
4 changes: 2 additions & 2 deletions crates/macros/src/config/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ impl<'l> Container<'l> {
}
}

pub fn generate_env_values(&self, prefix: Option<&String>) -> TokenStream {
pub fn generate_env_values(&self) -> TokenStream {
match self {
Self::NamedStruct {
fields: settings, ..
} => {
let env_stmts = settings
.iter()
.filter_map(|s| s.generate_env_statement(prefix))
.filter_map(|s| s.generate_env_statement())
.collect::<Vec<_>>();

if env_stmts.is_empty() {
Expand Down
9 changes: 3 additions & 6 deletions crates/macros/src/config/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ impl<'l> Field<'l> {
}
}

pub fn generate_env_statement(&self, prefix: Option<&String>) -> Option<TokenStream> {
pub fn generate_env_statement(&self) -> Option<TokenStream> {
if self.is_nested() {
return None;
}

let name = self.name;
let env = self.get_env_var();

let env = if let Some(env_name) = &self.args.env {
env_name.to_owned()
} else if let Some(env_prefix) = prefix {
format!("{}{}", env_prefix, self.get_name(None)).to_uppercase()
} else {
if env.is_none() {
if self.args.parse_env.is_some() {
panic!("Cannot use `parse_env` without `env` or a parent `env_prefix`.");
}
Expand Down
3 changes: 1 addition & 2 deletions crates/macros/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ impl<'l> ToTokens for ConfigMacro<'l> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let cfg = &self.0;
let name = cfg.name;
let env_prefix = cfg.args.env_prefix.as_ref();

// Generate the partial implementation
let partial_name = format_ident!("Partial{}", cfg.name);
Expand All @@ -27,7 +26,7 @@ impl<'l> ToTokens for ConfigMacro<'l> {
// Generate implementations
let meta = cfg.get_meta_struct();
let default_values = cfg.type_of.generate_default_values();
let env_values = cfg.type_of.generate_env_values(env_prefix);
let env_values = cfg.type_of.generate_env_values();
let extends_from = cfg.type_of.generate_extends_from();
let finalize = cfg.type_of.generate_finalize();
let merge = cfg.type_of.generate_merge();
Expand Down
1 change: 1 addition & 0 deletions crates/schematic/tests/generator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct GenConfig {

/// Some comment.
#[derive(Clone, Config)]
#[config(env_prefix = "ENV_PREFIX_")]
pub struct TwoDepthConfig {
/// An optional string.
opt: Option<String>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: crates/config/tests/env_test.rs
source: crates/schematic/tests/env_test.rs
expression: "std::fs::read_to_string(file).unwrap()"
---
// Automatically generated by schematic. DO NOT MODIFY!
Expand All @@ -12,12 +12,19 @@ export interface EnvVarsNested {
}

export interface EnvVarsPrefixed {
/** @envvar ENV_BOOL */
bool: boolean;
/** @envvar ENV_LIST1 */
list1: string[];
/** @envvar ENV_LIST2 */
list2: number[];
/** @envvar ENV_NESTED */
nested: EnvVarsNested;
/** @envvar ENV_NUMBER */
number: number;
/** @envvar ENV_PATH */
path: string;
/** @envvar ENV_STRING */
string: string;
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: crates/config/tests/generator_test.rs
source: crates/schematic/tests/generator_test.rs
expression: "fs::read_to_string(file).unwrap()"
---
{
Expand Down Expand Up @@ -39,6 +39,7 @@ expression: "fs::read_to_string(file).unwrap()"
// This is another nested field.
"two": {
// An optional string.
// @envvar ENV_PREFIX_OPT
"opt": "",
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: crates/config/tests/generator_test.rs
source: crates/schematic/tests/generator_test.rs
expression: "fs::read_to_string(file).unwrap()"
---
# This is a boolean with a medium length description.
Expand Down Expand Up @@ -44,6 +44,7 @@ enums = "foo"
# This is another nested field.
[one.two]
# An optional string.
# @envvar ENV_PREFIX_OPT
opt = ""


Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: crates/config/tests/generator_test.rs
source: crates/schematic/tests/generator_test.rs
expression: "fs::read_to_string(file).unwrap()"
---
# This is a boolean with a medium length description.
Expand Down Expand Up @@ -37,6 +37,7 @@ one:
# This is another nested field.
two:
# An optional string.
# @envvar ENV_PREFIX_OPT
opt: ""

# This is a string.
Expand Down

0 comments on commit 21145cc

Please sign in to comment.