gpt4 book ai didi

haskell couchdb-conduit 示例

转载 作者:行者123 更新时间:2023-12-02 12:36:03 26 4
gpt4 key购买 nike

{-# LANGUAGE DeriveDataTypeable, OverloadedStrings, ScopedTypeVariables #-}
module Db (
couchTest
) where

import Control.Monad.IO.Class (liftIO)
import Data.ByteString (ByteString)
import Data.Generics (Data, Typeable)
import Database.CouchDB.Conduit
import Database.CouchDB.Conduit.Generic

conn :: CouchConnection
conn = def {couchLogin = "admin", couchPass = "admin"}

data D = D { f1 :: Int, f2 :: String } deriving (Show, Data, Typeable)

couchTest = runCouch conn $ do
rev1 <- couchPut "mydb" "my-doc1" "" [] $ D 123 "str"
rev2 <- couchPut "mydb" "my-doc1" rev1 [] $ D 1234 "another"
(rev3, d1 :: D) <- couchGet "mydb" "my-doc1" []
liftIO $ print d1
couchPut' "mydb" "my-doc1" [] $ D 12345 "third" -- notice - no rev
rev3 <- couchRev "mydb" "my-doc1"
couchDelete "mydb" "my-doc1" rev3

错误

No instance for (monad-control-0.3.1.3:Control.Monad.Trans.Control.MonadBaseControl
IO m0)
arising from a use of `couchPut'
Possible fix:
add an instance declaration for
(monad-control-0.3.1.3:Control.Monad.Trans.Control.MonadBaseControl
IO m0)
In the expression: couchPut "mydb" "my-doc1" "" []
In a stmt of a 'do' block:
rev1 <- couchPut "mydb" "my-doc1" "" [] $ D 123 "str"
In the second argument of `($)', namely
`do { rev1 <- couchPut "mydb" "my-doc1" "" [] $ D 123 "str";
rev2 <- couchPut "mydb" "my-doc1" rev1 [] $ D 1234 "another";
(rev3, d1 :: D) <- couchGet "mydb" "my-doc1" [];
liftIO $ print d1;
.... }'

1) 如何为 monad-control-0.3.1.3:Control.Monad.Trans.Control.MonadBaseControl IO m0 添加实例声明?

2)是否可以使用 couchdb-conduit 创建附件?

3)是否有一个直接使用couchdb与http包的示例,主要是为了看看它需要多少代码?

最佳答案

最好将您的问题分开,因为您问的是三个不同的问题。就 #1 而言,MonadBaseControl 不需要新实例。添加类型签名以消除歧义并立即编译:

{-# LANGUAGE DeriveDataTypeable, FlexibleContexts,
OverloadedStrings, ScopedTypeVariables #-}
module Db (
couchTest
) where

import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Trans.Resource (MonadThrow, MonadUnsafeIO)
import Control.Monad.Trans.Control (MonadBaseControl)
import Data.ByteString (ByteString)
import Data.Generics (Data, Typeable)
import Database.CouchDB.Conduit
import Database.CouchDB.Conduit.Generic

conn :: CouchConnection
conn = def {couchLogin = "admin", couchPass = "admin"}

data D = D { f1 :: Int, f2 :: String } deriving (Show, Data, Typeable)

couchTest
:: (MonadIO m, MonadUnsafeIO m, MonadThrow m, MonadBaseControl IO m)
=> m ()
-- couchTest :: IO () -- restricting it to IO is also an option
couchTest = runCouch conn $ do
rev1 <- couchPut "mydb" "my-doc1" "" [] $ D 123 "str"
rev2 <- couchPut "mydb" "my-doc1" rev1 [] $ D 1234 "another"
(rev3, d1 :: D) <- couchGet "mydb" "my-doc1" []
liftIO $ print d1
couchPut' "mydb" "my-doc1" [] $ D 12345 "third" -- notice - no rev
rev3 <- couchRev "mydb" "my-doc1"
couchDelete "mydb" "my-doc1" rev3

我通过启用 NoMonomorphismRestriction 语言编译指示并在 GHCi 中运行以下命令,让 GHC 为我提供了约束列表。在许多情况下,您还可以保留 NMR 标志设置,而不是添加显式签名,但我更喜欢显式签名。

*Db> :info couchTest couchTest ::  (MonadIO m, Control.Monad.Trans.Resource.MonadUnsafeIO m,   Control.Monad.Trans.Resource.MonadThrow m,   MonadBaseControl IO m) =>  m ()    -- Defined at /tmp/cdb.hs:21:5

关于haskell couchdb-conduit 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11787372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com