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

removed 'flatten' #57

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

removed 'flatten' #57

wants to merge 2 commits into from

Conversation

evilbluebeaver
Copy link

@evilbluebeaver evilbluebeaver commented Nov 18, 2024

I encountered an error when I tried to deserialize config

#[derive(Debug, Default, Deserialize)]
struct Config {
    pub pools: HashMap<String, PoolConfig>,
}

#[derive(Debug, Default, Deserialize)]
struct PoolConfig {
    pub endpoints: Vec<String>,
    pub size: usize,
}

using these environment variables

POOLS_FOO_ENDPOINTS="http://localhost:2379"
POOLS_FOO_SIZE = "3"
POOLS_BAR_ENDPOINTS="http://localhost:2379"
POOLS_BAR_SIZE = "3"

It causes an error
Failed to load config: Error(missing field `endpoints`)

I found that the flatten method is causing all the troubles and it is unsound even for loading all the environment variables into HashMap<String, String> because it creates a key for every underscore. So I decided to remove it and replace it with just plain keys method.
That change fixed my issue and also an issue in #49 (I uncommented double_inner_mapping_with_enum_keys and it worked flawlessly)

@evilbluebeaver
Copy link
Author

Sorry for closing and reopening pull request. It was a misclick.

Copy link
Owner

@Xuanwo Xuanwo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I somehow feel like this could be addressed by #56

#[test]
#[ignore]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid that this is valid use case we need to support.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt that it is possible to support this feature with the same algorithm that is used for structured and specified data.
And also that does not look like valid case for me. Because it creates many misleading keys if an initial key has underscores. For example, if you have a FOO_BAR_BAZ=1 it creates FOO=, FOO_BAR= and FOO_BAR_BAZ=1 which can be misinterpreted by the underlying code.

@evilbluebeaver
Copy link
Author

evilbluebeaver commented Nov 19, 2024

I also added a test for HashMap with complex keys.
I don't know how to implement this correctly where key names aren't known in advance.
So I guess my solution is a half-measure and you feel free to decline it.

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.

2 participants