Skip to content
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [windows-latest]
os: [windows-2022, windows-latest]

steps:
- uses: actions/checkout@v3
Expand All @@ -40,8 +40,8 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
os: [macos-latest, ubuntu-latest, windows-2022, windows-latest]

steps:
- uses: actions/checkout@v3
- name: Install modules
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
src/obj
src/bin
src/*/obj
src/*/bin
src/*/*/obj
src/*/*/bin

# Test results
TestResults*.xml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The ```lib``` folder contains the YamlDotNet assemblies. They are not really req

## Installation

This module is available for installation via [Powershell Gallery](http://www.powershellgallery.com/). Simply run the following command:
This module is available for installation via [Powershell Gallery](http://www.powershellgallery.com/).

```powershell
Install-Module powershell-yaml
Expand Down
51 changes: 51 additions & 0 deletions Tests/CaseSensitiveKeys.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2016-2026 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#

using namespace PowerShellYaml

# Test class demonstrating YamlKey attribute for case-sensitive YAML keys
# PowerShell class properties are case-insensitive, so we need the attribute
# to distinguish between "Test" and "test" in the YAML
class CaseSensitiveTest : YamlBase {
[YamlKey("Test")]
[string]$CapitalizedTest = ""

[YamlKey("test")]
[int]$LowercaseTest = 0
}

# Test class with mixed attribute and auto-conversion
class MixedKeysTest : YamlBase {
# Uses attribute
[YamlKey("custom-key")]
[string]$CustomProperty = ""

# Uses automatic PascalCase -> hyphenated-case conversion
[int]$AutoConvertedKey = 0
}

# Test class that will fail due to duplicate key without explicit mapping
class IWillFailDueToDuplicateKey : YamlBase {
[string]$test = ""
}

# Test class that succeeds because all duplicate keys are explicitly mapped
class IWillSucceedBecauseIHaveAMappedKey : YamlBase {
[YamlKey("test")]
[string]$test = ""

[YamlKey("Test")]
[string]$alsoTestButUppercase = ""
}
177 changes: 177 additions & 0 deletions Tests/DeepNestedConvertersClasses.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#!/usr/bin/env pwsh
# Copyright 2016-2026 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Test classes for deep nesting with custom converters

using namespace PowerShellYaml
using namespace System
using namespace System.Collections.Generic

# Custom type: IP Address (renamed to avoid conflict with System.Net.IPAddress)
class CustomIPAddress {
[byte]$Octet1 = 0
[byte]$Octet2 = 0
[byte]$Octet3 = 0
[byte]$Octet4 = 0

CustomIPAddress() {
$this.Octet1 = 0
$this.Octet2 = 0
$this.Octet3 = 0
$this.Octet4 = 0
}

[string] ToString() {
return "$($this.Octet1).$($this.Octet2).$($this.Octet3).$($this.Octet4)"
}
}

# Custom converter for IP Address
class IPAddressConverter : YamlConverter {
[bool] CanHandle([string]$tag, [Type]$targetType) {
return $targetType -eq [CustomIPAddress]
}

[object] ConvertFromYaml([object]$data, [string]$tag, [Type]$targetType) {
$ip = [CustomIPAddress]@{}

if ($data -is [string]) {
# Parse string format: "192.168.1.1"
$octets = $data -split '\.'
if ($octets.Length -ne 4) {
throw [FormatException]::new("Invalid IP address format: $data")
}
$ip.Octet1 = [byte]$octets[0]
$ip.Octet2 = [byte]$octets[1]
$ip.Octet3 = [byte]$octets[2]
$ip.Octet4 = [byte]$octets[3]
}
elseif ($data -is [System.Collections.Generic.Dictionary[string, object]]) {
# Parse dictionary format
if ($data.ContainsKey('a')) { $ip.Octet1 = [byte]$data['a'] }
if ($data.ContainsKey('b')) { $ip.Octet2 = [byte]$data['b'] }
if ($data.ContainsKey('c')) { $ip.Octet3 = [byte]$data['c'] }
if ($data.ContainsKey('d')) { $ip.Octet4 = [byte]$data['d'] }
}

return $ip
}

[object] ConvertToYaml([object]$value) {
$ip = [CustomIPAddress]$value
return @{
Value = $ip.ToString()
Tag = '!ipaddr'
}
}
}

# Custom type: Duration (simple time span)
class Duration {
[int]$Hours = 0
[int]$Minutes = 0
[int]$Seconds = 0

Duration() {
$this.Hours = 0
$this.Minutes = 0
$this.Seconds = 0
}

[string] ToString() {
return "$($this.Hours)h$($this.Minutes)m$($this.Seconds)s"
}
}

# Custom converter for Duration
class DurationConverter : YamlConverter {
[bool] CanHandle([string]$tag, [Type]$targetType) {
return $targetType -eq [Duration]
}

[object] ConvertFromYaml([object]$data, [string]$tag, [Type]$targetType) {
$duration = [Duration]::new()

if ($data -is [string]) {
# Parse string format: "2h30m15s"
if ($data -match '^(\d+)h(\d+)m(\d+)s$') {
$duration.Hours = [int]$Matches[1]
$duration.Minutes = [int]$Matches[2]
$duration.Seconds = [int]$Matches[3]
}
else {
throw [FormatException]::new("Invalid duration format: $data")
}
}
elseif ($data -is [System.Collections.Generic.Dictionary[string, object]]) {
if ($data.ContainsKey('hours')) { $duration.Hours = [int]$data['hours'] }
if ($data.ContainsKey('minutes')) { $duration.Minutes = [int]$data['minutes'] }
if ($data.ContainsKey('seconds')) { $duration.Seconds = [int]$data['seconds'] }
}

return $duration
}

[object] ConvertToYaml([object]$value) {
$duration = [Duration]$value
return @{
Value = $duration.ToString()
Tag = '!duration'
}
}
}

# Level 3: Server configuration (deepest level with converters)
class ServerConfig : YamlBase {
[string]$Hostname = ""

[YamlConverter("IPAddressConverter")]
[CustomIPAddress]$Address = $null

[int]$Port = 0

[YamlConverter("DurationConverter")]
[Duration]$Timeout = $null
}

# Level 2: Database configuration (middle level with converters)
class DatabaseConfig : YamlBase {
[string]$Name = ""

[YamlConverter("IPAddressConverter")]
[CustomIPAddress]$Host = $null

[int]$Port = 0

[ServerConfig]$PrimaryServer = $null
[ServerConfig]$ReplicaServer = $null

[YamlConverter("DurationConverter")]
[Duration]$ConnectionTimeout = $null
}

# Level 1: Application configuration (top level)
class ApplicationConfig : YamlBase {
[string]$AppName = ""
[string]$Environment = ""

[DatabaseConfig]$Database = $null

[YamlConverter("IPAddressConverter")]
[CustomIPAddress]$ApiGateway = $null

[YamlConverter("DurationConverter")]
[Duration]$RequestTimeout = $null
}
35 changes: 35 additions & 0 deletions Tests/DeepNestingClasses.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2016-2026 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#

using namespace PowerShellYaml

# Simple classes using default YamlBase implementations
# No need to implement ToDictionary/FromDictionary!

class Person : YamlBase {
[string]$Name = ""
[int]$Age = 0
}

class Family : YamlBase {
[Person]$Mom = $null
[Person]$Dad = $null
[Person[]]$Children = $null
}

class MyClass : YamlBase {
[string]$Title = ""
[Family]$Family = $null
}
36 changes: 36 additions & 0 deletions Tests/MappingStyleClasses.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env pwsh
# Copyright 2016-2026 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#

using namespace PowerShellYaml

# Test classes for mapping style
class Address : YamlBase {
[string]$Street = ""
[string]$City = ""
[string]$Zip = ""
}

class Person : YamlBase {
[string]$Name = ""
[int]$Age = 0
[Address]$Address = $null
}

class Company : YamlBase {
[string]$Name = ""
[Person]$Ceo = $null
[Person[]]$Employees = @()
}
22 changes: 22 additions & 0 deletions Tests/TaggedScalars.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2016-2026 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#

using namespace PowerShellYaml

# Test class for tagged scalar values
class TaggedScalarsTest : YamlBase {
[string]$StringValue = ""
[int]$IntValue = 0
}
Loading