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

Feat:read section from multiple files;meger and override key value #141

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

kingbuffalo
Copy link

In my project, I have several subprocesses, and most of the .ini configurations are shared among them, with only a small portion being different.
Therefore, I need to read two configuration files, one of which is shared, and the other is used to correspond to the different configurations for each subprocess.
So the requirement is to read multiple configuration files, where a section may be configured by multiple files, and the configurations in the later files can override those in the earlier files.
This is the reason for my pull request (PR) this time.

src/lib.rs Outdated
@@ -1028,6 +1029,34 @@ impl Ini {
Ini::load_from_file_opt(filename, ParseOption::default())
}

/// Load from files;overwrite and append
pub fn load_from_files<P: AsRef<Path>>(filenames: Vec<P>) -> Result<Ini, Error> {
Copy link
Owner

Choose a reason for hiding this comment

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

filenames: &[P] would be better?

Copy link
Owner

Choose a reason for hiding this comment

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

You haven't change the type of filenames yet.

Copy link
Owner

Choose a reason for hiding this comment

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

However, filenames: IntoIter<Item=P> may also be a good choice.

Copy link
Owner

Choose a reason for hiding this comment

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

This is not fixed yet.

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
@kingbuffalo kingbuffalo closed this Dec 5, 2024
@kingbuffalo kingbuffalo reopened this Dec 5, 2024
@kingbuffalo
Copy link
Author

Sorry, I clicked the wrong button.

@davidhalter
Copy link
Contributor

I personally have kind of a strong opinion that stuff like this should not be merged. This is clearly not logic that most people want and expect. This is logic that is probably easily implemented in a function for the specific project. Why do the latter files overwrite? They could as well just have lower priority. If it is not possible to implement this with a simple function, we should probably somehow add the necessary APIs to deal with that, but that might help other users as well.

@kingbuffalo
Copy link
Author

I personally have kind of a strong opinion that stuff like this should not be merged. This is clearly not logic that most people want and expect. This is logic that is probably easily implemented in a function for the specific project. Why do the latter files overwrite? They could as well just have lower priority. If it is not possible to implement this with a simple function, we should probably somehow add the necessary APIs to deal with that, but that might help other users as well.

My project has been migrated from Go, and in the Go project, I used the library at https://github.com/go-ini/ini, which provides convenience when multiple sub-projects share the same configuration. That's why I submitted a pull request (PR).

@kingbuffalo
Copy link
Author

I personally have kind of a strong opinion that stuff like this should not be merged. This is clearly not logic that most people want and expect. This is logic that is probably easily implemented in a function for the specific project. Why do the latter files overwrite? They could as well just have lower priority. If it is not possible to implement this with a simple function, we should probably somehow add the necessary APIs to deal with that, but that might help other users as well.

Why do the latter files overwrite? I believe there is practical significance to this. For instance, imagine you are driving and the road signs indicate that you cannot turn left, but at the same location, a police officer is directing all vehicles to turn left. In this case, you would definitely turn left. It's clear that the police officer represents the later instruction. If traffic needs to follow the road signs and not turn left, the police officer simply needs to step aside. Similarly, in configuration files, if you do not want to overwrite the previous settings, you can choose not to write that particular setting. However, if you want to ensure that no matter what the previous configuration was, the latest one takes precedence, you would include it in the configuration.

@davidhalter
Copy link
Contributor

It is completely unclear from the function name & signature. For example inheritance in Python works the other way around.

@@ -1028,6 +1030,59 @@ impl Ini {
Ini::load_from_file_opt(filename, ParseOption::default())
}

/// Load from files;overwrite and append
Copy link
Owner

Choose a reason for hiding this comment

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

Write a complete document about this API.

}

#[cfg(not(feature = "case-insensitive"))]
pub fn load_from_files<P: AsRef<Path>>(filenames: &Vec<P>) -> Result<Ini, Error> {
Copy link
Owner

Choose a reason for hiding this comment

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

Write a complete document about this API.

#[cfg(not(feature = "case-insensitive"))]
pub fn load_from_files<P: AsRef<Path>>(filenames: &Vec<P>) -> Result<Ini, Error> {
let mut merged = Ini::new();
let mut section_2_props: HashMap<Option<String>, Properties> = HashMap::new();
Copy link
Owner

@zonyitoo zonyitoo Dec 9, 2024

Choose a reason for hiding this comment

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

It doesn't need to be 2 different functions to support case-insensitive, just use something like:

cfg_if! {
    if #[cfg(feature = "case-insensitive")] {
        let mut section_2_props: HashMap<Option<String>, Properties> = HashMap::new();
    } else {
        let mut section_2_props: HashMap<Option<UniCase<String>>, Properties> = HashMap::new();
    }
}

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.

4 participants