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

fix(core/config): inherit is ignored when explicit default is present #147

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PhotonQuantum
Copy link
Member

@PhotonQuantum PhotonQuantum commented Aug 16, 2022

Fix a bug when both inherit and explicit default are set. Inherited fields was overwritten by explicit defaults.

Example

Source

    #[derive(Deserialize, Config)]
    struct Nested {
        #[config(default = "false")]
        b: bool,
        c: usize,
    }

    #[derive(Deserialize, Config)]
    struct Config {
        #[config(inherit, default = r#"{ "c": 42 }"#)]
        a: Nested,
    }

Current Generated Code (Wrong)

Notice how the second insertion of key "a" overwrites the first one.

impl crate::utils::ConfigDefault for ConfigWithInheritAndExplicitDefaultsTwo {
    fn config_defaults() -> crate::utils::serde_json::Value {
        crate::utils::serde_json::Value::Object({
            {
                let mut dict = crate::utils::serde_json::Map::new();
                dict.insert("a".to_string(), <Nested as crate::utils::ConfigDefault>::config_defaults());
                dict.insert("a".to_string(), crate::utils::serde_json::from_str::<crate::utils::serde_json::Value>("{ \"c\": 42 }").expect("Given string literal is not a valid json value."));
                dict
            }
        })
    }
}

Stacked PR

This PR is stacked on top of PR #145. It shouldn't be merged until #145 is merged.

@netlify
Copy link

netlify bot commented Aug 16, 2022

Deploy Preview for stargazer-book canceled.

Name Link
🔨 Latest commit a345490
🔍 Latest deploy log https://app.netlify.com/sites/stargazer-book/deploys/62fbd94398a4f00009f25e07

@George-Miao George-Miao removed their request for review May 1, 2024 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant