1
# Copyright 2016 Cloudbase Solutions Srl
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
16
function Get-YamlDocuments {
19
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
23
$stringReader = new-object System.IO.StringReader($Yaml)
24
$yamlStream = New-Object "YamlDotNet.RepresentationModel.YamlStream"
25
$yamlStream.Load([System.IO.TextReader] $stringReader)
31
function Convert-ValueToProperType {
34
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
38
if (!($Value -is [string])) {
41
$types = @([int], [long], [double], [boolean], [datetime])
42
foreach($i in $types){
44
return $i::Parse($Value)
53
function Convert-YamlMappingToHashtable {
56
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
57
[YamlDotNet.RepresentationModel.YamlMappingNode]$Node
61
foreach($i in $Node.Children.Keys) {
62
$ret[$i.Value] = Convert-YamlDocumentToPSObject $Node.Children[$i]
68
function Convert-YamlSequenceToArray {
71
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
72
[YamlDotNet.RepresentationModel.YamlSequenceNode]$Node
75
$ret = [System.Collections.Generic.List[object]](New-Object "System.Collections.Generic.List[object]")
76
foreach($i in $Node.Children){
77
$ret.Add((Convert-YamlDocumentToPSObject $i))
83
function Convert-YamlDocumentToPSObject {
86
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
90
switch($Node.GetType().FullName){
91
"YamlDotNet.RepresentationModel.YamlMappingNode"{
92
return Convert-YamlMappingToHashtable $Node
94
"YamlDotNet.RepresentationModel.YamlSequenceNode" {
95
return Convert-YamlSequenceToArray $Node
97
"YamlDotNet.RepresentationModel.YamlScalarNode" {
98
return (Convert-ValueToProperType $Node.Value)
104
function Convert-HashtableToDictionary {
106
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
109
foreach($i in $($data.Keys)) {
110
$Data[$i] = Convert-PSObjectToGenericObject $Data[$i]
115
function Convert-ListToGenericList {
117
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
120
for($i=0; $i -lt $Data.Count; $i++) {
121
$Data[$i] = Convert-PSObjectToGenericObject $Data[$i]
126
function Convert-PSCustomObjectToDictionary {
128
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
129
[PSCustomObject]$Data
131
$ret = [System.Collections.Generic.Dictionary[string,object]](New-Object 'System.Collections.Generic.Dictionary[string,object]')
132
foreach ($i in $Data.psobject.properties) {
133
$ret[$i.Name] = Convert-PSObjectToGenericObject $i.Value
138
function Convert-PSObjectToGenericObject {
140
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
143
# explicitly cast object to its type. Without this, it gets wrapped inside a powershell object
144
# which causes YamlDotNet to fail
145
$data = $data -as $data.GetType().FullName
146
switch($data.GetType()) {
147
($_.FullName -eq "System.Management.Automation.PSCustomObject") {
148
return Convert-PSCustomObjectToDictionary
151
if (([System.Collections.IDictionary].IsAssignableFrom($_))){
152
return Convert-HashtableToDictionary $data
153
} elseif (([System.Collections.IList].IsAssignableFrom($_))) {
154
return Convert-ListToGenericList $data
161
function ConvertFrom-Yaml {
164
[Parameter(Mandatory=$false, ValueFromPipeline=$true)]
166
[switch]$AllDocuments=$false
172
$documents = Get-YamlDocuments -Yaml $Yaml
173
if (!$documents.Count) {
176
if($documents.Count -eq 1){
177
return Convert-YamlDocumentToPSObject $documents[0].RootNode
180
return Convert-YamlDocumentToPSObject $documents[0].RootNode
183
foreach($i in $documents) {
184
$ret += Convert-YamlDocumentToPSObject $i.RootNode
190
function ConvertTo-Yaml {
193
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
194
[System.Object]$Data,
195
[Parameter(Mandatory=$false)]
197
[switch]$Force=$false
200
$d = [System.Collections.Generic.List[object]](New-Object "System.Collections.Generic.List[object]")
209
$norm = Convert-PSObjectToGenericObject $d
211
$parent = Split-Path $OutFile
212
if(!(Test-Path $parent)) {
213
Throw "Parent folder for specified path does not exist"
215
if((Test-Path $OutFile) -and !$Force){
216
Throw "Target file already exists. Use -Force to overwrite."
218
$wrt = New-Object "System.IO.StreamWriter" $OutFile
220
$wrt = New-Object "System.IO.StringWriter"
223
$serializer = New-Object "YamlDotNet.Serialization.Serializer" 0
224
$serializer.Serialize($wrt, $norm)
231
return $wrt.ToString()
236
Export-ModuleMember -Function * -Alias *