phil-factor / psyaml Goto Github PK
View Code? Open in Web Editor NEWA Powershell module to convert YAML documents to and from PowerShell objects
License: MIT License
A Powershell module to convert YAML documents to and from PowerShell objects
License: MIT License
I have the following generic logic:
import-module psyaml
## local variables
$my_arr = @()
$my_hash = @{}
$blahs = @('one', 'two', 'three')
$something = @(@{Name = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'})
## generate output
foreach ($someValue in $something) {
foreach ($blah in $blahs) {
$my_hash += @{$blah = $someValue.Name}
}
}
$my_arr += @{'zzzz' = $my_hash}
## convert to yaml
$my_arr | ConvertTo-YAML
I end up getting output that looks like:
---
zzzz:
one: >
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
three: >
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
two: >
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'PSYaml'. Try Get-PSRepository to see all available registered module repositories.
Getting this when trying to install the module
Looks like a find/replace gone awry in ConvertTo-Yaml.ps1. Multiple references to Select-Object-Object and Where-Object-Object cmdlets which of course do not exist.
ConvertFrom-Yaml
is converting strings to datetime.
I understand that '12/31/1999' looks like a date format, but i am passing it in as a string because i want the value to actually be '12/31/1999'
# Test YAML
$y = @"
test:
tag: 12/31/1999
@"
# Converts to PS object
$PSObj = $y | ConvertToYaml
# Outputs the results
C:\> $PSObj['test']
Name Value
---- -----
tag 12/31/1999 12:00:00 AM
# Shows the type
C:\> $PSObj['test'].tag.gettype()
IsPublic IsSerial Name
-------- -------- ----
True True DateTime
I have been able to test this on CentOS 7 and it works fine - unlike powershell-yaml which does not work on Powershell Core.
Would it be possible to publish this to the PowerShell gallery?
This can be worked around by using -Raw
on the Get-Content
Cmdlet, but it would be nice if the Cmdlets would behave the same as the original ones for Json, Csv and Xml, which seem to have no issue with string[]
as returned from Get-Content
.
Hi,
Can you please publish to https://www.powershellgallery.com/packages/PSYaml/
I really want to use it in my automatic process without doing a clone.
Currently there is only the version 1.0.2
Also It could help if you create tags on master
git tag -a 1.0.3 -m 1.0.3
Git push --tags
Big thanks
Hello!
I'm using this module for the first time, following the recommend initial load code on your website.
I find that when the module initializes, it's trying to find the YamlDotNet assembly in the wrong path.
VERBOSE: Performing the operation "Set Alias" on target "Name: nuget Value:
C:\Users\luke.ryan\Documents\WindowsPowerShell\Modules\PSYaml\nuget.exe".
Add-Type : Cannot bind parameter 'Path' to the target. Exception setting "Path": "Cannot find path
'C:\Users\luke.ryan\Documents\WindowsPowerShell\Modules\PSYaml\YAMLDotNet\YamlDotNet.4.2.1\lib\dotnet\yamldotnet.dll'
because it does not exist."
At C:\Users\luke.ryan\Documents\WindowsPowerShell\Modules\psyaml\PSYaml.psm1:44 char:20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
convertfrom-yaml does not preserve single-element arrays.
Example:
# Create a hash with two key-value pairs. Both values are arrays, but one of them
# has just a single element
$j = [ordered]@{
apac = @('Australia')
emea = @('Italy', 'Finland')
}
#Confirm that both values are arrays
PS C:\> $j.apac.gettype().name
Object[]
PS C:\> $j.emea.gettype().name
Object[]
# Convert to yaml. Good so far, we still have two arrays/sequences.
PS C:\> $j | convertto-yaml
---
apac:
- 'Australia'
emea:
- 'Italy'
- 'Finland'
# Convert back to a PowerShell object and we lose one of the arrays.
# Now we have one array and one scalar. We should have two arrays.
PS C:\> $j = $j | convertto-yaml | convertfrom-yaml
PS C:\> $j.apac.gettype().name
String
PS C:\> $j.emea.gettype().name
Object[]
When converting a YAML document containing strings that could represent numbers or dates, ConvertFrom-YAML
will always convert them to numbers or dates.
Install-Package
ConvertFrom-Yaml
String values that look like numbers are dates are converted to number or date objects:
> $fromYaml = ConvertFrom-Yaml -YamlString @"
one: thing
two: "2"
three: "2014-04-01"
"@
> $fromYaml.two; $fromYaml.two.GetType().Name
2
Int32
> $fromYaml.three; $fromYaml.three.GetType().Name
Tuesday, April 1, 2014 12:00:00 AM
DateTime
I expected that, just like with the native ConvertFrom-Json
cmdlet, string values are preserved as strings, rather than converted to numbers or dates, as long as the values are wrapped in double quotes.
Here is similar output for the ConvertFrom-Json
cmdlet, showing that strings are preserved:
> $fromJson = ConvertFrom-Json -InputObject '{"one": "thing", "two": "2", "three": "2014-04-01"}'
> $fromJson.two; $fromJson.two.GetType().Name
2
String
> $fromJson.three; $fromJson.three.GetType().Name
2014-04-01
String
For what it's worth, other YAML converters had my expected output, where strings are preserved. For instance, here is the PyYAML
module for Python converting a YAML string into a Python dict:
>>> import yaml
>>> from_yaml = yaml.load("""
... one: thing
... two: "2"
... three: "2014-04-01"
... """)
>>> from_yaml['two']; type(from_yaml['two'])
'2'
<class 'str'>
>>> from_yaml['three']; type(from_yaml['three'])
'2014-04-01'
<class 'str'>
Happily, there is a workaround for anyone else who might run into this. PSYaml supports YAML tags, as documented in the readme. If you have control over the input YAML document, you can prepend the !!str
tag to the value that should remain a string, like so:
> $fromTaggedYaml = ConvertFrom-Yaml -YamlString @"
one: thing
two: !!str "2"
three: !!str "2014-04-01"
"@
> $fromTaggedYaml.two; $fromTaggedYaml.two.GetType().Name
2
String
> $fromTaggedYaml.three; $fromTaggedYaml.three.GetType().Name
2014-04-01
String
Hi, apologise for the extra noise but I came across the usage of Set-StrictMode
in your tests like https://github.com/Phil-Factor/PSYaml/blob/master/Tests/PSYaml.ConvertFrom.Tests.ps1#L64. Your description indicates that the -Version
refers to the PowerShell version it is testing against but it's my understanding that StrictMode has 1.0, 2.0, and latest which are specific rule sets and not PS version compatibilities. Am I misunderstanding the use of Set-StrictMode
here or are those extra tests not doing what they should be doing?
Seems that this module won't parse comments? If it's the first line, it causes an exception. If you add a comment further down, it seems to stop at the commented line, and not parse any further.
You could try this example to see.
Would you be able to create a release? I can get the module more easily that way.
invoke-webrequest -uri http://github.com/phil-factor/psyaml/0.1.2.zip -outfile psyaml.zip
Given a file with this content:
- test1
- - test2
- test3
the expected output from piping this file's content to ConvertFrom-Yaml would be a list with 'test1' and a nested list with 'test2' and 'test3'. This should be the proper formatting for nested lists in YAML and converting this YAML file in Python, with the PyYAML module, exhibits this behavior.
However, the ConvertFrom-Yaml cmdlet returns a list with 'test1', 'test2', and 'test3'.
Hi there:
Found a small issue in CovertTo-Yaml.ps1:
# if it is null return null
If ( !($inputObject) )
Should be:
# if it is null return null
If ( $inputObject -eq $null )
Reason:
If the object itself is "$false", it will return an empty yaml value
How to reproduce:
$test = [PSCustomObject]@{TestValue = $false }
ConvertTo-Yaml $test
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.