-
Notifications
You must be signed in to change notification settings - Fork 38
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
merge() with a leading join() doesn't overwrite #128
Comments
I also wanted to load configuration variables from a file which should overwrite the defaults. But this doesn't seem to work either, although I basically used this example from the docs. so I think the here is a test which is failing: #[test]
fn file_overwrites_defaults_with_jail() {
#[derive(Debug, Deserialize, PartialEq)]
pub struct Config {
pub port: u16,
}
figment::Jail::expect_with(|jail| {
jail.create_file("config.toml", r#"
port = 9999
"#)?;
let config: Config = Figment::new()
.join(("port", 8080))
.merge(Toml::file("config.toml"))
.extract().expect("extract result");
assert_eq!(config, Config {
port: 9999,
});
Ok(())
});
} why is the port not overwritten by |
Hello! I was looking at this issue as I seem to have the same problem. But I made an interesting observation when working with @jaads example test. At first glance, the issue seems to be in the merge/join logic (from looking at the code, the term #[test]
fn file_overwrites_defaults_with_jail() {
#[derive(Debug, Deserialize, PartialEq)]
pub struct Config {
pub port: u16,
}
figment::Jail::expect_with(|jail| {
let config: Config = Figment::new()
.join(("port", 8080))
// Note the inlining of the config file vs `Toml::file("config.toml")` before
.merge(("port", 9999))
.extract().expect("extract result");
// Test passes!
assert_eq!(config, Config {
port: 9999,
});
Ok(())
});
} This suggests that there is a problem in the interaction between |
I have found the issue with the example test case that you provided @jaads . At first glance it seems to be an issue with the a good way to visualise this is using the If we visualise the data shape of #[test]
fn visualise_tuple() {
let config = ("port", 8080);
let shape = dbg!(&config.data());
} We see the following (cleaned up for readability): &config.data() = Ok(
{
Profile(string: "global"): {
"port": Num(8080),
},
},
) Note the #[test]
fn visualise_toml_data() {
let toml = Data::<Toml>::string("port = 9999");
let shape = dbg!(&toml.data());
} we see (again, cleaned up for readability): &toml.data() = Ok(
{
Profile(string: "default"): {
"port": Num(9999),
},
},
) Where you can see that the value is under a different profile. If you use the What actually happens inside the test case then?first, an empty {
// No data here
} The first set of port information is joined ( {
// Data is joined against an empty dictionary, so the port is set
Profile(string: "global"): {
"port": Num(8080),
},
} Then we merge the toml file {
// Original data was not touched because it has the `global` profile.
Profile(string: "global"): {
"port": Num(8080),
},
// Because the data was merged with a different profile (`default`), the data was not combined
Profile(string: "default"): {
"port": Num(9999),
},
} So the data never actually got combined! But how do we get Default combining behaviour of
|
nice thanks for the update 👍 |
btw, I overcame this issue by using this approach for declaring default values for host and port. the issue still exists though I'd say |
I'm trying to load configuration variables from various sources using figment. The module looks like this:
When I'm tried to test the
load_condig
function, it seems that the environment variables don't get loaded. Here is my test moduleThe first test run successfully. But the second test fails - the port stays unchanged although I expected it to be overwritten in the figment with 8888.
What I am doing wrong? It seems that the env vars don't get loaded or not overwritten correctly. I also tested with Jail as follows, but with the same result with the port variables don't get overwritten.
The text was updated successfully, but these errors were encountered: