Skip to content

Read Multiple Values for Common Key into Array#43

Merged
lipkau merged 7 commits into
lipkau:masterfrom
heilkn:arrays
Jan 5, 2019
Merged

Read Multiple Values for Common Key into Array#43
lipkau merged 7 commits into
lipkau:masterfrom
heilkn:arrays

Conversation

@heilkn

@heilkn heilkn commented Jan 4, 2019

Copy link
Copy Markdown

Dear lipkau,

thank you very much for the great tool. When I wanted to use it, I encountered the problem that the ini files I have to modify contain several keys in a section with the same name. Therefore, I'd like to propose to read in those entries as an array.

Please consider adding this feature. This pullrequest is a simple approach to solve that problem. Probably, Set-IniContent and Out-IniFile needs adaption, too. I think you will know best which adjustments need to be made. However, if you are not able to do the necessary changes, please let me know what is required.

Kind regards

Konstantin

@lipkau lipkau left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Interesting suggestion.
This can be a problem, as implementations on this vary:
https://en.wikipedia.org/wiki/INI_file#Duplicate_names

Comment thread PSIni/Functions/Out-IniFile.ps1 Outdated
Comment thread PSIni/Functions/Get-IniContent.ps1 Outdated
@heilkn

heilkn commented Jan 4, 2019

Copy link
Copy Markdown
Author

Regarding the duplicate names: I think it is up to the program how to interprete the ini file. However, when dealing with ini files on the command line, you usually want to do changes to an existing file. If you know, a program does not support multiple entries, don't create any. If a program does, then the tool is of no help if it can't support that.

@lipkau lipkau left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Agreed. That's why I didn't reject the PR.

I made suggestions on how to change the code.
here is my POC for the changes:

$section = "section"
$name = "key"
$delimiter = " = "
$ini = @{section = @{}}

"value", "second value", "third value", "forth value" | % {
    $value = $_
    if ( -not $ini[$section][$name]) {
        $ini[$section][$name] = @($value)
    }
    else {
        $ini[$section][$name] += $value
    }
}
$ini[$section]["other"] = "stuff"

$ini[$section][$name]
$ini[$section][$name].gettype()

"" > dumpfile.txt

"key", "other" | % {
    $key = $_
    $ini[$section][$key] | Foreach-Object {"$key$delimiter$_"} | Add-Content -Path dumpfile.txt
}
cat dumpfile.txt

Comment thread PSIni/Functions/Out-IniFile.ps1 Outdated
@lipkau

lipkau commented Jan 4, 2019

Copy link
Copy Markdown
Owner

Also:
can you please add tests for this new behavior?

@heilkn

heilkn commented Jan 4, 2019

Copy link
Copy Markdown
Author

@lipkau Please let me know if you think more work is required on this, before it can be merged.

Comment thread Tests/PsIni.Tests.ps1 Outdated
@lipkau lipkau merged commit 2ec97f3 into lipkau:master Jan 5, 2019
@EliaSaSe

Copy link
Copy Markdown

Dear @lipkau

FYI - Similar problem as with versions 1.2.38 / 42 and 2.0.0.0:

This change introduces a breaking change between version 2.0.5 and 2.0.6.
For sections with non-duplicate keys, there will be also returned an array instead of a string.

Example:

my.ini

[Trace]
Path=C:\temp
PurgeAfterDays=5
Level=Warning

behavior 2.0.5

> Import-Module PsIni -MaximumVersion 2.0.5
> $c = Get-IniContent -FilePath C:\temp\my.ini
> $c.Trace
Name                           Value
----                           -----
Path                           C:\temp
PurgeAfterDays                 5
Level                          Warning

> $c.Trace.Path.GetType()
IsPublic IsSerial Name   BaseType
-------- -------- ----   --------
True     True     String System.Object

behavior 2.0.6

> Import-Module PsIni -MinimumVersion 2.0.6
> $c = Get-IniContent -FilePath C:\temp\my.ini
> $c.Trace

Name                           Value
----                           -----
Path                           {C:\temp}
PurgeAfterDays                 {5}
Level                          {Warning}

PS C:\Users\ese> $c.Trace.Path.GetType()

IsPublic IsSerial Name     BaseType
-------- -------- ----     --------
True     True     Object[] System.Array

Because I have to support PsIni version 2.x, I have to deal with both behaviors.
As workaround I need to test the value with -is [array]. But that's manageable.

Best regards
Elia

@lipkau

lipkau commented Jan 16, 2019

Copy link
Copy Markdown
Owner

I am surprised it works for you.
I just tried to run it with powershell -version 2 (I know it's not the same thing) and got this error

ipmo : The '.\PSIni\PSIni.psd1' module cannot be imported because its manifest contains one or more members that are not valid.

@EliaSaSe

Copy link
Copy Markdown

I expressed myself wrong: I don't have to support Powershell 2.0, I need to support PsIni in Version 2.X. I told my users: "To use my module, you need PsIni in Major/Minor-Version 2.0, because my module reads and writes ini-files."

I don't have control over all systems where my module is running. Some of my ini-related module-functions are now failling when someone updates PsIni from <=2.0.5 to 2.0.6 because of the behavior changes in this pull request.

In my next module version, I will use PsIni as nested module to be sure, which version of PsIni my module is using. This will remove the external dependency to PsIni.

lipkau added a commit that referenced this pull request Jan 16, 2019
Values should only be of type [array] when necessary. [string] otherwise

as mentioned by @EliaSaSe in
#43 (comment)
@lipkau

lipkau commented Jan 16, 2019

Copy link
Copy Markdown
Owner

should be fixed
c7b6b08

@EliaSaSe

Copy link
Copy Markdown

Cool. Thank you.

lipkau added a commit that referenced this pull request Mar 13, 2025
Read Multiple Values for Common Key into Array
lipkau added a commit that referenced this pull request Mar 13, 2025
Values should only be of type [array] when necessary. [string] otherwise

as mentioned by @EliaSaSe in
#43 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants