Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
c887394
Update Helpers.ps1
mdaneri Jul 10, 2024
48bec85
Fix an issue where ConvertTo-PodeYamlInternal was usedinstead of Conv…
mdaneri Jul 10, 2024
f9470ab
Yaml function are now public
mdaneri Jul 12, 2024
7834895
Update Utilities.ps1
mdaneri Jul 12, 2024
e99a121
fix test
mdaneri Jul 12, 2024
725faed
Update Utilities.Tests.ps1
mdaneri Jul 13, 2024
c1048ef
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Jul 14, 2024
5b8b5ef
Merge branch 'develop' into ConvertFrom-PodeYaml
mdaneri Jul 14, 2024
0cc2174
Merge branch 'develop' into ConvertFrom-PodeYaml
mdaneri Jul 18, 2024
8a7e802
Merge branch 'develop' into ConvertFrom-PodeYaml
mdaneri Jul 20, 2024
1a898be
Merge branch 'develop' into ConvertFrom-PodeYaml
mdaneri Jul 20, 2024
03168c9
converted yaml function in C#
mdaneri Aug 13, 2024
31067cc
Changed default Json conversion to PSCustomObject
Aug 14, 2024
c76ab58
Fix an issue with Windows Powershell
mdaneri Aug 15, 2024
9bee3fa
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Aug 20, 2024
c96b144
Update server.psd1
mdaneri Sep 8, 2024
083feaa
add support for PodeOrderedConcurrentDictionary
mdaneri Sep 11, 2024
f4cc4dd
fixes
mdaneri Sep 11, 2024
93f2283
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Sep 15, 2024
d431533
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Sep 21, 2024
7456ec2
Update Schedules.Tests.ps1
mdaneri Sep 21, 2024
601cae8
Merge branch 'develop' into ConvertFrom-PodeYaml
mdaneri Sep 22, 2024
357d93b
Merge branch 'develop' into ConvertFrom-PodeYaml
mdaneri Sep 22, 2024
37fa3b1
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Sep 27, 2024
3b09d8e
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Sep 28, 2024
8f70835
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Sep 28, 2024
588217c
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Sep 28, 2024
6399d3d
Merge branch 'develop' into ConvertFrom-PodeYaml
mdaneri Sep 29, 2024
a97e639
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Oct 16, 2024
1153515
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Oct 20, 2024
67b98f6
Merge branch 'develop' into ConvertFrom-PodeYaml
mdaneri Oct 21, 2024
8bf30ef
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Oct 23, 2024
68c86e8
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Oct 23, 2024
f3ac206
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Oct 27, 2024
faf31a6
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Oct 28, 2024
9f96e19
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Oct 30, 2024
b6bdf1b
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Nov 2, 2024
24e622d
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Nov 22, 2024
04f280d
Update pode.build.ps1
mdaneri Nov 22, 2024
51bbd2e
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Jan 26, 2025
996e6b9
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Feb 5, 2025
e9bfcf8
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Feb 9, 2025
e468175
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Feb 12, 2025
35c22a7
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Feb 16, 2025
56cbd30
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Feb 22, 2025
e65167a
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Mar 18, 2025
76d7e98
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Apr 12, 2025
12577fe
Merge remote-tracking branch 'upstream/develop' into ConvertFrom-Pode…
mdaneri Apr 19, 2025
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
3 changes: 3 additions & 0 deletions docs/Tutorials/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ A "path" like `Server.Ssl.Protocols` looks like the below in the file:
| Web.ContentType | Define expected Content Types for certain Routes | [link](../Routes/Utilities/ContentTypes) |
| Web.ErrorPages | Defines configuration for custom error pages | [link](../Routes/Utilities/ErrorPages) |
| Web.Static | Defines configuration for static content, such as caching | [link](../Routes/Utilities/StaticContent) |
| Web.Conversion.JsonToHashTable | Set the default Json conversion to HashTable if true otherwise PSCustomObject (Default: True for PSCore and False for Windows Powershell )| |
| Web.Conversion.YamlToHashTable | Set the default Yaml conversion to HashTable if true otherwise PSCustomObject (Default: True)| |

10 changes: 9 additions & 1 deletion examples/server.psd1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
Web = @{
Web = @{
Static = @{
Defaults = @(
'index.html',
Expand All @@ -19,6 +19,7 @@
Default = 'application/html'
Routes = @{
'/john' = 'application/json'
'/auth' = 'application/json'
}
}
Compression = @{
Expand All @@ -27,7 +28,14 @@
OpenApi = @{
UsePodeYamlInternal = $true
}

Conversion = @{
JsonToHashTable = $true
XmlToHashTable = $true
YamlToHashTable = $true
}
}

Server = @{
FileMonitor = @{
Enable = $false
Expand Down
10 changes: 10 additions & 0 deletions src/Listener/Pode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@
<PropertyGroup Condition="'$(NETCoreSdkVersion)' >= '10.0'">
<TargetFrameworks>$(TargetFrameworks);net10.0</TargetFrameworks>
</PropertyGroup>

<!-- Package reference for net6.0 -->
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.0" />
</ItemGroup>

<!-- Package reference for net8.0 -->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.4.4" />
</ItemGroup>
</Project>
336 changes: 336 additions & 0 deletions src/Listener/PodeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,336 @@
#if !NETSTANDARD2_0

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Management.Automation;

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build (macOS-latest)

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build (macOS-latest)

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build-preview (windows-latest)

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build-preview (ubuntu-latest)

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)

Check failure on line 9 in src/Listener/PodeConverter.cs

View workflow job for this annotation

GitHub Actions / build-preview (macOS-latest)

The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)
using System.Text;
using System.Text.RegularExpressions;

namespace Pode
{
public static class PodeConverter
{
public static string ToYaml(object inputObject, int depth = 10, int nestingLevel = 0, bool noNewLine = false)
{
if (depth < nestingLevel)
{
return string.Empty;
}

if (inputObject == null)
{
return inputObject is Array ? "[]" : string.Empty;
}

string padding = new string(' ', nestingLevel * 2);
string type = inputObject.GetType().Name;

if (inputObject is Array)
{
type = inputObject.GetType().BaseType.Name;
}

if (type != "String")
{
if (inputObject.GetType().IsGenericType && inputObject.GetType().GetGenericTypeDefinition() == typeof(ConcurrentDictionary<,>))
{
type = "hashtable";
}
else if (inputObject is OrderedDictionary)
{
type = "ordereddictionary";
}
else if (inputObject is IList)
{
type = "array";
}
else if (inputObject is Hashtable)
{
type = "hashtable";
}
else if (inputObject.GetType().IsGenericType && inputObject.GetType().GetGenericTypeDefinition() == typeof(PodeOrderedConcurrentDictionary<,>))
{
type = "ordereddictionary";
}
}

StringBuilder output = new StringBuilder();
switch (type.ToLower())
{
case "string":
string stringValue = inputObject.ToString();
if ((stringValue.Contains("\r\n") || stringValue.Length > 80) && !stringValue.StartsWith("http"))
{
StringBuilder multiline = new StringBuilder("|" + Environment.NewLine);
string[] items = stringValue.Split(new[] { "\n" }, StringSplitOptions.None);
foreach (string item in items)
{
string workingString = item.Replace("\r", "");
int length = workingString.Length;
int index = 0;
int wrap = 80;

while (index < length)
{
int breakpoint = wrap;
bool linebreak = false;

if ((length - index) > wrap)
{
int lastSpaceIndex = workingString.LastIndexOf(' ', index + wrap, wrap);
if (lastSpaceIndex != -1)
{
breakpoint = lastSpaceIndex - index;
}
else
{
linebreak = true;
breakpoint--;
}
}
else
{
breakpoint = length - index;
}

multiline.Append(padding).Append(workingString.Substring(index, breakpoint).Trim());
if (linebreak)
{
multiline.Append('\\');
}

index += breakpoint;
if (index < length)
{
multiline.Append(Environment.NewLine);
}
}

multiline.Append(Environment.NewLine);
}

output.Append(multiline.ToString().TrimEnd());
}
else
{
// Define the set of special characters that require wrapping in quotes
var specialChars = new HashSet<char> { '#', '[', ']', '@', '{', '}', '!', '*' };

// Check if the first character is in the special characters set
if (stringValue.Length > 0 && specialChars.Contains(stringValue[0]))
{
output.AppendFormat("'{0}'", stringValue.Replace("'", "''"));
}
else
{
output.Append(stringValue);
}
}
break;

case "hashtable":
case "ordereddictionary":
if (inputObject is IDictionary dict && dict.Count > 0)
{
int index = 0;
StringBuilder stringBuilder = new StringBuilder();
foreach (DictionaryEntry item in dict)
{
string newPadding = noNewLine && index++ == 0 ? string.Empty : Environment.NewLine + padding;
stringBuilder.Append(newPadding).Append(item.Key).Append(": ");
if (item.Value is ValueType)
{
if (item.Value is bool)
{
stringBuilder.Append(item.Value.ToString().ToLower());
}
else
{
stringBuilder.Append(item.Value);
}
}
else
{
int increment = item.Value is string ? 2 : 1;
stringBuilder.Append(ToYaml(item.Value, depth, nestingLevel + increment));
}
}
output.Append(stringBuilder);
}
else
{
output.Append("{}");
}
break;

case "pscustomobject":
if (inputObject is PSObject psObject && psObject.Properties.Any())
{
int index = 0;
StringBuilder stringBuilder = new StringBuilder();
foreach (PSPropertyInfo item in psObject.Properties)
{
string newPadding = noNewLine && index++ == 0 ? string.Empty : Environment.NewLine + padding;
stringBuilder.Append(newPadding).Append(item.Name).Append(": ");
if (item.Value is ValueType)
{
if (item.Value is bool)
{
stringBuilder.Append(item.Value.ToString().ToLower());
}
else
{
stringBuilder.Append(item.Value);
}
}
else
{
int increment = item.Value is string ? 2 : 1;
stringBuilder.Append(ToYaml(item.Value, depth, nestingLevel + increment));
}
}
output.Append(stringBuilder);
}
else
{
output.Append("{}");
}
break;

case "array":
IList list = inputObject as IList;
if (list != null && list.Count == 0)
{
output.Append("[]");
}
else
{
StringBuilder arrayStringBuilder = new StringBuilder();
int arrayIndex = 0;
foreach (object item in list)
{
string newPadding = noNewLine && arrayIndex++ == 0 ? string.Empty : Environment.NewLine + padding;
arrayStringBuilder.Append(newPadding).Append("- ").Append(ToYaml(item, depth, nestingLevel + 1, true).Trim('\''));
}
output.Append(arrayStringBuilder.ToString());
}
break;

default:
output.AppendFormat("'{0}'", inputObject);
break;
}

return output.ToString();
}


public static OrderedDictionary FromYaml(string inputObject)
{
// Split the YAML input into lines
string[] lines = inputObject.Split(new[] { "\n" }, StringSplitOptions.None);
// Initialize the main hashtable as an ordered hashtable
var hashtable = new OrderedDictionary();
// Stacks to keep track of current hashtable and indentation levels
var stack = new Stack();
var indentStack = new Stack();
stack.Push(hashtable);
indentStack.Push(-1);

// Regex patterns for matching lines
var keyValuePattern = new Regex(@"^(\s*)([^:]+):\s*(.*)$");
var arrayPattern = new Regex(@"^\[(.*)\]$");
var multilinePattern = new Regex(@"^\s+(.*)$");
var listItemPattern = new Regex(@"^\s*-\s*(.*)$");

// Variables to keep track of current key and hashtable
OrderedDictionary current = hashtable;
string currentKey = null;

// Iterate over each line of the YAML input
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
Match match = keyValuePattern.Match(line);
if (match.Success)
{
int indent = match.Groups[1].Length; // Indentation level
string key = match.Groups[2].Value.Trim(); // Key
string value = match.Groups[3].Value.Trim(); // Value

// Pop the stack if the current indentation level is less than or equal to the previous level
while ((int)indentStack.Peek() >= indent)
{
indentStack.Pop();
stack.Pop();
}

// Peek the current hashtable from the stack
current = (OrderedDictionary)stack.Peek();
currentKey = key;

// If value is empty, create a new nested ordered hashtable
if (string.IsNullOrEmpty(value))
{
var newDict = new OrderedDictionary();
current[key] = newDict;
stack.Push(newDict);
indentStack.Push(indent);
}
// Handle inline arrays
else if (arrayPattern.IsMatch(value))
{
current[key] = arrayPattern.Match(value).Groups[1].Value.Split(new[] { ", " }, StringSplitOptions.None);
}
// Handle multiline strings
else if (value == "|")
{
value = string.Empty;
while (++i < lines.Length && multilinePattern.IsMatch(lines[i]))
{
value += multilinePattern.Match(lines[i]).Groups[1].Value + "\n";
}
i--;
current[key] = value.TrimEnd();
}
// Convert and assign the value
else
{
current[key] = ConvertPodeStringToType(value);
}
}
// Handle list items
else if (listItemPattern.IsMatch(line))
{
string value = listItemPattern.Match(line).Groups[1].Value.Trim();
if (!(current[currentKey] is ArrayList list))
{
list = new ArrayList();
current[currentKey] = list;
}
list.Add(ConvertPodeStringToType(value));
}
}

return hashtable;
}


private static object ConvertPodeStringToType(string value)
{
if (bool.TryParse(value, out bool boolResult)) return boolResult;
if (int.TryParse(value, out int intResult)) return intResult;
if (double.TryParse(value, out double doubleResult)) return doubleResult;
if (DateTime.TryParse(value, out DateTime dateResult)) return dateResult;
return value;
}
}



}
#endif
Loading
Loading