-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework how field-based parameters are serialized and deserialized (#467)
Adds a mechanism that allows to configure how path parameters and form encoded values are read and written. Additional changes: * Allow DateOnly to be injected and returned * Controllers may now also return simple data if they would like to
- Loading branch information
1 parent
f46397b
commit d1533b7
Showing
38 changed files
with
1,067 additions
and
671 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
|
||
namespace GenHTTP.Modules.Conversion.Formatters | ||
{ | ||
|
||
public sealed class BoolFormatter : IFormatter | ||
{ | ||
|
||
public bool CanHandle(Type type) => type == typeof(bool); | ||
|
||
public object? Read(string value, Type type) | ||
{ | ||
if (value == "1" || value == "on") | ||
{ | ||
return true; | ||
} | ||
else if (value == "0" || value == "off") | ||
{ | ||
return false; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public string? Write(object value, Type type) => ((bool)value) ? "1" : "0"; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace GenHTTP.Modules.Conversion.Formatters | ||
{ | ||
|
||
public sealed class DateOnlyFormatter : IFormatter | ||
{ | ||
private static readonly Regex DATE_ONLY_PATTERN = new(@"^([0-9]{4})\-([0-9]{2})\-([0-9]{2})$", RegexOptions.Compiled); | ||
|
||
public bool CanHandle(Type type) => type == typeof(DateOnly); | ||
|
||
public object? Read(string value, Type type) | ||
{ | ||
var match = DATE_ONLY_PATTERN.Match(value); | ||
|
||
if (match.Success) | ||
{ | ||
var year = int.Parse(match.Groups[1].Value); | ||
var month = int.Parse(match.Groups[2].Value); | ||
var day = int.Parse(match.Groups[3].Value); | ||
|
||
return new DateOnly(year, month, day); | ||
} | ||
|
||
throw new ArgumentException($"Input does not match the requested format (yyyy-mm-dd): {value}"); | ||
} | ||
|
||
public string? Write(object value, Type type) => ((DateOnly)value).ToString("yyyy-MM-dd"); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
|
||
namespace GenHTTP.Modules.Conversion.Formatters | ||
{ | ||
|
||
public sealed class EnumFormatter : IFormatter | ||
{ | ||
|
||
public bool CanHandle(Type type) => type.IsEnum; | ||
|
||
public object? Read(string value, Type type) => Enum.Parse(type, value); | ||
|
||
public string? Write(object value, Type type) => value.ToString(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Collections.Generic; | ||
|
||
using GenHTTP.Api.Infrastructure; | ||
|
||
namespace GenHTTP.Modules.Conversion.Formatters | ||
{ | ||
|
||
public sealed class FormatterBuilder : IBuilder<FormatterRegistry> | ||
{ | ||
private readonly List<IFormatter> _Registry = new(); | ||
|
||
#region Functionality | ||
|
||
public FormatterBuilder Add(IFormatter formatter) | ||
{ | ||
_Registry.Add(formatter); | ||
return this; | ||
} | ||
|
||
public FormatterBuilder Add<T>() where T : IFormatter, new() => Add(new T()); | ||
|
||
public FormatterRegistry Build() | ||
{ | ||
return new FormatterRegistry(_Registry); | ||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace GenHTTP.Modules.Conversion.Formatters | ||
{ | ||
|
||
public sealed class FormatterRegistry | ||
{ | ||
|
||
#region Get-/Setters | ||
|
||
public IReadOnlyList<IFormatter> Formatters { get; private set; } | ||
|
||
#endregion | ||
|
||
#region Initialization | ||
|
||
public FormatterRegistry(List<IFormatter> formatters) | ||
{ | ||
Formatters = formatters; | ||
} | ||
|
||
#endregion | ||
|
||
#region Functionality | ||
|
||
public bool CanHandle(Type type) => Formatters.Any(f => f.CanHandle(type)); | ||
|
||
public object? Read(string value, Type type) => Formatters.First(f => f.CanHandle(type)).Read(value, type); | ||
|
||
public string? Write(object value, Type type) => Formatters.First(f => f.CanHandle(type)).Write(value, type); | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.