gpt4 book ai didi

Haskell servant 组合api实现

转载 作者:行者123 更新时间:2023-12-05 03:18:43 26 4
gpt4 key购买 nike

我正在浏览找到的仆人教程 here .有一个部分实现留给用户弄清楚它应该是什么。

type APIFor a i =
Get '[JSON] [a]
:<|> ReqBody '[JSON] a :> PostNoContent
:<|> Capture "id" i :>
( Get '[JSON] a
:<|> ReqBody '[JSON] a :> PutNoContent
:<|> DeleteNoContent
)

serverFor :: Handler [a]
-> (a -> Handler NoContent)
-> (i -> Handler a)
-> (i -> a -> Handler NoContent)
-> (i -> Handler NoContent)
-> Server (APIFor a i)
serverFor = error "..." -- What should be here?

根据前面的示例,我想到了实现:

serverFor = listing :<|> ops

where
listing :: Handler [a]
listing = error ""

ops a i =
creating a
:<|> getById i
:<|> updating i a
:<|> deleting i

where
creating :: a -> Handler NoContent
creating a = error ""

getById :: i -> Handler a
getById id = error ""

updating :: i -> a -> Handler NoContent
updating i a = error ""

deleting :: i -> Handler NoContent
deleting i = error ""

但是我得到了错误:

• Couldn't match expected type ‘Handler [a]
-> (a -> Handler NoContent)
-> (i -> Handler a)
-> (i -> a -> Handler NoContent)
-> (i -> Handler NoContent)
-> Server (APIFor a i)’
with actual type ‘Handler [a0]
:<|> (a1
-> i0
-> Handler NoContent
:<|> (Handler a2
:<|> (Handler NoContent :<|> Handler NoContent)))’
• Possible cause: ‘(:<|>)’ is applied to too many arguments
In the expression: listing :<|> ops

我明白错误的意思,但不知道如何正确实现。是否有人能够帮助确定正确的实现方式?

最佳答案

serverFor 函数的签名表明它采用处理函数并构建 Server

serverFor list create get update delete = 
-- combine handlers according to API type
list :<|> create :<|> (\i -> get i :<|> update i :<|> delete i)

然后您可以为您的特定类型调用 serverFor

data User = User {id :: Int}

server :: Server (APIFor User Int)
server = serverFor listing creating getById updating deleting
where
listing :: Handler [User]
listing = error ""
creating :: User -> Handler NoContent
creating a = error ""
getById :: Int -> Handler User
getById id = error ""
updating :: Int -> User -> Handler NoContent
updating i a = error ""
deleting :: Int -> Handler NoContent
deleting i = error ""

关于Haskell servant 组合api实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73643300/

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