rickyah / ini-parser

Read/Write an INI file the easy way!
MIT License
971 stars 241 forks source link

Q: Is SectionData.Clone() supposed to return a complete copy of the section? #100

Closed mponton closed 8 years ago

mponton commented 8 years ago

My .NET knowledge is quite limited and I'm using your library with PowerShell (thanks!).

I noticed that when I Clone() then Merge() a section, the Merge() affects the original SectionData object. I'm not sure if an issue or my lack of understanding of the Clone() method. For example, assuming this INI file:

[DEFAULT]
Recurse=True

[A]
Path=somepath
Recurse=False

[B]
Path=someotherpath

And this code:

$parserConfig = New-Object IniParser.Model.Configuration.IniParserConfiguration
$parserConfig.CaseInsensitive = $true;
$parser = New-Object IniParser.FileIniDataParser($parserConfig)
$config = $parser.ReadFile($ConfigFile)

$config.Sections | Where SectionName -ne 'DEFAULT' | % {
    $sectionName = $_.SectionName
    $section = $config['DEFAULT'].Clone()
    $section.Merge($config[$sectionName])

    # DO SOMETHING BASED ON SETTINGS
}

A.Recurse will be False as expected, but B.Recurse will ALSO be False (where I would have expected it to be True, from the DEFAULT section, since it is not present in the B section.

If this is normal behavior, do you have any suggestion to accomplish this? (e.g. merge a section with some default settings in a loop to have a final set of settings specific to that section)

Thanks and regards,

Marco

rickyah commented 8 years ago

Nope, it is supposed to create a deep copy. I'm almost finished with on the fix, you'll have it available in the development branch today

rickyah commented 8 years ago

You have the fixes available in the develop branch

mponton commented 8 years ago

Wow, thank you so much for the quick fix!

And again, thanks for the project too, very much appreciated.

Svetomech commented 8 years ago

Very much appreciated indeed. BTW, author, can you tell if I'm doing anything wrong (Lines 279/396)? It's just that the code feels messy, feels like it could be simplified. Sorry if too much off topic.