gpt4 book ai didi

f# - 从 Suave 请求中聚合信息

转载 作者:行者123 更新时间:2023-12-03 21:35:38 24 4
gpt4 key购买 nike

我正在使用 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>
  • getGame 从 httpContext.request.querystring 获取 id getInSession
  • 从 httpContext.cookie 中获取 sessionId

  • 我找到的唯一方法就是将信息存储在 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 Session 等,并且能够按照我希望处理不同路线的方式组合它们:
    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)

    恐怕我需要的是与一些真正的函数式程序员进行一天的交谈。

    最佳答案

    setUserData是纯函数 - src .

    不确定是否this仍然是最新的,但它说 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 中,这并不完全有效,但您可以将其调整为它们所在的位置

    关于f# - 从 Suave 请求中聚合信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35825290/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com