Skip to content

Commit

Permalink
Rogue object-object suddenly reappeared
Browse files Browse the repository at this point in the history
Strange reversions going on.  This hopefully allows a complex object to be converted repeatedly back and forth between YAML and PowerShell without morphing
  • Loading branch information
Phil-Factor authored Mar 27, 2019
1 parent 048aa7c commit d667a91
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions PSYaml/Public/ConvertTo-Yaml.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ function ConvertTo-Yaml
$array=@()
$array+=Get-Process wi* | Select-Object Handles,NPM,PM,WS,VM,CPU,Id,ProcessName
ConvertTo-YAML $array
.PARAMETER Object
the object that you want scripted out
.PARAMETER Depth
Expand Down Expand Up @@ -38,11 +37,11 @@ function ConvertTo-Yaml
# if it is null return null
If ( !($inputObject) )
{
$p += 'null'
return $p

return ''
}

if ($NestingLevel -eq 0) { '---' }
if ($NestingLevel -eq 0) {$Output= "---`r`n" } else {$output=''}

$padding = [string]' ' * $NestingLevel # lets just create our left-padding for the block
try
Expand Down Expand Up @@ -108,7 +107,7 @@ function ConvertTo-Yaml
}
write-verbose "$($padding)Type:='$Type', Object type:=$($inputObject.GetType().Name), BaseName:=$($inputObject.GetType().BaseType.Name) "

switch ($Type)
$output += switch ($Type)
{
'ScriptBlock'{ "{$($inputObject.ToString())}" }
'InnerXML' { "|`r`n" + ($inputObject.OuterXMl.Split("`r`n") | ForEach-Object{ "$padding$_`r`n" }) }
Expand Down Expand Up @@ -216,7 +215,7 @@ function ConvertTo-Yaml
("$($inputObject.GetEnumerator() | Foreach-Object {
if ($_.Value.GetType().Name -eq 'String'){$increment=2} else {$Increment=1}
"`r`n$padding $($_.Name): " +
(ConvertTo-YAML -inputObject $_.Value -depth $depth -NestingLevel ($NestingLevel + 1))
(ConvertTo-YAML -inputObject $_.Value -depth $depth -NestingLevel ($NestingLevel + $increment))
})")
}
'Dictionary`2'{
Expand All @@ -227,16 +226,17 @@ function ConvertTo-Yaml
}
'PSObject' { ("$($inputObject.PSObject.Properties | Foreach-Object { "`r`n$padding $($_.Name): " + (ConvertTo-YAML -inputObject $_ -depth $depth -NestingLevel ($NestingLevel + 1)) })") }
'generic' { "$($inputObject.Keys | Foreach-Object { "`r`n$padding $($_): $(ConvertTo-YAML -inputObject $inputObject.$_ -depth $depth -NestingLevel ($NestingLevel + 1))" })" }
'Object' { ("$($inputObject | Get-Member -membertype properties | Select-Object-Object name | Foreach-Object { "`r`n$padding $($_.name): $(ConvertTo-YAML -inputObject $inputObject.$($_.name) -depth $NestingLevel -NestingLevel ($NestingLevel + 1))" })") }
'XML' { ("$($inputObject | Get-Member -membertype properties | Where-Object-object { @('xml', 'schema') -notcontains $_.name } | Select-Object-Object name | Foreach-Object { "`r`n$padding $($_.name): $(ConvertTo-YAML -inputObject $inputObject.$($_.name) -depth $depth -NestingLevel ($NestingLevel + 1))" })") }
'DataRow' { ("$($inputObject | Get-Member -membertype properties | Select-Object-Object name | Foreach-Object { "`r`n$padding $($_.name): $(ConvertTo-YAML -inputObject $inputObject.$($_.name) -depth $depth -NestingLevel ($NestingLevel + 1))" })") }
'Object' { ("$($inputObject | Get-Member -membertype properties | Select-Object name | Foreach-Object { "`r`n$padding $($_.name): $(ConvertTo-YAML -inputObject $inputObject.$($_.name) -depth $NestingLevel -NestingLevel ($NestingLevel + 1))" })") }
'XML' { ("$($inputObject | Get-Member -membertype properties | Where-Object { @('xml', 'schema') -notcontains $_.name } | Select-Object name | Foreach-Object { "`r`n$padding $($_.name): $(ConvertTo-YAML -inputObject $inputObject.$($_.name) -depth $depth -NestingLevel ($NestingLevel + 1))" })") }
'DataRow' { ("$($inputObject | Get-Member -membertype properties | Select-Object name | Foreach-Object { "`r`n$padding $($_.name): $(ConvertTo-YAML -inputObject $inputObject.$($_.name) -depth $depth -NestingLevel ($NestingLevel + 1))" })") }
<#
'SqlDataReader'{ $all = $inputObject.FieldCount
while ($inputObject.Read()) {for ($i = 0; $i -lt $all; $i++)
{"`r`n$padding $($Reader.GetName($i)): $(ConvertTo-YAML -inputObject $($Reader.GetValue($i)) -depth $depth -NestingLevel ($NestingLevel+1))"}}
#>
default { "'$inputObject'" }
}
return $Output
}
catch
{
Expand Down

0 comments on commit d667a91

Please sign in to comment.