|
| 1 | +function Copy-YamlMergeNode { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Deep-clones a YAML representation graph with identity memoization. |
| 5 | + #> |
| 6 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( |
| 7 | + 'PSUseShouldProcessForStateChangingFunctions', '', |
| 8 | + Justification = 'Constructs an isolated in-memory representation graph.' |
| 9 | + )] |
| 10 | + [CmdletBinding()] |
| 11 | + [OutputType([pscustomobject])] |
| 12 | + param ( |
| 13 | + [Parameter(Mandatory)] |
| 14 | + [pscustomobject] $Node, |
| 15 | + |
| 16 | + [Parameter(Mandatory)] |
| 17 | + [AllowEmptyCollection()] |
| 18 | + [System.Collections.Generic.Dictionary[int, object]] $Cache, |
| 19 | + |
| 20 | + [Parameter(Mandatory)] |
| 21 | + [pscustomobject] $State |
| 22 | + ) |
| 23 | + |
| 24 | + if ($Cache.ContainsKey($Node.Id)) { |
| 25 | + Write-Output -InputObject $Cache[$Node.Id] -NoEnumerate |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + $result = [pscustomobject]@{ Value = $null } |
| 30 | + $stack = [System.Collections.Generic.Stack[object]]::new() |
| 31 | + $stack.Push([pscustomobject]@{ |
| 32 | + Source = $Node |
| 33 | + Holder = $result |
| 34 | + Target = $null |
| 35 | + Child = $null |
| 36 | + Key = $null |
| 37 | + Index = 0 |
| 38 | + State = 'Start' |
| 39 | + }) |
| 40 | + |
| 41 | + while ($stack.Count -gt 0) { |
| 42 | + $frame = $stack.Peek() |
| 43 | + if ($frame.State -eq 'Start') { |
| 44 | + if ($Cache.ContainsKey($frame.Source.Id)) { |
| 45 | + $frame.Holder.Value = $Cache[$frame.Source.Id] |
| 46 | + [void] $stack.Pop() |
| 47 | + continue |
| 48 | + } |
| 49 | + |
| 50 | + $State.CreatedNodes++ |
| 51 | + if ($State.CreatedNodes -gt $State.MaxNodes) { |
| 52 | + throw (New-YamlMergeException -Node $frame.Source ` |
| 53 | + -ErrorId 'YamlMergeNodeLimitExceeded' -Message ( |
| 54 | + "Cloning the merged YAML graph exceeded the configured limit of $($State.MaxNodes) nodes." |
| 55 | + )) |
| 56 | + } |
| 57 | + $target = New-YamlNode -Id $State.NextId -Kind $frame.Source.Kind ` |
| 58 | + -Start $frame.Source.Start -End $frame.Source.End |
| 59 | + $State.NextId++ |
| 60 | + $target.Tag = [string] $frame.Source.Tag |
| 61 | + $target.HasUnknownTag = [bool] $frame.Source.HasUnknownTag |
| 62 | + $target.Anchor = [string] $frame.Source.Anchor |
| 63 | + $target.Value = $frame.Source.Value |
| 64 | + $target.Style = [string] $frame.Source.Style |
| 65 | + $target.IsPlainImplicit = [bool] $frame.Source.IsPlainImplicit |
| 66 | + $target.IsQuotedImplicit = [bool] $frame.Source.IsQuotedImplicit |
| 67 | + $target.MaxNumericLength = [int] $frame.Source.MaxNumericLength |
| 68 | + $Cache[$frame.Source.Id] = $target |
| 69 | + $frame.Target = $target |
| 70 | + $frame.Holder.Value = $target |
| 71 | + |
| 72 | + if ($frame.Source.Kind -eq 'Scalar') { |
| 73 | + [void] $stack.Pop() |
| 74 | + } elseif ($frame.Source.Kind -eq 'Alias') { |
| 75 | + $frame.Child = [pscustomobject]@{ Value = $null } |
| 76 | + $frame.State = 'Alias' |
| 77 | + $stack.Push([pscustomobject]@{ |
| 78 | + Source = $frame.Source.Target |
| 79 | + Holder = $frame.Child |
| 80 | + Target = $null |
| 81 | + Child = $null |
| 82 | + Key = $null |
| 83 | + Index = 0 |
| 84 | + State = 'Start' |
| 85 | + }) |
| 86 | + } elseif ($frame.Source.Kind -eq 'Sequence') { |
| 87 | + $frame.State = 'Sequence' |
| 88 | + } else { |
| 89 | + $frame.State = 'MappingKey' |
| 90 | + } |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + if ($frame.State -eq 'Alias') { |
| 95 | + $frame.Target.Target = $frame.Child.Value |
| 96 | + [void] $stack.Pop() |
| 97 | + continue |
| 98 | + } |
| 99 | + if ($frame.State -eq 'Sequence') { |
| 100 | + if ($frame.Index -ge $frame.Source.Items.Count) { |
| 101 | + [void] $stack.Pop() |
| 102 | + continue |
| 103 | + } |
| 104 | + $frame.Child = [pscustomobject]@{ Value = $null } |
| 105 | + $frame.State = 'SequenceValue' |
| 106 | + $stack.Push([pscustomobject]@{ |
| 107 | + Source = $frame.Source.Items[$frame.Index] |
| 108 | + Holder = $frame.Child |
| 109 | + Target = $null |
| 110 | + Child = $null |
| 111 | + Key = $null |
| 112 | + Index = 0 |
| 113 | + State = 'Start' |
| 114 | + }) |
| 115 | + continue |
| 116 | + } |
| 117 | + if ($frame.State -eq 'SequenceValue') { |
| 118 | + $frame.Target.Items.Add($frame.Child.Value) |
| 119 | + $frame.Index++ |
| 120 | + $frame.State = 'Sequence' |
| 121 | + continue |
| 122 | + } |
| 123 | + if ($frame.State -eq 'MappingKey') { |
| 124 | + if ($frame.Index -ge $frame.Source.Entries.Count) { |
| 125 | + [void] $stack.Pop() |
| 126 | + continue |
| 127 | + } |
| 128 | + $frame.Child = [pscustomobject]@{ Value = $null } |
| 129 | + $frame.State = 'MappingKeyValue' |
| 130 | + $stack.Push([pscustomobject]@{ |
| 131 | + Source = $frame.Source.Entries[$frame.Index].Key |
| 132 | + Holder = $frame.Child |
| 133 | + Target = $null |
| 134 | + Child = $null |
| 135 | + Key = $null |
| 136 | + Index = 0 |
| 137 | + State = 'Start' |
| 138 | + }) |
| 139 | + continue |
| 140 | + } |
| 141 | + if ($frame.State -eq 'MappingKeyValue') { |
| 142 | + $frame.Key = $frame.Child.Value |
| 143 | + $frame.Child = [pscustomobject]@{ Value = $null } |
| 144 | + $frame.State = 'MappingValue' |
| 145 | + $stack.Push([pscustomobject]@{ |
| 146 | + Source = $frame.Source.Entries[$frame.Index].Value |
| 147 | + Holder = $frame.Child |
| 148 | + Target = $null |
| 149 | + Child = $null |
| 150 | + Key = $null |
| 151 | + Index = 0 |
| 152 | + State = 'Start' |
| 153 | + }) |
| 154 | + continue |
| 155 | + } |
| 156 | + if ($frame.State -eq 'MappingValue') { |
| 157 | + $frame.Target.Entries.Add([pscustomobject]@{ |
| 158 | + Key = $frame.Key |
| 159 | + Value = $frame.Child.Value |
| 160 | + }) |
| 161 | + $frame.Index++ |
| 162 | + $frame.State = 'MappingKey' |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + Write-Output -InputObject $result.Value -NoEnumerate |
| 167 | +} |
0 commit comments