Skip to content

Commit 2a818d0

Browse files
Add missing documentation for the formatting feature
1 parent d1533b7 commit 2a818d0

File tree

10 files changed

+72
-10
lines changed

10 files changed

+72
-10
lines changed

Modules/Controllers/Extensions.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public static class Extensions
2222
/// <param name="builder">The layout the controller should be added to</param>
2323
/// <param name="path">The path that should be handled by the controller</param>
2424
/// <param name="injectors">Optionally the injectors to be used by this controller</param>
25-
/// <param name="formats">Optionally the formats to be used by this controller</param>
25+
/// <param name="serializers">Optionally the serializers to be used by this controller</param>
26+
/// <param name="formatters">Optionally the formatters to be used by this controller</param>
2627
public static LayoutBuilder AddController<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder builder, string path, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? serializers = null, IBuilder<FormatterRegistry>? formatters = null) where T : new()
2728
{
2829
builder.Add(path, Controller.From<T>().Configured(injectors, serializers, formatters));
@@ -36,7 +37,8 @@ public static class Extensions
3637
/// <typeparam name="T">The type of the controller used to handle requests</typeparam>
3738
/// <param name="builder">The layout the controller should be added to</param>
3839
/// <param name="injectors">Optionally the injectors to be used by this controller</param>
39-
/// <param name="formats">Optionally the formats to be used by this controller</param>
40+
/// <param name="serializers">Optionally the serializers to be used by this controller</param>
41+
/// <param name="formatters">Optionally the formatters to be used by this controller</param>
4042
public static LayoutBuilder IndexController<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder builder, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? serializers = null, IBuilder<FormatterRegistry>? formatters = null) where T : new()
4143
{
4244
builder.Add(Controller.From<T>().Configured(injectors, serializers, formatters));

Modules/Controllers/Provider/ControllerBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public ControllerBuilder<T> Serializers(IBuilder<SerializationRegistry> registry
3131
return this;
3232
}
3333

34-
public ControllerBuilder<T> Injectors(IBuilder<InjectionRegistry>? registry)
34+
public ControllerBuilder<T> Injectors(IBuilder<InjectionRegistry> registry)
3535
{
3636
_Injection = registry;
3737
return this;
3838
}
3939

40-
public ControllerBuilder<T> Formatters(IBuilder<FormatterRegistry>? registry)
40+
public ControllerBuilder<T> Formatters(IBuilder<FormatterRegistry> registry)
4141
{
4242
_Formatters = registry;
4343
return this;

Modules/Conversion/Extensions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static class Extensions
1616
/// </summary>
1717
/// <param name="value">The value to be converted</param>
1818
/// <param name="type">The target type to convert the value to</param>
19+
/// <param name="formatters">The formatting to be used to actually perform the conversion</param>
1920
/// <returns>The converted value</returns>
2021
public static object? ConvertTo(this string? value, Type type, FormatterRegistry formatters)
2122
{

Modules/Conversion/Formatters/BoolFormatter.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public sealed class BoolFormatter : IFormatter
1010

1111
public object? Read(string value, Type type)
1212
{
13-
if (value == "1" || value == "on")
13+
if (value == "1" || Compare(value, "on") || Compare(value, "true"))
1414
{
1515
return true;
1616
}
17-
else if (value == "0" || value == "off")
17+
else if (value == "0" || Compare(value, "off") || Compare(value, "false"))
1818
{
1919
return false;
2020
}
@@ -24,6 +24,8 @@ public sealed class BoolFormatter : IFormatter
2424

2525
public string? Write(object value, Type type) => ((bool)value) ? "1" : "0";
2626

27+
private static bool Compare(string value, string expected) => string.Equals(value, expected, StringComparison.InvariantCultureIgnoreCase);
28+
2729
}
2830

2931
}

Modules/Conversion/Formatters/FormatterBuilder.cs

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public sealed class FormatterBuilder : IBuilder<FormatterRegistry>
1111

1212
#region Functionality
1313

14+
/// <summary>
15+
/// Adds the given formatter to the registry.
16+
/// </summary>
17+
/// <param name="formatter">The formatter to be added</param>
1418
public FormatterBuilder Add(IFormatter formatter)
1519
{
1620
_Registry.Add(formatter);
@@ -19,6 +23,10 @@ public FormatterBuilder Add(IFormatter formatter)
1923

2024
public FormatterBuilder Add<T>() where T : IFormatter, new() => Add(new T());
2125

26+
/// <summary>
27+
/// Builds the formatter registry based on the configuration.
28+
/// </summary>
29+
/// <returns>The newly created formatter registry</returns>
2230
public FormatterRegistry Build()
2331
{
2432
return new FormatterRegistry(_Registry);

Modules/Conversion/Formatters/IFormatter.cs

+28
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,41 @@
33
namespace GenHTTP.Modules.Conversion.Formatters
44
{
55

6+
/// <summary>
7+
/// Allows to add support for a specific type to be used as a parameter
8+
/// of a web service or controller as well as in form encoded data.
9+
/// </summary>
610
public interface IFormatter
711
{
812

13+
/// <summary>
14+
/// Checks, whether the formatter is capable of handling the given type.
15+
/// </summary>
16+
/// <param name="type">The type to be checked</param>
17+
/// <returns>true, if the formatter can be used to read and write such types</returns>
918
bool CanHandle(Type type);
1019

20+
/// <summary>
21+
/// Converts the given string into the specified type.
22+
/// </summary>
23+
/// <param name="value">The value to be converted</param>
24+
/// <param name="type">The type to convert the value to</param>
25+
/// <returns>The value converted into the given type</returns>
26+
/// <remarks>
27+
/// Used by the framework to read parameters to be passed to controllers or the like.
28+
/// </remarks>
1129
object? Read(string value, Type type);
1230

31+
/// <summary>
32+
/// Converts the given object instance of the given type into a string representation.
33+
/// </summary>
34+
/// <param name="value">The value to be formatted</param>
35+
/// <param name="type">The declared type of the value</param>
36+
/// <returns>The string representation of the given value</returns>
37+
/// <remarks>
38+
/// Used by the framework to serialize a single value into the response's body
39+
/// or to generate form encoded data.
40+
/// </remarks>
1341
string? Write(object value, Type type);
1442

1543
}

Modules/Conversion/Formatting.cs

+14
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
namespace GenHTTP.Modules.Conversion
44
{
55

6+
/// <summary>
7+
/// Entry point to customize the string representation generated for
8+
/// various types that can be returned or read by services.
9+
/// </summary>
610
public static class Formatting
711
{
812

13+
/// <summary>
14+
/// The default formatters to be used with support for enums, GUIDs,
15+
/// primitive types and strings.
16+
/// </summary>
17+
/// <returns>The default formatters</returns>
918
public static FormatterBuilder Default()
1019
{
1120
return new FormatterBuilder().Add<StringFormatter>()
@@ -16,6 +25,11 @@ public static FormatterBuilder Default()
1625
.Add<PrimitiveFormatter>();
1726
}
1827

28+
/// <summary>
29+
/// Creates an empty formatter registry that can be extended
30+
/// as needed.
31+
/// </summary>
32+
/// <returns>An empty formatter registry</returns>
1933
public static FormatterBuilder Empty() => new FormatterBuilder();
2034

2135
}

Modules/Functional/Provider/InlineBuilder.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ public InlineBuilder Injectors(IBuilder<InjectionRegistry> registry)
5252
return this;
5353
}
5454

55-
public InlineBuilder Formatters(IBuilder<FormatterRegistry>? registry)
55+
/// <summary>
56+
/// Configures the formatters to be used to extract path values.
57+
/// </summary>
58+
/// <param name="registry">The registry to be used by the handler</param>
59+
public InlineBuilder Formatters(IBuilder<FormatterRegistry> registry)
5660
{
5761
_Formatters = registry;
5862
return this;

Modules/Webservices/Extensions.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Diagnostics.CodeAnalysis;
22

33
using GenHTTP.Api.Infrastructure;
4+
45
using GenHTTP.Modules.Conversion.Formatters;
56
using GenHTTP.Modules.Conversion.Providers;
67
using GenHTTP.Modules.Layouting.Provider;
@@ -23,7 +24,8 @@ public static class Extensions
2324
/// <typeparam name="T">The type of the resource to be added</typeparam>
2425
/// <param name="path">The path the resource should be available at</param>
2526
/// <param name="injectors">Optionally the injectors to be used by this service</param>
26-
/// <param name="formats">Optionally the formats to be used by this service</param>
27+
/// <param name="serializers">Optionally the formats to be used by this service</param>
28+
/// <param name="formatters">Optionally the formatters to be used by this service</param>
2729
public static LayoutBuilder AddService<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(this LayoutBuilder layout, string path, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? serializers = null, IBuilder<FormatterRegistry>? formatters = null) where T : new()
2830
{
2931
return layout.Add(path, ServiceResource.From<T>().Configured(injectors, serializers, formatters));
@@ -36,7 +38,8 @@ public static class Extensions
3638
/// <param name="path">The path the resource should be available at</param>
3739
/// <param name="instance">The webservice resource instance</param>
3840
/// <param name="injectors">Optionally the injectors to be used by this service</param>
39-
/// <param name="formats">Optionally the formats to be used by this service</param>
41+
/// <param name="serializers">Optionally the formats to be used by this service</param>
42+
/// <param name="formatters">Optionally the formatters to be used by this service</param>
4043
public static LayoutBuilder AddService(this LayoutBuilder layout, string path, object instance, IBuilder<InjectionRegistry>? injectors = null, IBuilder<SerializationRegistry>? serializers = null, IBuilder<FormatterRegistry>? formatters = null)
4144
{
4245
return layout.Add(path, ServiceResource.From(instance).Configured(injectors, serializers, formatters));

Modules/Webservices/Provider/ServiceResourceBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public ServiceResourceBuilder Injectors(IBuilder<InjectionRegistry> registry)
4747
return this;
4848
}
4949

50-
public ServiceResourceBuilder Formatters(IBuilder<FormatterRegistry>? registry)
50+
public ServiceResourceBuilder Formatters(IBuilder<FormatterRegistry> registry)
5151
{
5252
_Formatters = registry;
5353
return this;

0 commit comments

Comments
 (0)