2012-12-08 102 views
0
{-# LANGUAGE DeriveDataTypeable #-} 
{-# LANGUAGE OverloadedStrings #-} 
{-# LANGUAGE DeriveGeneric, ScopedTypeVariables #-} 

import Data.Generics (Data, Typeable) 
import Data.Conduit 
import qualified Data.Conduit.List as CL 
import Database.CouchDB.Conduit.Generic 
import Database.CouchDB.Conduit 
import Database.CouchDB.Conduit.View 
import Data.ByteString.Char8 (ByteString, empty) 
import Control.Monad.IO.Class (liftIO) 
import Data.Aeson 
import Data.Aeson.Types 
import GHC.Generics 

data Page = Page { id_ :: ByteString, url :: ByteString } 
      deriving (Show, Data, Typeable, Generic) 

instance FromJSON Page 

getPages :: IO() 
getPages = runCouch (def { couchHost = "192.168.0.103" }) $ do 
    couchView_ "reader" "reader" "pages" [] $ CL.mapM_ (liftIO . print) 

這工作,並給了我這樣的:CouchDB.Conduit:映射視圖數據

*Main> getPages 
    fromList [("key",String "802e343945c7f8da2d8a71fdb80025a7"),("id",String "802e343945c7f8da2d8a71fdb80025a7"),("value",String "http://yandex.ru")]` 

但其實我是想一個函數getPages :: IO [Page],所以我想這:

getPages = runCouch (def { couchHost = "192.168.0.103" }) $ do 
    couchView_ "reader" "reader" "pages" [] $ toType =$ CL.consume` 

這給我鍵入的錯誤:

`Reader/Couch.hs:24:47: 
    Couldn't match expected type `Object' with actual type `Value' 
    Expected type: Conduit Object m1 b0 
     Actual type: Conduit Value m0 a0 
    In the first argument of `(=$)', namely `toType' 
    In the second argument of `($)', namely `toType =$ CL.consume' 
Failed, modules loaded: none. 

Thi s並不令人驚訝,因爲couchView需要Sink Object m a作爲參數。

問題是:如何實現getPages :: IO [Page]?

回答

0

我不熟悉的導管,埃宋,或CouchDB的,但這至少類型檢查:

getPages :: IO [Result Page] 
getPages = runCouch (def { couchHost = "192.168.0.103" }) $ do 
    couchView_ "reader" "reader" "pages" [] $ CL.map (fromJSON . Object) =$ CL.consume