From 70f2f3b6c0cce713df99904287af00b0e7e29fd0 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Fri, 5 Jan 2024 17:00:26 -0500 Subject: [PATCH] Resolves JSON tests --- src/private/ConvertFrom-PSCustomObject.ps1 | 23 ++++++++++++++++++ src/private/Import-JsonConfigData.ps1 | 27 ++++++++++++++++++++++ src/public/Import-ConfigData.ps1 | 5 +--- test/Import-ConfigData.Tests.ps1 | 24 +++++++++++++++++++ test/test3/TestConfig.psd1 | 2 +- 5 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 src/private/ConvertFrom-PSCustomObject.ps1 diff --git a/src/private/ConvertFrom-PSCustomObject.ps1 b/src/private/ConvertFrom-PSCustomObject.ps1 new file mode 100644 index 0000000..1a35634 --- /dev/null +++ b/src/private/ConvertFrom-PSCustomObject.ps1 @@ -0,0 +1,23 @@ +function ConvertFrom-PSCustomObject ($Object) { + + foreach ($item in $Object) { + + $itemType = $item.GetType() + + if ($itemType.Name -eq 'PSCustomObject') { + $keys = $item | Get-Member -MemberType NoteProperty + + $hash = @{} + foreach ($key in $keys) { + $name = $key.Name + $value = @(ConvertFrom-PSCustomObject $item.$name) + $hash.Add($name, $value) + } + $hash | Write-Output + } + + if ($itemType.IsValueType -or $itemType.Name -eq 'String') { + $item | Write-Output + } + } +} diff --git a/src/private/Import-JsonConfigData.ps1 b/src/private/Import-JsonConfigData.ps1 index e69de29..27d0a9e 100644 --- a/src/private/Import-JsonConfigData.ps1 +++ b/src/private/Import-JsonConfigData.ps1 @@ -0,0 +1,27 @@ +function Import-JsonConfigData { + [CmdletBinding()] + param ( + [Parameter(Mandatory, + Position = 0, + ValueFromPipeline, + ValueFromPipelineByPropertyName)] + [Alias("PSPath")] + [ValidateNotNullOrEmpty()] + [string] + $Path + ) + + $content = Get-Content -Path $Path -Raw + + switch ($PSVersionTable.PSVersion.Major) { + 5 { + $psObject = $content | ConvertFrom-Json + ConvertFrom-PSCustomObject $psObject + break + } + Default { + $content | ConvertFrom-Json -AsHashtable + } + } + +} \ No newline at end of file diff --git a/src/public/Import-ConfigData.ps1 b/src/public/Import-ConfigData.ps1 index d750091..fe613c0 100644 --- a/src/public/Import-ConfigData.ps1 +++ b/src/public/Import-ConfigData.ps1 @@ -57,10 +57,7 @@ function Import-ConfigData { '.toml' { Import-TomlConfigData -Path $Path; break } '.yaml' { Import-YamlConfigData -Path $Path; break } '.yml' { Import-YamlConfigData -Path $Path; break } - '.json' { - $content = Get-Content -Path $Path -Raw - $content | ConvertFrom-Json -AsHashtable; break - } + '.json' { Import-JsonConfigData -Path $Path; break } } } diff --git a/test/Import-ConfigData.Tests.ps1 b/test/Import-ConfigData.Tests.ps1 index b2bdbc0..fb25703 100644 --- a/test/Import-ConfigData.Tests.ps1 +++ b/test/Import-ConfigData.Tests.ps1 @@ -4,6 +4,30 @@ BeforeAll { } +Describe 'ConvertFrom-PSCustomObject' { + Context 'Object Array' { + BeforeAll { + . "$PSScriptRoot/../publish/Import-ConfigData/private/ConvertFrom-PSCustomObject.ps1" + } + + AfterAll { + Remove-Item "function:/ConvertFrom-PSCustomObject" + } + + It 'Should return an array of two objects' { + $sut = Get-Content -Path "$PSScriptRoot/Test2/TestConfig.json" -Raw | ConvertFrom-Json + $result = ConvertFrom-PSCustomObject $sut + $result.ObjectArray.Count | Should -Be 2 + } + + It 'Should return an array of one object' { + $sut = Get-Content -Path "$PSScriptRoot/Test3/TestConfig.json" -Raw | ConvertFrom-Json + $result = ConvertFrom-PSCustomObject $sut + $result.plan.Count | Should -Be 1 + } + } +} + Describe 'Import-ConfigData' { Context 'Simple Object' { It 'Should import a file' -ForEach @( diff --git a/test/test3/TestConfig.psd1 b/test/test3/TestConfig.psd1 index 75eee34..6df7ae8 100644 --- a/test/test3/TestConfig.psd1 +++ b/test/test3/TestConfig.psd1 @@ -1,5 +1,5 @@ @{ - Plan = @( + plan = @( @{ label = "google" url = "https://www.google.com"