Skip to content

Commit

Permalink
fix(libs): env-collector required field check (#1047)
Browse files Browse the repository at this point in the history
* test with options

* fix: wrong checking that field is required

---------

Co-authored-by: sevenzing <41516657+sevenzing@users.noreply.github.com>
  • Loading branch information
bragov4ik and sevenzing authored Sep 3, 2024
1 parent 773d33d commit 8aac825
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions libs/env-collector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,21 +299,26 @@ where
{
let mut json = serde_json::to_value(settings).expect("structure should be serializable");
json.dot_remove(path).expect("value path not found");

let settings_with_default_value = serde_json::from_value::<S>(json).ok()?;
let json = serde_json::to_value(&settings_with_default_value)
let json: serde_json::Value = serde_json::to_value(&settings_with_default_value)
.expect("structure should be serializable");
json.dot_get(path).expect("value path not found")
let default_value: serde_json::Value = json
.dot_get(path)
.expect("value path not found")
.unwrap_or_default();
Some(default_value)
}

fn try_get_description(_key: &str, value: &str, default: &Option<serde_json::Value>) -> String {
if value.is_empty() {
return Default::default();
}
let default_str = default.as_ref().map(|v| v.to_string()).unwrap_or_default();

if let Some(default) = default {
if *default == value {
return Default::default();
}
// If the value is the same as the default value, we don't need to show it in the description
if default_str == value {
return Default::default();
}

format!("e.g. `{}`", value)
Expand Down Expand Up @@ -408,6 +413,8 @@ mod tests {
pub test: String,
#[serde(default = "default_test2")]
pub test2: i32,
pub test3_set: Option<bool>,
pub test4_not_set: Option<bool>,
pub database: DatabaseSettings,
}

Expand Down Expand Up @@ -441,6 +448,7 @@ mod tests {
fn default_config_example_file_toml() -> tempfile::NamedTempFile {
let content = r#"test = "value"
test2 = 123
test3_set = false
[database.connect]
url = "test-url"
"#;
Expand All @@ -451,6 +459,7 @@ mod tests {
let content = r#"{
"test": "value",
"test2": 123,
"test3_set": false,
"database": {
"connect": {
"url": "test-url"
Expand All @@ -476,6 +485,13 @@ mod tests {
"",
),
var("TEST_SERVICE__TEST2", Some("1000"), false, "e.g. `123`"),
var(
"TEST_SERVICE__TEST3_SET",
Some("null"),
false,
"e.g. `false`",
),
var("TEST_SERVICE__TEST4_NOT_SET", Some("null"), false, ""),
var(
"TEST_SERVICE__DATABASE__CONNECT__URL",
None,
Expand All @@ -489,12 +505,14 @@ mod tests {
r#"
[anchor]: <> (anchors.envs.start)
| Variable | Required | Description | Default Value |
|-------------------------------------------|-------------|-------------|---------------|
| `TEST_SERVICE__TEST` | true | e.g. `value` | |
| `TEST_SERVICE__DATABASE__CREATE_DATABASE` | false | | `false` |
| `TEST_SERVICE__DATABASE__RUN_MIGRATIONS` | false | | `false` |
| `TEST_SERVICE__TEST2` | false | e.g. `123` | `1000` |
| Variable | Required | Description | Default Value |
|-------------------------------------------|-------------|------------------|---------------|
| `TEST_SERVICE__TEST` | true | e.g. `value` | |
| `TEST_SERVICE__DATABASE__CREATE_DATABASE` | false | | `false` |
| `TEST_SERVICE__DATABASE__RUN_MIGRATIONS` | false | | `false` |
| `TEST_SERVICE__TEST2` | false | e.g. `123` | `1000` |
| `TEST_SERVICE__TEST3_SET` | false | e.g. `false` | `null` |
| `TEST_SERVICE__TEST4_NOT_SET` | false | | `null` |
| `TEST_SERVICE__DATABASE__CONNECT__URL` | true | e.g. `test-url` | |
[anchor]: <> (anchors.envs.end)
"#
Expand Down Expand Up @@ -587,6 +605,8 @@ mod tests {
| `TEST_SERVICE__DATABASE__CREATE_DATABASE` | | | `false` |
| `TEST_SERVICE__DATABASE__RUN_MIGRATIONS` | | | `false` |
| `TEST_SERVICE__TEST2` | | e.g. `123` | `1000` |
| `TEST_SERVICE__TEST3_SET` | | e.g. `false` | `null` |
| `TEST_SERVICE__TEST4_NOT_SET` | | | `null` |
[anchor]: <> (anchors.envs.end)
"#
Expand Down

0 comments on commit 8aac825

Please sign in to comment.