Skip to content

Commit

Permalink
* Add instances to Val to enable conversions to/from ByteString and […
Browse files Browse the repository at this point in the history
…Word8]. Reliability to be tested.
  • Loading branch information
ozataman committed Dec 8, 2010
1 parent 92bad96 commit 7f00b97
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
README
======


Snap Extension for MongoDB
--------------------------
==========================

This package provides a straightforward way to integrate MongoDB database
connectivity into Snap applications.

This is a very crude initial realease, to be refined on an ongoing basis. The
This is a very crude initial realease to be refined on an ongoing basis. The
API may change, so please use at your own risk for the time being.


3 changes: 2 additions & 1 deletion snap-extension-mongodb.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: snap-extension-mongodb
Version: 0.1
Version: 0.1.1
Synopsis: MongoDB extension for Snap Framework
Homepage: https://github.com/ozataman/snap-extension-mongodb
License: BSD3
Expand Down Expand Up @@ -32,3 +32,4 @@ Library
, Rank2Types
, MultiParamTypeClasses
, UndecidableInstances
, FlexibleInstances
21 changes: 21 additions & 0 deletions src/Snap/Extension/MongoDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ module Snap.Extension.MongoDB

import Control.Monad.Trans
import Control.Monad.Reader
import Data.ByteString.Internal (c2w, w2c)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import Data.Word (Word8)


import Database.MongoDB
import Snap.Types
Expand All @@ -34,3 +39,19 @@ class MonadSnap m => MonadMongoDB m where
----------------------------------------------------------------------------
-- |
withDB :: ReaderT Database (Action m) a -> m (Either Failure a)


------------------------------------------------------------------------------
-- | Get strict ByteString to work directly with BSON auto-casting
instance Val B8.ByteString where
val = val . B8.unpack
cast' x = fmap B8.pack . cast' $ x
cast' _ = Nothing


------------------------------------------------------------------------------
-- | Get [Octet] to work directly with BSON auto-casting
instance Val [Word8] where
val = val . fmap w2c
cast' x = fmap (fmap c2w) . cast' $ x
cast' _ = Nothing

0 comments on commit 7f00b97

Please sign in to comment.