Skip to content

Commit

Permalink
Cleanup of 'Compare-Hashtable' (#117)
Browse files Browse the repository at this point in the history
Co-authored-by: Gael <gaelcolas@users.noreply.github.com>
  • Loading branch information
raandree and gaelcolas committed Apr 8, 2021
1 parent 26fbbea commit b1a7d33
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Formatting in all files with VSCode formatting according to the 'settings.json' file taken from Sampler
- Added yaml format config settings 'singleQuote' and 'bracketSpacing' and reformatted all yaml files according to the new settings.
- Cleanup
- Compare-Hashtable.ps1
- Node.ps1
- FileProvider.ps1
- ConvertTo-Datum.ps1
Expand Down
57 changes: 30 additions & 27 deletions source/Private/Compare-Hashtable.ps1
Original file line number Diff line number Diff line change
@@ -1,74 +1,77 @@
function Compare-Hashtable
{
[CmdletBinding()]
Param(

param (
[Parameter(Mandatory = $true)]
[hashtable]
$ReferenceHashtable,

[Parameter(Mandatory = $true)]
[hashtable]
$DifferenceHashtable,

[Parameter()]
[string[]]
$Property = ($ReferenceHashtable.Keys + $DifferenceHashtable.Keys | Select-Object -Unique)
)

Write-Debug "Compare-Hashtable -Ref @{$($ReferenceHashtable.keys -join ';')} -Diff @{$($DifferenceHashtable.keys -join ';')} -Property [$($Property -join ', ')]"
#Write-Debug "REF:`r`n$($ReferenceHashtable|ConvertTo-JSON)"
#Write-Debug "DIFF:`r`n$($DifferenceHashtable|ConvertTo-JSON)"
Write-Debug -Message "Compare-Hashtable -Ref @{$($ReferenceHashtable.keys -join ';')} -Diff @{$($DifferenceHashtable.keys -join ';')} -Property [$($Property -join ', ')]"
#Write-Debug -Message "REF:`r`n$($ReferenceHashtable | ConvertTo-Json)"
#Write-Debug -Message "DIFF:`r`n$($DifferenceHashtable | ConvertTo-Json)"

foreach ($PropertyName in $Property)
foreach ($propertyName in $Property)
{
Write-Debug " Testing <$PropertyName>'s value"
if ( ($inRef = $ReferenceHashtable.Contains($PropertyName)) -and
($inDiff = $DifferenceHashtable.Contains($PropertyName))
)
Write-Debug -Message " Testing <$propertyName>'s value"
if (($inRef = $ReferenceHashtable.Contains($propertyName)) -and
($inDiff = $DifferenceHashtable.Contains($propertyName)))
{
if ($ReferenceHashtable[$PropertyName] -as [hashtable[]] -or $DifferenceHashtable[$PropertyName] -as [hashtable[]] )
if ($ReferenceHashtable[$propertyName] -as [hashtable[]] -or $DifferenceHashtable[$propertyName] -as [hashtable[]])
{
if ( (Compare-Hashtable -ReferenceHashtable $ReferenceHashtable[$PropertyName] -DifferenceHashtable $DifferenceHashtable[$PropertyName]) )
if ((Compare-Hashtable -ReferenceHashtable $ReferenceHashtable[$propertyName] -DifferenceHashtable $DifferenceHashtable[$propertyName]))
{
Write-Debug " Skipping $PropertyName...."
Write-Debug -Message " Skipping $propertyName...."
# If Compae returns something, they're not the same
Continue
continue
}
}
else
{
Write-Debug "Comparing: $($ReferenceHashtable[$PropertyName]) With $($DifferenceHashtable[$PropertyName])"
if ($ReferenceHashtable[$PropertyName] -ne $DifferenceHashtable[$PropertyName])
Write-Debug -Message "Comparing: $($ReferenceHashtable[$propertyName]) With $($DifferenceHashtable[$propertyName])"
if ($ReferenceHashtable[$propertyName] -ne $DifferenceHashtable[$propertyName])
{
[PSCustomObject]@{
SideIndicator = '<='
PropertyName = $PropertyName
Value = $ReferenceHashtable[$PropertyName]
PropertyName = $propertyName
Value = $ReferenceHashtable[$propertyName]
}

[PSCustomObject]@{
SideIndicator = '=>'
PropertyName = $PropertyName
Value = $DifferenceHashtable[$PropertyName]
PropertyName = $propertyName
Value = $DifferenceHashtable[$propertyName]
}
}
}
}
else
{
Write-Debug " Property $PropertyName Not in one Side: Ref: [$($ReferenceHashtable.Keys -join ',')] | [$($DifferenceHashtable.Keys -join ',')]"
Write-Debug -Message " Property $propertyName Not in one Side: Ref: [$($ReferenceHashtable.Keys -join ',')] | [$($DifferenceHashtable.Keys -join ',')]"
if ($inRef)
{
Write-Debug "$PropertyName found in Reference hashtable"
Write-Debug -Message "$propertyName found in Reference hashtable"
[PSCustomObject]@{
SideIndicator = '<='
PropertyName = $PropertyName
Value = $ReferenceHashtable[$PropertyName]
PropertyName = $propertyName
Value = $ReferenceHashtable[$propertyName]
}
}
else
{
Write-Debug "$PropertyName found in Difference hashtable"
Write-Debug -Message "$propertyName found in Difference hashtable"
[PSCustomObject]@{
SideIndicator = '=>'
PropertyName = $PropertyName
Value = $DifferenceHashtable[$PropertyName]
PropertyName = $propertyName
Value = $DifferenceHashtable[$propertyName]
}
}
}
Expand Down

0 comments on commit b1a7d33

Please sign in to comment.