|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +# Copyright 2016-2026 Cloudbase Solutions Srl |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | +# |
| 16 | +# Test classes for deep nesting with custom converters |
| 17 | + |
| 18 | +using namespace PowerShellYaml |
| 19 | +using namespace System |
| 20 | +using namespace System.Collections.Generic |
| 21 | + |
| 22 | +# Custom type: IP Address (renamed to avoid conflict with System.Net.IPAddress) |
| 23 | +class CustomIPAddress { |
| 24 | + [byte]$Octet1 = 0 |
| 25 | + [byte]$Octet2 = 0 |
| 26 | + [byte]$Octet3 = 0 |
| 27 | + [byte]$Octet4 = 0 |
| 28 | + |
| 29 | + CustomIPAddress() { |
| 30 | + $this.Octet1 = 0 |
| 31 | + $this.Octet2 = 0 |
| 32 | + $this.Octet3 = 0 |
| 33 | + $this.Octet4 = 0 |
| 34 | + } |
| 35 | + |
| 36 | + [string] ToString() { |
| 37 | + return "$($this.Octet1).$($this.Octet2).$($this.Octet3).$($this.Octet4)" |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +# Custom converter for IP Address |
| 42 | +class IPAddressConverter : YamlConverter { |
| 43 | + [bool] CanHandle([string]$tag, [Type]$targetType) { |
| 44 | + return $targetType -eq [CustomIPAddress] |
| 45 | + } |
| 46 | + |
| 47 | + [object] ConvertFromYaml([object]$data, [string]$tag, [Type]$targetType) { |
| 48 | + $ip = [CustomIPAddress]@{} |
| 49 | + |
| 50 | + if ($data -is [string]) { |
| 51 | + # Parse string format: "192.168.1.1" |
| 52 | + $octets = $data -split '\.' |
| 53 | + if ($octets.Length -ne 4) { |
| 54 | + throw [FormatException]::new("Invalid IP address format: $data") |
| 55 | + } |
| 56 | + $ip.Octet1 = [byte]$octets[0] |
| 57 | + $ip.Octet2 = [byte]$octets[1] |
| 58 | + $ip.Octet3 = [byte]$octets[2] |
| 59 | + $ip.Octet4 = [byte]$octets[3] |
| 60 | + } |
| 61 | + elseif ($data -is [System.Collections.Generic.Dictionary[string, object]]) { |
| 62 | + # Parse dictionary format |
| 63 | + if ($data.ContainsKey('a')) { $ip.Octet1 = [byte]$data['a'] } |
| 64 | + if ($data.ContainsKey('b')) { $ip.Octet2 = [byte]$data['b'] } |
| 65 | + if ($data.ContainsKey('c')) { $ip.Octet3 = [byte]$data['c'] } |
| 66 | + if ($data.ContainsKey('d')) { $ip.Octet4 = [byte]$data['d'] } |
| 67 | + } |
| 68 | + |
| 69 | + return $ip |
| 70 | + } |
| 71 | + |
| 72 | + [object] ConvertToYaml([object]$value) { |
| 73 | + $ip = [CustomIPAddress]$value |
| 74 | + return @{ |
| 75 | + Value = $ip.ToString() |
| 76 | + Tag = '!ipaddr' |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +# Custom type: Duration (simple time span) |
| 82 | +class Duration { |
| 83 | + [int]$Hours = 0 |
| 84 | + [int]$Minutes = 0 |
| 85 | + [int]$Seconds = 0 |
| 86 | + |
| 87 | + Duration() { |
| 88 | + $this.Hours = 0 |
| 89 | + $this.Minutes = 0 |
| 90 | + $this.Seconds = 0 |
| 91 | + } |
| 92 | + |
| 93 | + [string] ToString() { |
| 94 | + return "$($this.Hours)h$($this.Minutes)m$($this.Seconds)s" |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +# Custom converter for Duration |
| 99 | +class DurationConverter : YamlConverter { |
| 100 | + [bool] CanHandle([string]$tag, [Type]$targetType) { |
| 101 | + return $targetType -eq [Duration] |
| 102 | + } |
| 103 | + |
| 104 | + [object] ConvertFromYaml([object]$data, [string]$tag, [Type]$targetType) { |
| 105 | + $duration = [Duration]::new() |
| 106 | + |
| 107 | + if ($data -is [string]) { |
| 108 | + # Parse string format: "2h30m15s" |
| 109 | + if ($data -match '^(\d+)h(\d+)m(\d+)s$') { |
| 110 | + $duration.Hours = [int]$Matches[1] |
| 111 | + $duration.Minutes = [int]$Matches[2] |
| 112 | + $duration.Seconds = [int]$Matches[3] |
| 113 | + } |
| 114 | + else { |
| 115 | + throw [FormatException]::new("Invalid duration format: $data") |
| 116 | + } |
| 117 | + } |
| 118 | + elseif ($data -is [System.Collections.Generic.Dictionary[string, object]]) { |
| 119 | + if ($data.ContainsKey('hours')) { $duration.Hours = [int]$data['hours'] } |
| 120 | + if ($data.ContainsKey('minutes')) { $duration.Minutes = [int]$data['minutes'] } |
| 121 | + if ($data.ContainsKey('seconds')) { $duration.Seconds = [int]$data['seconds'] } |
| 122 | + } |
| 123 | + |
| 124 | + return $duration |
| 125 | + } |
| 126 | + |
| 127 | + [object] ConvertToYaml([object]$value) { |
| 128 | + $duration = [Duration]$value |
| 129 | + return @{ |
| 130 | + Value = $duration.ToString() |
| 131 | + Tag = '!duration' |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +# Level 3: Server configuration (deepest level with converters) |
| 137 | +class ServerConfig : YamlBase { |
| 138 | + [string]$Hostname = "" |
| 139 | + |
| 140 | + [YamlConverter("IPAddressConverter")] |
| 141 | + [CustomIPAddress]$Address = $null |
| 142 | + |
| 143 | + [int]$Port = 0 |
| 144 | + |
| 145 | + [YamlConverter("DurationConverter")] |
| 146 | + [Duration]$Timeout = $null |
| 147 | +} |
| 148 | + |
| 149 | +# Level 2: Database configuration (middle level with converters) |
| 150 | +class DatabaseConfig : YamlBase { |
| 151 | + [string]$Name = "" |
| 152 | + |
| 153 | + [YamlConverter("IPAddressConverter")] |
| 154 | + [CustomIPAddress]$Host = $null |
| 155 | + |
| 156 | + [int]$Port = 0 |
| 157 | + |
| 158 | + [ServerConfig]$PrimaryServer = $null |
| 159 | + [ServerConfig]$ReplicaServer = $null |
| 160 | + |
| 161 | + [YamlConverter("DurationConverter")] |
| 162 | + [Duration]$ConnectionTimeout = $null |
| 163 | +} |
| 164 | + |
| 165 | +# Level 1: Application configuration (top level) |
| 166 | +class ApplicationConfig : YamlBase { |
| 167 | + [string]$AppName = "" |
| 168 | + [string]$Environment = "" |
| 169 | + |
| 170 | + [DatabaseConfig]$Database = $null |
| 171 | + |
| 172 | + [YamlConverter("IPAddressConverter")] |
| 173 | + [CustomIPAddress]$ApiGateway = $null |
| 174 | + |
| 175 | + [YamlConverter("DurationConverter")] |
| 176 | + [Duration]$RequestTimeout = $null |
| 177 | +} |
0 commit comments