Skip to content

Commit

Permalink
Resolves JSON tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hunt committed Jan 5, 2024
1 parent 63cd49f commit 70f2f3b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/private/ConvertFrom-PSCustomObject.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
}
27 changes: 27 additions & 0 deletions src/private/Import-JsonConfigData.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}

}
5 changes: 1 addition & 4 deletions src/public/Import-ConfigData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/Import-ConfigData.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 <type> file' -ForEach @(
Expand Down
2 changes: 1 addition & 1 deletion test/test3/TestConfig.psd1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
Plan = @(
plan = @(
@{
label = "google"
url = "https://www.google.com"
Expand Down

0 comments on commit 70f2f3b

Please sign in to comment.