mirror of
https://github.com/Smaug123/WoofWare.Myriad
synced 2025-12-14 21:05:39 +00:00
Allow cancellation token arg to have another name (#96)
This commit is contained in:
@@ -1034,7 +1034,7 @@ module ApiWithBasePath =
|
|||||||
/// Create a REST client.
|
/// Create a REST client.
|
||||||
let make (client : System.Net.Http.HttpClient) : IApiWithBasePath =
|
let make (client : System.Net.Http.HttpClient) : IApiWithBasePath =
|
||||||
{ new IApiWithBasePath with
|
{ new IApiWithBasePath with
|
||||||
member _.GetPathParam (parameter : string, ct : CancellationToken option) =
|
member _.GetPathParam (parameter : string, cancellationToken : CancellationToken option) =
|
||||||
async {
|
async {
|
||||||
let! ct = Async.CancellationToken
|
let! ct = Async.CancellationToken
|
||||||
|
|
||||||
@@ -1067,7 +1067,7 @@ module ApiWithBasePath =
|
|||||||
let! responseString = response.Content.ReadAsStringAsync ct |> Async.AwaitTask
|
let! responseString = response.Content.ReadAsStringAsync ct |> Async.AwaitTask
|
||||||
return responseString
|
return responseString
|
||||||
}
|
}
|
||||||
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
|
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = cancellationToken))
|
||||||
}
|
}
|
||||||
namespace PureGym
|
namespace PureGym
|
||||||
|
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ type internal IApiWithoutBaseAddress =
|
|||||||
[<BasePath "foo">]
|
[<BasePath "foo">]
|
||||||
type IApiWithBasePath =
|
type IApiWithBasePath =
|
||||||
[<Get "endpoint/{param}">]
|
[<Get "endpoint/{param}">]
|
||||||
abstract GetPathParam : [<Path "param">] parameter : string * ?ct : CancellationToken -> Task<string>
|
abstract GetPathParam : [<Path "param">] parameter : string * ?cancellationToken : CancellationToken -> Task<string>
|
||||||
|
|
||||||
[<WoofWare.Myriad.Plugins.HttpClient>]
|
[<WoofWare.Myriad.Plugins.HttpClient>]
|
||||||
[<BaseAddress "https://whatnot.com">]
|
[<BaseAddress "https://whatnot.com">]
|
||||||
|
|||||||
@@ -322,10 +322,9 @@ The [Grug-brained developer](https://grugbrain.dev/) would prefer to do this wit
|
|||||||
But since F# does not let you partially update an interface definition, we instead stamp out a record,
|
But since F# does not let you partially update an interface definition, we instead stamp out a record,
|
||||||
thereby allowing the programmer to use F#'s record-update syntax.
|
thereby allowing the programmer to use F#'s record-update syntax.
|
||||||
|
|
||||||
### Limitations
|
### Features
|
||||||
|
|
||||||
* We make the resulting record type at most internal (never public), since this is intended only to be used in tests.
|
* You may supply an `isInternal : bool` argument to the attribute. By default, we make the resulting record type at most internal (never public), since this is intended only to be used in tests; but you can instead make it public with `[<GenerateMock false>]`.
|
||||||
You will therefore need an `AssemblyInfo.fs` file [like the one in WoofWare.Myriad's own tests](./ConsumePlugin/AssemblyInfo.fs).
|
|
||||||
|
|
||||||
# Detailed examples
|
# Detailed examples
|
||||||
|
|
||||||
|
|||||||
@@ -183,27 +183,34 @@ module internal HttpClientGenerator =
|
|||||||
None
|
None
|
||||||
)
|
)
|
||||||
|
|
||||||
|
let args =
|
||||||
|
info.Args
|
||||||
|
|> List.map (fun arg ->
|
||||||
|
let argName =
|
||||||
|
match arg.Id with
|
||||||
|
| None -> failwith "TODO: create an arg name"
|
||||||
|
| Some id -> id
|
||||||
|
|
||||||
|
let argType =
|
||||||
|
if arg.IsOptional then
|
||||||
|
SynType.CreateApp (
|
||||||
|
SynType.CreateLongIdent (SynLongIdent.CreateString "option"),
|
||||||
|
[ arg.Type ],
|
||||||
|
isPostfix = true
|
||||||
|
)
|
||||||
|
else
|
||||||
|
arg.Type
|
||||||
|
|
||||||
|
argName, SynPat.CreateTyped (SynPat.CreateNamed argName, argType)
|
||||||
|
)
|
||||||
|
|
||||||
|
let cancellationTokenArg =
|
||||||
|
match List.tryLast args with
|
||||||
|
| None -> failwith $"expected an optional cancellation token as final arg in %s{info.Identifier.idText}"
|
||||||
|
| Some (arg, _) -> arg
|
||||||
|
|
||||||
let argPats =
|
let argPats =
|
||||||
let args =
|
let args = args |> List.map snd
|
||||||
info.Args
|
|
||||||
|> List.map (fun arg ->
|
|
||||||
let argName =
|
|
||||||
match arg.Id with
|
|
||||||
| None -> failwith "TODO: create an arg name"
|
|
||||||
| Some id -> id
|
|
||||||
|
|
||||||
let argType =
|
|
||||||
if arg.IsOptional then
|
|
||||||
SynType.CreateApp (
|
|
||||||
SynType.CreateLongIdent (SynLongIdent.CreateString "option"),
|
|
||||||
[ arg.Type ],
|
|
||||||
isPostfix = true
|
|
||||||
)
|
|
||||||
else
|
|
||||||
arg.Type
|
|
||||||
|
|
||||||
SynPat.CreateTyped (SynPat.CreateNamed argName, argType)
|
|
||||||
)
|
|
||||||
|
|
||||||
SynPat.Tuple (false, args, List.replicate (args.Length - 1) range0, range0)
|
SynPat.Tuple (false, args, List.replicate (args.Length - 1) range0, range0)
|
||||||
|> SynPat.CreateParen
|
|> SynPat.CreateParen
|
||||||
@@ -677,7 +684,7 @@ module internal HttpClientGenerator =
|
|||||||
yield jsonNode
|
yield jsonNode
|
||||||
]
|
]
|
||||||
|> SynExpr.createCompExpr "async" returnExpr
|
|> SynExpr.createCompExpr "async" returnExpr
|
||||||
|> SynExpr.startAsTask
|
|> SynExpr.startAsTask (SynLongIdent.CreateFromLongIdent [ cancellationTokenArg ])
|
||||||
|
|
||||||
SynMemberDefn.Member (
|
SynMemberDefn.Member (
|
||||||
SynBinding.SynBinding (
|
SynBinding.SynBinding (
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ module internal SynExpr =
|
|||||||
SynExpr.CreateApp (SynExpr.CreateIdent (Ident.Create "reraise"), SynExpr.CreateConst SynConst.Unit)
|
SynExpr.CreateApp (SynExpr.CreateIdent (Ident.Create "reraise"), SynExpr.CreateConst SynConst.Unit)
|
||||||
|
|
||||||
/// {body} |> fun a -> Async.StartAsTask (a, ?cancellationToken=ct)
|
/// {body} |> fun a -> Async.StartAsTask (a, ?cancellationToken=ct)
|
||||||
let startAsTask (body : SynExpr) =
|
let startAsTask (ct : SynLongIdent) (body : SynExpr) =
|
||||||
let lambda =
|
let lambda =
|
||||||
SynExpr.CreateApp (
|
SynExpr.CreateApp (
|
||||||
SynExpr.CreateLongIdent (SynLongIdent.Create [ "Async" ; "StartAsTask" ]),
|
SynExpr.CreateLongIdent (SynLongIdent.Create [ "Async" ; "StartAsTask" ]),
|
||||||
@@ -189,7 +189,7 @@ module internal SynExpr =
|
|||||||
SynExpr.CreateLongIdent (SynLongIdent.CreateString "a")
|
SynExpr.CreateLongIdent (SynLongIdent.CreateString "a")
|
||||||
equals
|
equals
|
||||||
(SynExpr.LongIdent (true, SynLongIdent.CreateString "cancellationToken", None, range0))
|
(SynExpr.LongIdent (true, SynLongIdent.CreateString "cancellationToken", None, range0))
|
||||||
(SynExpr.CreateLongIdent (SynLongIdent.CreateString "ct"))
|
(SynExpr.CreateLongIdent ct)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|> createLambda "a"
|
|> createLambda "a"
|
||||||
|
|||||||
Reference in New Issue
Block a user