From 6ee9f1a6e8e88d291324dd0cc6da072ff295d9a9 Mon Sep 17 00:00:00 2001 From: Lucas Teles Date: Tue, 14 Nov 2023 15:06:06 -0300 Subject: [PATCH] patch: async handlers resolution --- FSharp.MinimalApi.Interop/AsParameter.cs | 32 ++++++++++++++----- .../Builder/EndpointHandlerBuilder.fs | 21 ++---------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/FSharp.MinimalApi.Interop/AsParameter.cs b/FSharp.MinimalApi.Interop/AsParameter.cs index ed0e4a3..a721cfb 100644 --- a/FSharp.MinimalApi.Interop/AsParameter.cs +++ b/FSharp.MinimalApi.Interop/AsParameter.cs @@ -1,4 +1,6 @@ -namespace FSharp.MinimalApi; +using System.Reflection; + +namespace FSharp.MinimalApi; using Microsoft.FSharp.Core; using Microsoft.AspNetCore.Http; @@ -6,13 +8,16 @@ public static class AsParameters { public static Delegate Of(FSharpFunc requestDelegate) => - typeof(TParam) == typeof(Unit) - ? typeof(TResult) == typeof(Unit) - ? void () => requestDelegate.Invoke(Operators.Unchecked.DefaultOf()) - : () => requestDelegate.Invoke(Operators.Unchecked.DefaultOf()) - : typeof(TResult) == typeof(Unit) - ? void ([AsParameters] TParam parameters) => requestDelegate.Invoke(parameters) - : ([AsParameters] TParam parameters) => requestDelegate.Invoke(parameters); + typeof(TResult).IsGenericType && + typeof(TResult).GetGenericTypeDefinition() == typeof(Task<>) + ? CreateAsTask(requestDelegate) + : typeof(TParam) == typeof(Unit) + ? typeof(TResult) == typeof(Unit) + ? void () => requestDelegate.Invoke(Operators.Unchecked.DefaultOf()) + : () => requestDelegate.Invoke(Operators.Unchecked.DefaultOf()) + : typeof(TResult) == typeof(Unit) + ? void ([AsParameters] TParam parameters) => requestDelegate.Invoke(parameters) + : ([AsParameters] TParam parameters) => requestDelegate.Invoke(parameters); public static Delegate OfTask( FSharpFunc> requestDelegate) => @@ -25,4 +30,15 @@ public static Delegate OfTask( : typeof(TResult) == typeof(Unit) ? Task ([AsParameters] TParam parameters) => requestDelegate.Invoke(parameters) : ([AsParameters] TParam parameters) => requestDelegate.Invoke(parameters); + + static Delegate CreateAsTask(FSharpFunc requestDelegate) + { + var underType = typeof(TResult).GetGenericArguments()[0]; + var method = + typeof(AsParameters).GetMethod(nameof(OfTask), + BindingFlags.Public | BindingFlags.Static)! + .MakeGenericMethod(typeof(TParam), underType); + + return (Delegate) method.Invoke(null, new object?[] {requestDelegate})!; + } } \ No newline at end of file diff --git a/FSharp.MinimalApi/Builder/EndpointHandlerBuilder.fs b/FSharp.MinimalApi/Builder/EndpointHandlerBuilder.fs index 70b3405..1275350 100644 --- a/FSharp.MinimalApi/Builder/EndpointHandlerBuilder.fs +++ b/FSharp.MinimalApi/Builder/EndpointHandlerBuilder.fs @@ -61,15 +61,11 @@ type RouterBaseBuilder<'state>() = //**************************************************************************************************** // MapGet [] - member this.MapGet(s, route, f: Delegate, ?config) = this.get s route f config - - [] - member this.MapGet(s, route, f: 'p -> 'r, ?config) = + member inline this.MapGet(s, route, f: 'p -> 'r, ?config) = this.get s route (AsParameters.Of f) config [] - member this.MapGet(s, route, f: 'p -> Task<'r>, ?config) = - this.get s route (AsParameters.OfTask f) config + member this.MapGet(s, route, f: Delegate, ?config) = this.get s route f config // MapPost [] @@ -79,10 +75,6 @@ type RouterBaseBuilder<'state>() = member this.MapPost(s, route, f: 'p -> 'r, ?config) = this.post s route (AsParameters.Of f) config - [] - member this.MapPost(s, route, f: 'p -> Task<'r>, ?config) = - this.post s route (AsParameters.OfTask f) config - // MapPut [] member this.MapPut(s, route, f: Delegate, ?config) = this.put s route f config @@ -91,10 +83,6 @@ type RouterBaseBuilder<'state>() = member this.MapPut(s, route, f: 'p -> 'r, ?config) = this.put s route (AsParameters.Of f) config - [] - member this.MapPut(s, route, f: 'p -> Task<'r>, ?config) = - this.put s route (AsParameters.OfTask f) config - // MapDelete [] member this.MapDelete(s, route, f: Delegate, ?config) = this.delete s route f config @@ -103,11 +91,6 @@ type RouterBaseBuilder<'state>() = member this.MapDelete(s, route, f: 'p -> 'r, ?config) = this.delete s route (AsParameters.Of f) config - [] - member this.MapDelete(s, route, f: 'p -> Task<'r>, ?config) = - this.delete s route (AsParameters.OfTask f) config - - //**************************************************************************************************** // TypedResult Maps //****************************************************************************************************