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)

这有效并给了我这个:

    *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.

这并不奇怪,因为 couchView 需要Sink Object m a作为参数。

问题是:如何实现getPages :: IO [Page]?

4

1 回答 1

0

我不熟悉管道、aeson 或 couchDB,但这至少可以进行类型检查:

getPages :: IO [Result Page]
getPages = runCouch (def { couchHost = "192.168.0.103" }) $ do
    couchView_ "reader" "reader" "pages" [] $ CL.map (fromJSON . Object) =$ CL.consume
于 2012-12-10T18:27:52.793 回答