8000 Change ConvertFrom-Toml pipe input behaviour by jborean93 · Pull Request #2 · jborean93/PSToml · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Change ConvertFrom-Toml pipe input behaviour #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog for PSToml

## v0.2.0 - 2023-05-23

+ Changed piping behaviour of `ConvertFrom-Toml` to build the final TOML string for each input rather than try and convert each string input as individual TOML entries
+ This copies the behaviour of `ConvertFrom-Json` and makes `Get-Content $path | ConvertFrom-Toml` work as people would expect

## v0.1.0 - 2023-05-22

+ Initial version of the `PSToml` module
9 changes: 8 additions & 1 deletion docs/en-US/ConvertFrom-Toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ PS C:\> $obj.foo # bar
Converts the TOML string to a Dictionary object.
The TOML keys can be accessed in the dictionary like any other dictionary object in PowerShell.

### Example 2 - Convert TOML from file to an object
```powershell
PS C:\> Get-Content pyproject.toml | ConvertFrom-Toml
```

Reads the contents of the file `pyproject.toml` and converts it from the TOML string to an object.

## PARAMETERS

### -InputObject
Expand All @@ -57,7 +64,7 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable
## INPUTS

### System.String[]
Any string piped into this cmdlet will be converted to their own `OrderedDictionary` object.
All the string inputs will be combined together as a single string to convert from a TOML string.

## OUTPUTS

Expand Down
2 changes: 1 addition & 1 deletion module/PSToml.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
RootModule = 'bin/net6.0/PSToml.dll'

# Version number of this module.
ModuleVersion = '0.1.0'
ModuleVersion = '0.2.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
45 changes: 27 additions & 18 deletions src/PSToml/ConvertFromToml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Management.Automation;
using System.Text;
using Tomlyn;
using Tomlyn.Model;

Expand All @@ -12,38 +13,46 @@ namespace PSToml;
[OutputType(typeof(OrderedDictionary))]
public sealed class ConvertFromTomlCommand : PSCmdlet
{
private StringBuilder _inputValues = new();

[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true
)]
[ValidateNotNullOrEmpty]
[AllowEmptyString]
public string[] InputObject { get; set; } = Array.Empty<string>();

protected override void ProcessRecord()
{
foreach (string toml in InputObject)
{
TomlTable table;
try
{
table = TOMLLib.ConvertFromToml(toml);
}
catch (Exception e)
{
WriteError(new ErrorRecord(
e,
"ParseError",
ErrorCategory.NotSpecified,
toml
));
continue;
}
_inputValues.AppendLine(toml);
}
}

OrderedDictionary result = ConvertToOrderedDictionary(table);
WriteObject(result);
protected override void EndProcessing()
{
string toml = _inputValues.ToString();
TomlTable table;
try
{
table = TOMLLib.ConvertFromToml(toml);
}
catch (Exception e)
{
WriteError(new ErrorRecord(
e,
"ParseError",
ErrorCategory.NotSpecified,
toml
));
return;
}

OrderedDictionary result = ConvertToOrderedDictionary(table);
WriteObject(result);
}

private OrderedDictionary ConvertToOrderedDictionary(TomlTable table)
Expand Down
8 changes: 7 additions & 1 deletion tests/ConvertFrom-Toml.Tests.ps1
5B79
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ Describe "ConvertFrom-Toml" {
$actual = ConvertFrom-Toml -InputObject 'foo' -ErrorAction SilentlyContinue -ErrorVariable err
$actual | Should -BeNullOrEmpty
$err.Count | Should -Be 1
[string]$err[0] | Should -BeLike '*Expecting ``=`` after a key instead of <eof>*'
[string]$err[0] | Should -BeLike '*Expecting ``=`` after a key instead of*'
}

It "Converts multiple input values into 1 toml object" {
$actual = "", "foo = 'bar'`n", "`n", "", "hello = 123`r`n", "`r`n" | ConvertFrom-Toml
$actual.foo | Should -Be bar
$actual.hello | Should -Be 123
}

It "Converts string type - <Scenario>" -TestCases @(
Expand Down
0