diff --git a/Modules/Controllers/Extensions.cs b/Modules/Controllers/Extensions.cs index 21f18638..1fe52f3b 100644 --- a/Modules/Controllers/Extensions.cs +++ b/Modules/Controllers/Extensions.cs @@ -22,7 +22,8 @@ public static class Extensions /// The layout the controller should be added to /// The path that should be handled by the controller /// Optionally the injectors to be used by this controller - /// Optionally the formats to be used by this controller + /// Optionally the serializers to be used by this controller + /// Optionally the formatters to be used by this controller public static LayoutBuilder AddController<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder builder, string path, IBuilder? injectors = null, IBuilder? serializers = null, IBuilder? formatters = null) where T : new() { builder.Add(path, Controller.From().Configured(injectors, serializers, formatters)); @@ -36,7 +37,8 @@ public static class Extensions /// The type of the controller used to handle requests /// The layout the controller should be added to /// Optionally the injectors to be used by this controller - /// Optionally the formats to be used by this controller + /// Optionally the serializers to be used by this controller + /// Optionally the formatters to be used by this controller public static LayoutBuilder IndexController<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder builder, IBuilder? injectors = null, IBuilder? serializers = null, IBuilder? formatters = null) where T : new() { builder.Add(Controller.From().Configured(injectors, serializers, formatters)); diff --git a/Modules/Controllers/Provider/ControllerBuilder.cs b/Modules/Controllers/Provider/ControllerBuilder.cs index 76e38e57..2c76e8f6 100644 --- a/Modules/Controllers/Provider/ControllerBuilder.cs +++ b/Modules/Controllers/Provider/ControllerBuilder.cs @@ -31,13 +31,13 @@ public ControllerBuilder Serializers(IBuilder registry return this; } - public ControllerBuilder Injectors(IBuilder? registry) + public ControllerBuilder Injectors(IBuilder registry) { _Injection = registry; return this; } - public ControllerBuilder Formatters(IBuilder? registry) + public ControllerBuilder Formatters(IBuilder registry) { _Formatters = registry; return this; diff --git a/Modules/Conversion/Extensions.cs b/Modules/Conversion/Extensions.cs index 5c7005c0..c423fedc 100644 --- a/Modules/Conversion/Extensions.cs +++ b/Modules/Conversion/Extensions.cs @@ -16,6 +16,7 @@ public static class Extensions /// /// The value to be converted /// The target type to convert the value to + /// The formatting to be used to actually perform the conversion /// The converted value public static object? ConvertTo(this string? value, Type type, FormatterRegistry formatters) { diff --git a/Modules/Conversion/Formatters/BoolFormatter.cs b/Modules/Conversion/Formatters/BoolFormatter.cs index 803a0416..9879c9a7 100644 --- a/Modules/Conversion/Formatters/BoolFormatter.cs +++ b/Modules/Conversion/Formatters/BoolFormatter.cs @@ -10,11 +10,11 @@ public sealed class BoolFormatter : IFormatter public object? Read(string value, Type type) { - if (value == "1" || value == "on") + if (value == "1" || Compare(value, "on") || Compare(value, "true")) { return true; } - else if (value == "0" || value == "off") + else if (value == "0" || Compare(value, "off") || Compare(value, "false")) { return false; } @@ -24,6 +24,8 @@ public sealed class BoolFormatter : IFormatter public string? Write(object value, Type type) => ((bool)value) ? "1" : "0"; + private static bool Compare(string value, string expected) => string.Equals(value, expected, StringComparison.InvariantCultureIgnoreCase); + } } diff --git a/Modules/Conversion/Formatters/FormatterBuilder.cs b/Modules/Conversion/Formatters/FormatterBuilder.cs index b9044b3d..e2803b9a 100644 --- a/Modules/Conversion/Formatters/FormatterBuilder.cs +++ b/Modules/Conversion/Formatters/FormatterBuilder.cs @@ -11,6 +11,10 @@ public sealed class FormatterBuilder : IBuilder #region Functionality + /// + /// Adds the given formatter to the registry. + /// + /// The formatter to be added public FormatterBuilder Add(IFormatter formatter) { _Registry.Add(formatter); @@ -19,6 +23,10 @@ public FormatterBuilder Add(IFormatter formatter) public FormatterBuilder Add() where T : IFormatter, new() => Add(new T()); + /// + /// Builds the formatter registry based on the configuration. + /// + /// The newly created formatter registry public FormatterRegistry Build() { return new FormatterRegistry(_Registry); diff --git a/Modules/Conversion/Formatters/IFormatter.cs b/Modules/Conversion/Formatters/IFormatter.cs index 625ce99f..a1778d6f 100644 --- a/Modules/Conversion/Formatters/IFormatter.cs +++ b/Modules/Conversion/Formatters/IFormatter.cs @@ -3,13 +3,41 @@ namespace GenHTTP.Modules.Conversion.Formatters { + /// + /// Allows to add support for a specific type to be used as a parameter + /// of a web service or controller as well as in form encoded data. + /// public interface IFormatter { + /// + /// Checks, whether the formatter is capable of handling the given type. + /// + /// The type to be checked + /// true, if the formatter can be used to read and write such types bool CanHandle(Type type); + /// + /// Converts the given string into the specified type. + /// + /// The value to be converted + /// The type to convert the value to + /// The value converted into the given type + /// + /// Used by the framework to read parameters to be passed to controllers or the like. + /// object? Read(string value, Type type); + /// + /// Converts the given object instance of the given type into a string representation. + /// + /// The value to be formatted + /// The declared type of the value + /// The string representation of the given value + /// + /// Used by the framework to serialize a single value into the response's body + /// or to generate form encoded data. + /// string? Write(object value, Type type); } diff --git a/Modules/Conversion/Formatting.cs b/Modules/Conversion/Formatting.cs index 36d29c55..64e1c982 100644 --- a/Modules/Conversion/Formatting.cs +++ b/Modules/Conversion/Formatting.cs @@ -3,9 +3,18 @@ namespace GenHTTP.Modules.Conversion { + /// + /// Entry point to customize the string representation generated for + /// various types that can be returned or read by services. + /// public static class Formatting { + /// + /// The default formatters to be used with support for enums, GUIDs, + /// primitive types and strings. + /// + /// The default formatters public static FormatterBuilder Default() { return new FormatterBuilder().Add() @@ -16,6 +25,11 @@ public static FormatterBuilder Default() .Add(); } + /// + /// Creates an empty formatter registry that can be extended + /// as needed. + /// + /// An empty formatter registry public static FormatterBuilder Empty() => new FormatterBuilder(); } diff --git a/Modules/Functional/Provider/InlineBuilder.cs b/Modules/Functional/Provider/InlineBuilder.cs index c8dd9446..67ea69ad 100644 --- a/Modules/Functional/Provider/InlineBuilder.cs +++ b/Modules/Functional/Provider/InlineBuilder.cs @@ -52,7 +52,11 @@ public InlineBuilder Injectors(IBuilder registry) return this; } - public InlineBuilder Formatters(IBuilder? registry) + /// + /// Configures the formatters to be used to extract path values. + /// + /// The registry to be used by the handler + public InlineBuilder Formatters(IBuilder registry) { _Formatters = registry; return this; diff --git a/Modules/Webservices/Extensions.cs b/Modules/Webservices/Extensions.cs index b36bd046..c097078e 100644 --- a/Modules/Webservices/Extensions.cs +++ b/Modules/Webservices/Extensions.cs @@ -1,6 +1,7 @@ using System.Diagnostics.CodeAnalysis; using GenHTTP.Api.Infrastructure; + using GenHTTP.Modules.Conversion.Formatters; using GenHTTP.Modules.Conversion.Providers; using GenHTTP.Modules.Layouting.Provider; @@ -23,7 +24,8 @@ public static class Extensions /// The type of the resource to be added /// The path the resource should be available at /// Optionally the injectors to be used by this service - /// Optionally the formats to be used by this service + /// Optionally the formats to be used by this service + /// Optionally the formatters to be used by this service public static LayoutBuilder AddService<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder layout, string path, IBuilder? injectors = null, IBuilder? serializers = null, IBuilder? formatters = null) where T : new() { return layout.Add(path, ServiceResource.From().Configured(injectors, serializers, formatters)); @@ -36,7 +38,8 @@ public static class Extensions /// The path the resource should be available at /// The webservice resource instance /// Optionally the injectors to be used by this service - /// Optionally the formats to be used by this service + /// Optionally the formats to be used by this service + /// Optionally the formatters to be used by this service public static LayoutBuilder AddService(this LayoutBuilder layout, string path, object instance, IBuilder? injectors = null, IBuilder? serializers = null, IBuilder? formatters = null) { return layout.Add(path, ServiceResource.From(instance).Configured(injectors, serializers, formatters)); diff --git a/Modules/Webservices/Provider/ServiceResourceBuilder.cs b/Modules/Webservices/Provider/ServiceResourceBuilder.cs index a6c85090..a692e988 100644 --- a/Modules/Webservices/Provider/ServiceResourceBuilder.cs +++ b/Modules/Webservices/Provider/ServiceResourceBuilder.cs @@ -47,7 +47,7 @@ public ServiceResourceBuilder Injectors(IBuilder registry) return this; } - public ServiceResourceBuilder Formatters(IBuilder? registry) + public ServiceResourceBuilder Formatters(IBuilder registry) { _Formatters = registry; return this;