Yesod处理程序,发布文件的内容 [英] Yesod handlers, content of POSTed files

查看:71
本文介绍了Yesod处理程序,发布文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  postImportR = do 
fi< - lookupFiles文件
fc< - lift $ fileSource(fi !! 0)$$ consume

似乎可行至少我可以liftIO $ print fc),将它分解为一个迭代函数不会:

  process :: [FileInfo]  - > [String] 
process [] = []
process(f:r)= do
fn < - fileName f
fc < - lift $ fileSource f $$消耗
([fn]:(process r))

postImportR = do
fi< - lookupFilesfile
process fi

甚至可以使用lambda函数:

<$ p (文件名f,提取$ fileSource f $$消耗))fi

在Handler中它给我一个类型错误,我不明白。

我的错在哪里 - - 喜欢从文件的行(和l生成数据库导入内容赚取更多的Haskell,当然)。

解决方案

您有

  fileName :: FileInfo  - >文本

所以你不能直接使用 fileName 在一个do-block中,比如

  fn < -  fileName f 
pre>

这需要是一个绑定的绑定

  let fn = fileName f 

接下来就是 process :: [FileInfo ] - > [String] 不可能(1)

  fileSource :: FileInfo - >源(ResourceT IO)ByteString 

所以带有

  fc<  -  lift $ fileSource f $$ consume 

你在一个do块中,它的 Monad MonadIO ,你不能从 Monad ,它可以包装任意 IO --actions,就像你不能脱离 IO 本身。



您可以拥有的是

  process ::(SomeFiendishConstraint m)=> [FileInfo]  - > m [Text] 
process [] = return []
process(f:r)= do
let fn = fileName f
lift $ fileSource f $$ consume
fs< - 进程r
返回(fn:fs)

或者更简洁,

  process = mapM(\f  - > lift $ fileSource f $$ consume>> return fileName f) 

然后

  postImportR = do 
fi< - lookupFilesfile
process fi

(1)限制 unsafePerformIO


while the following code:

postImportR = do
    fi <- lookupFiles "file"
    fc <- lift $ fileSource (fi !! 0) $$ consume

seems to work (at least can I "liftIO $ print fc), splitting it off to a function for iterating doesn't:

process :: [FileInfo] -> [String]
process [] = []
process (f:r) = do
    fn <- fileName f
    fc <- lift $ fileSource f $$ consume
    ([fn] : (process r))

postImportR = do
    fi <- lookupFiles "file"
    process fi

or even with a lambda function:

files <- L.map (\f -> (fileName f, lift $ fileSource f $$ consume)) fi

in the Handler it gives me a type error I don't understand.

Where's my fault -- liked to generate content for database import from the file's lines (and to learn some more Haskell, of course).

解决方案

You have

fileName :: FileInfo -> Text

so you can't directly use fileName in a do-block like

fn <- fileName f

That would need to be a let-binding

let fn = fileName f

The next thing is what makes process :: [FileInfo] -> [String] impossible(1),

fileSource :: FileInfo -> Source (ResourceT IO) ByteString

so with

fc <- lift $ fileSource f $$ consume

you are in a do-block whose Monad is a MonadIO, and you can't get out of a Monad that can wrap arbitrary IO-actions, just like you can't get out of IO itself.

What you can have is

process :: (SomeFiendishConstraint m) => [FileInfo] -> m [Text]
process [] = return []
process (f:r) = do
    let fn = fileName f
    lift $ fileSource f $$ consume
    fs <- process r
    return (fn : fs)

or, more succinctly,

process = mapM (\f -> lift $ fileSource f $$ consume >> return fileName f)

and then

postImportR = do
    fi <- lookupFiles "file"
    process fi

(1) Barring unsafePerformIO.

这篇关于Yesod处理程序,发布文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆