2015-12-17 90 views
1

我真的很努力去打破這個圈子。懶惰的字符串精神錯亂

getPostContent使用Wreq下載博客文章並將其返回。

getPostContent url = do 
    let opts = defaults & W.checkStatus .~ (Just $ \_ _ _ -> Nothing) 

    postResp <- getWith opts $ baseUrl ++ url 
    if postResp ^. W.responseStatus . statusCode == 200 
     -- then return $ LEnc.encodeUtf8 $ postResp ^. W.responseBody . _String  -- :: Prism T Text 
     then return $ postResp ^. W.responseBody . _String 
     else return "error downloading" 

這是由parseLBS

do 
    page <- getPostContent r -- :: IO String 
    let 
    -- parseLBS :: Data.ByteString.Lazy.Internal.ByteString -> Text.XML.Document 
    cursor = fromDocument $ parseLBS page 

據我瞭解,getPostContent是提供Data.Text.Text消耗,而我需要Data.ByteString.Lazy.Internal.ByteString,我無法工作,如何將它們轉換(認爲它應該是this,看上面的代碼片段,但它不能編譯)。與編碼

Couldn't match expected type ‘Data.ByteString.Lazy.Internal.ByteString’ 
       with actual type ‘T.Text’ 
    In the first argument of ‘parseLBS’, namely ‘page’ 
    In the second argument of ‘($)’, namely ‘parseLBS page’ 

編譯消息未註釋

Couldn't match type ‘TL.Text’ 
       with ‘T.Text’ 
NB: ‘TL.Text’ is defined in ‘Data.Text.Internal.Lazy’ 
    ‘T.Text’ is defined in ‘Data.Text.Internal’ 
Expected type: (TL.Text -> Const TL.Text TL.Text) 
       -> Data.ByteString.Lazy.Internal.ByteString 
       -> Const TL.Text Data.ByteString.Lazy.Internal.ByteString 
    Actual type: (T.Text -> Const TL.Text T.Text) 
       -> Data.ByteString.Lazy.Internal.ByteString 
       -> Const TL.Text Data.ByteString.Lazy.Internal.ByteString 
In the second argument of ‘(.)’, namely ‘_String’ 
In the second argument of ‘(^.)’, namely ‘responseBody . _String’ 
+0

它看起來對我來說,'encodeUtf8'或相似的應該工作。當你說它「不能編譯」時,它究竟會失敗? – MathematicalOrchid

+0

現在也添加了編譯器消息 –

+2

對。所以'encodeUtf8'想要一個懶惰的'Text',但是你有一個嚴格的'Text'。我認爲有一個轉換函數的地方...是的,嘗試'Data.Text.Lazy.toStrict'或'fromStrict'。 – MathematicalOrchid

回答

2

總結:encodeUtf8是正確的道路要走。看起來你使用的是Data.Text.Lazy.Encoding,這需要一個懶惰的Text。您可以使用Data.Text.Lazy.fromStrict轉換...或者你可以看看Data.Text.Encoding,其嚴格的Text作品(但隨後給你一個嚴格ByteString ...)