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

Non-deterministic results for overlapping overrides #442

Open
neilisaac opened this issue Jul 4, 2023 · 4 comments
Open

Non-deterministic results for overlapping overrides #442

neilisaac opened this issue Jul 4, 2023 · 4 comments

Comments

@neilisaac
Copy link

Source::collect returns a config::Map which is a HashMap. HashMap traversals are unordered, so the processing order of a Source is non-deterministic.

This can be an issue if multiple values in the Source affect the same data:

    #[derive(Debug, Serialize, Deserialize)]
    struct TestConfig {
        nested: std::ops::Range<i32>,
    }


impl Source for ExampleSource {
    fn collect(&self) -> Result<config::Map<String, config::Value>, config::ConfigError> {
        Ok([
            ("nested".to_string(), config::Value::new(None, config::ValueKind::String("{\"start\": 1, \"end\": 3}".to_string())),
            ("nested.end".to_string(), config::Value::new(None, config::ValueKind::String("5".to_string()))),
        ].into_iter().collect())
    }
}

This can result in deserializing 1..3 or 1..5 randomly.

@matthiasbeyer
Copy link
Collaborator

I agree that this can be an issue.

From what I see the solution for this would be to use an BTreeMap or something similar in the implementation, right?

@neilisaac
Copy link
Author

neilisaac commented Jul 7, 2023

Yes, although for compatibility you may want to keep the trait as is, and sort afterwards. Alternatively a breaking api change to BTreeMap would mean that the implementations need to handle determinism (ex. by sorting env vars for env var source).

@matthiasbeyer
Copy link
Collaborator

So config-rs is in 0.x.y-version, so a breaking API wouldn't be that much of a problem, at least semver-wise.

Could you maybe provide a PR with a (failing, of course) testcase for above example? I think we can then go from there... changing the impl from HashMap to BTreeMap should be a rather trivial patch I think...

@haydenflinner
Copy link

Testing something non-deterministic sounds like a recipe for a flaky test, maybe unnecessary in this case. If the patch is straightforward I think it makes sense to go straight to that.

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

No branches or pull requests

3 participants