-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-YamlFlowText.ps1
More file actions
146 lines (138 loc) · 4.87 KB
/
Copy pathConvertTo-YamlFlowText.ps1
File metadata and controls
146 lines (138 loc) · 4.87 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
function ConvertTo-YamlFlowText {
<#
.SYNOPSIS
Iteratively renders one emission graph in flow form.
#>
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory)]
[pscustomobject] $Node,
[Parameter(Mandatory)]
[AllowEmptyCollection()]
[System.Collections.Generic.HashSet[long]] $EmittedReferences
)
$root = [pscustomobject]@{ Value = '' }
$stack = [System.Collections.Generic.Stack[object]]::new()
$stack.Push([pscustomobject]@{
Node = $Node
Holder = $root
State = 'Start'
Index = 0
Prefix = ''
Parts = $null
Child = $null
KeyText = ''
})
while ($stack.Count -gt 0) {
$frame = $stack.Peek()
if ($frame.State -eq 'Start') {
if ($frame.Node.ReferenceId -ne 0 -and
$EmittedReferences.Contains($frame.Node.ReferenceId)) {
$frame.Holder.Value = "*$($frame.Node.Anchor)"
[void] $stack.Pop()
continue
}
if ($frame.Node.ReferenceId -ne 0) {
[void] $EmittedReferences.Add($frame.Node.ReferenceId)
}
$frame.Prefix = Get-YamlEmissionPrefix -Node $frame.Node
if (-not [string]::IsNullOrEmpty($frame.Prefix)) {
$frame.Prefix += ' '
}
if ($frame.Node.Kind -eq 'Scalar') {
$text = if ($frame.Node.Style -eq 'Plain') {
$frame.Node.Value
} else {
ConvertTo-YamlQuotedText -Value $frame.Node.Value
}
$frame.Holder.Value = $frame.Prefix + $text
[void] $stack.Pop()
continue
}
$frame.Parts = [System.Collections.Generic.List[string]]::new()
$frame.State = if ($frame.Node.Kind -eq 'Sequence') {
'Sequence'
} else {
'MappingKey'
}
continue
}
if ($frame.State -eq 'Sequence') {
if ($frame.Index -ge $frame.Node.Items.Count) {
$frame.Holder.Value = $frame.Prefix + '[{0}]' -f (
$frame.Parts -join ', '
)
[void] $stack.Pop()
continue
}
$frame.Child = [pscustomobject]@{ Value = '' }
$frame.State = 'SequenceValue'
$stack.Push([pscustomobject]@{
Node = $frame.Node.Items[$frame.Index]
Holder = $frame.Child
State = 'Start'
Index = 0
Prefix = ''
Parts = $null
Child = $null
KeyText = ''
})
continue
}
if ($frame.State -eq 'SequenceValue') {
$frame.Parts.Add($frame.Child.Value)
$frame.Index++
$frame.State = 'Sequence'
continue
}
if ($frame.State -eq 'MappingKey') {
if ($frame.Index -ge $frame.Node.Entries.Count) {
$frame.Holder.Value = $frame.Prefix + '{{{0}}}' -f (
$frame.Parts -join ', '
)
[void] $stack.Pop()
continue
}
$frame.Child = [pscustomobject]@{ Value = '' }
$frame.State = 'MappingKeyValue'
$stack.Push([pscustomobject]@{
Node = $frame.Node.Entries[$frame.Index].Key
Holder = $frame.Child
State = 'Start'
Index = 0
Prefix = ''
Parts = $null
Child = $null
KeyText = ''
})
continue
}
if ($frame.State -eq 'MappingKeyValue') {
$frame.KeyText = $frame.Child.Value
$frame.Child = [pscustomobject]@{ Value = '' }
$frame.State = 'MappingValue'
$stack.Push([pscustomobject]@{
Node = $frame.Node.Entries[$frame.Index].Value
Holder = $frame.Child
State = 'Start'
Index = 0
Prefix = ''
Parts = $null
Child = $null
KeyText = ''
})
continue
}
if ($frame.State -eq 'MappingValue') {
$explicitKey = (
Get-YamlEmissionImplicitKeyLength -RenderedText $frame.KeyText
) -gt 1024
$keyPrefix = if ($explicitKey) { '? ' } else { '' }
$frame.Parts.Add("$keyPrefix$($frame.KeyText)`: $($frame.Child.Value)")
$frame.Index++
$frame.State = 'MappingKey'
}
}
$root.Value
}