我正在使用Suave构建一个经过身份验证的web API,而且我经常遇到通过不同功能聚合信息的问题。
pathScan Navigation.playersAvailables GetGame >>= getInSession >>= (fun (gameId,playerInSession) -> //access to both gameId and player in session)签名:
getGame : HttpContext -> Async<HttpContext option>
getInSession : HttpContext -> Async<HttpContext option> 为了做到这一点,我发现的唯一方法就是在userDataDictionnary中存储信息:
Writers.setUserData "player" { playerId= playersId; socialId=socialId; username = username}并在另一个函数中检索它,但在我看来,它非常讨厌:
let player = x.userState.["player"] :?> PlayerSession
//do some other stuff now that we have the current player还有别的办法吗?我想拥有像getGameId和get会话之类的纯函数。当我想要处理我的不同路线时,我能写出它们:
pathScan Navigation.playersAvailables GetGame >>= getInSession >>= (fun (gameId,playerInSession) -> //access to both gameId and player in session)
pathScan Navigation.otherRoute GetGame >>= (fun (gameId) -> //process gameId)
pathScan Navigation.otherRoute2 getInSession >>= (fun (sessionId) -> //process sessionId to do some other stuff)恐怕我需要的是和一个真正的功能程序员谈一天。
发布于 2019-02-19 15:37:08
setUserData是一个纯函数-- src。
不确定这是否仍然是当前的,但它说pathScan和>>=不能很好地链接在一起。然而,我认为您正在使用的Writers.setUserData可能能够完成它。
拿着一个物品袋把东西拿出来不是很好。
不如:
let (|ParseInt|_|) =
function
| "" | null -> None
| x ->
match Int32.TryParse x with
| true, i -> Some i
| _ -> None
let (|HasParam|_|) name (ctx:HttpContext) =
ctx.request.queryParam name
|> function
|Choice1Of2 value ->
Some value
| _ -> None
let playersAvailablePart:WebPart =
function
//access to both gameId and player in session
|HasParam "inSession" playerInSession & HasParam "gameId" gameId as ctx ->
// do your processing here, sample return:
OK "we had all the required important parts" ctx
// or an example of something more strongly typed
| HasParam "inSession" (ParseInt playerInSession) & HasParam "gameId" (ParseInt gameId) as ctx ->
// do your processing here, sample return:
OK "we had all the required important parts" ctx
| ctx -> never ctx如果这些值不在queryParameters中,则这并不完全有效,但您可以将其调整到它们所在的位置
https://stackoverflow.com/questions/35825290
复制相似问题