可能会与yesod叠在一起 [英] Deep maybe stack with yesod
问题描述
我试图设置一个授权方案,用于检查1.用户是否已登录2.用户可以访问某个对象。为此,我首先调用 maybeAuthId
,然后尝试获取当前对象,然后将'join'添加到列出权限的另一个表中。有两种级别的可能情况和一级空白情况。我想过使用MaybeT,但是我太累了,无法使其工作,或者不是真正的monad变压器 - handler-transformers不能与MaybeT一起使用。有没有一种很好的方式来处理深层的maybes?
I'm trying to set an authorization scheme where I check that 1. user is logged in 2. user has access to a certain object. For this I first call maybeAuthId
, then try to get the current object, and 'join' to another table which lists permissions. There are two levels of maybe-cases and one level of empty-list case. I thought of using MaybeT, but either I'm too tired to get it to work or the "not really monad transformer"-handler-transformers can't be used with MaybeT. Is there a nice way to handle deep maybes?
编辑:
我看起来有点不清楚。我的意思是说我有这样的事情:
I was a bit unclear it seems. I meant that I have something like this:
case foo of
Nothing -> something
Just foo' -> do
bar <- somethingelse
case bar of
Nothing -> ...
Just bar' -> ...
推荐答案
您完全可以使用<$ c $对Yesod来说,c> MaybeT 。只需要这样做:
You can totally use MaybeT
for Yesod. Just do it like this:
runMaybeT $ do
uid <- MaybeT maybeAuthID
car <- MaybeT . runDB . getBy $ UniqueCarOwner uid
location <- MaybeT . liftIO . ciaLocateLicensePlate . licensePlate $ car
country <- MaybeT . findCountry $ location
return (car, country)
正如你所说,大部分函数都不是'针对Yesod中的通用错误处理进行了优化。但是,如果您有某种形式的 Monad m =>你可以简单地使用
。 MaybeT
来将它转化为 Monad m => m(也许a)
也许(m a)
As you said, most functions aren't optimized for generic error handling in Yesod. However, if you have something of the form Monad m => m (Maybe a)
, you can simply use MaybeT
to turn it inside out into Monad m => Maybe (m a)
.
这篇关于可能会与yesod叠在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!