-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-YamlNode.ps1
More file actions
204 lines (192 loc) · 7.83 KB
/
Copy pathConvertTo-YamlNode.ps1
File metadata and controls
204 lines (192 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
function ConvertTo-YamlNode {
<#
.SYNOPSIS
Iteratively normalizes a supported PowerShell value to an emission graph.
#>
[CmdletBinding()]
[OutputType([pscustomobject])]
param (
[Parameter(Mandatory)]
[AllowNull()]
[object] $Value,
[Parameter(Mandatory)]
[pscustomobject] $State,
[Parameter(Mandatory)]
[ValidateRange(1, 2147483647)]
[int] $Depth,
[switch] $EnumsAsStrings
)
$root = [pscustomobject]@{ Value = $null }
$stack = [System.Collections.Generic.Stack[object]]::new()
$stack.Push([pscustomobject]@{
Value = $Value
Depth = $Depth
Holder = $root
State = 'Start'
Shape = $null
Node = $null
ReferenceId = [long] 0
Index = 0
Child = $null
KeyNode = $null
Fingerprints = $null
Reserved = $false
})
while ($stack.Count -gt 0) {
$frame = $stack.Peek()
if ($frame.State -eq 'Start') {
if ($frame.Depth -gt $State.MaxDepth) {
throw (New-YamlSerializationException -ErrorId 'YamlDepthExceeded' -Message (
"The object graph exceeds the configured depth limit of $($State.MaxDepth)."
))
}
if ($frame.Reserved) {
$State.ReservedNodeCount--
}
$State.NodeCount++
if ($State.NodeCount -gt $State.MaxNodes) {
throw (New-YamlSerializationException -ErrorId 'YamlNodeLimitExceeded' -Message (
"The object graph exceeds the configured limit of $($State.MaxNodes) nodes."
))
}
$isReference = Test-YamlSerializationReference -Value $frame.Value
if ($isReference) {
$firstTime = $false
$frame.ReferenceId = $State.IdGenerator.GetId($frame.Value, [ref] $firstTime)
if (-not $firstTime) {
$State.ReferenceCounts[$frame.ReferenceId]++
if ($State.Active.Contains($frame.ReferenceId)) {
throw (New-YamlSerializationException -ErrorId 'YamlCycleDetected' -Message (
"A cycle was detected while serializing type '$($frame.Value.GetType().FullName)'."
))
}
$frame.Holder.Value = $State.NodesById[$frame.ReferenceId]
[void] $stack.Pop()
continue
}
$State.ReferenceCounts[$frame.ReferenceId] = 1
$State.ReferenceOrder.Add($frame.ReferenceId)
}
$frame.Shape = Get-YamlSerializationShape -Value $frame.Value -State $State `
-EnumsAsStrings:$EnumsAsStrings
if ($frame.Shape.Kind -eq 'Scalar') {
$frame.Holder.Value = $frame.Shape.Node
[void] $stack.Pop()
continue
}
if ($frame.Shape.Kind -eq 'Binary') {
$frame.Shape.Node.ReferenceId = $frame.ReferenceId
$State.NodesById[$frame.ReferenceId] = $frame.Shape.Node
$frame.Holder.Value = $frame.Shape.Node
[void] $stack.Pop()
continue
}
$frame.Node = New-YamlEmissionNode -Kind $frame.Shape.Kind
$frame.Node.ReferenceId = $frame.ReferenceId
$State.NodesById[$frame.ReferenceId] = $frame.Node
$frame.Holder.Value = $frame.Node
[void] $State.Active.Add($frame.ReferenceId)
if ($frame.Shape.Kind -eq 'Mapping') {
$frame.Fingerprints = [System.Collections.Generic.HashSet[string]]::new(
[System.StringComparer]::Ordinal
)
$frame.State = 'MappingKey'
} else {
$frame.State = 'Sequence'
}
continue
}
if ($frame.State -eq 'Sequence') {
if ($frame.Index -ge $frame.Shape.Values.Count) {
[void] $State.Active.Remove($frame.ReferenceId)
[void] $stack.Pop()
continue
}
$frame.Child = [pscustomobject]@{ Value = $null }
$frame.State = 'SequenceValue'
$stack.Push([pscustomobject]@{
Value = [object] $frame.Shape.Values[$frame.Index]
Depth = $frame.Depth + 1
Holder = $frame.Child
State = 'Start'
Shape = $null
Node = $null
ReferenceId = [long] 0
Index = 0
Child = $null
KeyNode = $null
Fingerprints = $null
Reserved = $true
})
continue
}
if ($frame.State -eq 'SequenceValue') {
$frame.Node.Items.Add($frame.Child.Value)
$frame.Index++
$frame.State = 'Sequence'
continue
}
if ($frame.State -eq 'MappingKey') {
if ($frame.Index -ge $frame.Shape.Values.Count) {
[void] $State.Active.Remove($frame.ReferenceId)
[void] $stack.Pop()
continue
}
$frame.Child = [pscustomobject]@{ Value = $null }
$frame.State = 'MappingKeyValue'
$stack.Push([pscustomobject]@{
Value = [object] $frame.Shape.Values[$frame.Index].Key
Depth = $frame.Depth + 1
Holder = $frame.Child
State = 'Start'
Shape = $null
Node = $null
ReferenceId = [long] 0
Index = 0
Child = $null
KeyNode = $null
Fingerprints = $null
Reserved = $true
})
continue
}
if ($frame.State -eq 'MappingKeyValue') {
$frame.KeyNode = $frame.Child.Value
$fingerprint = Get-YamlEmissionNodeFingerprint -Node $frame.KeyNode `
-Cache $State.Fingerprints `
-Active ([System.Collections.Generic.HashSet[long]]::new()) `
-Hasher $State.FingerprintHasher
if (-not $frame.Fingerprints.Add($fingerprint)) {
throw (New-YamlSerializationException -ErrorId 'YamlDuplicateKey' -Message (
'Two mapping keys normalize to the same YAML value.'
))
}
$frame.Child = [pscustomobject]@{ Value = $null }
$frame.State = 'MappingValue'
$stack.Push([pscustomobject]@{
Value = [object] $frame.Shape.Values[$frame.Index].Value
Depth = $frame.Depth + 1
Holder = $frame.Child
State = 'Start'
Shape = $null
Node = $null
ReferenceId = [long] 0
Index = 0
Child = $null
KeyNode = $null
Fingerprints = $null
Reserved = $true
})
continue
}
if ($frame.State -eq 'MappingValue') {
$frame.Node.Entries.Add([pscustomobject]@{
Key = $frame.KeyNode
Value = $frame.Child.Value
})
$frame.Index++
$frame.State = 'MappingKey'
}
}
$root.Value
}