2010-01-17 01:22:05 +00:00
|
|
|
{-
|
|
|
|
|
|
|
|
Copyright (C) 2010 Scott R Parish <srp@srparish.net>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2010-01-09 22:49:06 +00:00
|
|
|
module Database.MongoDB.BSON
|
|
|
|
(
|
2010-01-18 19:24:14 +00:00
|
|
|
-- * Types
|
2010-01-17 17:41:24 +00:00
|
|
|
BsonValue(..),
|
2010-01-21 04:29:27 +00:00
|
|
|
BsonDoc,
|
2010-01-18 05:08:14 +00:00
|
|
|
BinarySubType(..),
|
2010-01-18 21:03:25 +00:00
|
|
|
-- * BsonDoc Operations
|
2010-01-19 00:32:44 +00:00
|
|
|
empty, lookup,
|
2010-01-21 04:29:27 +00:00
|
|
|
-- * Type Conversion
|
|
|
|
fromBson, toBson,
|
|
|
|
fromBsonDoc, toBsonDoc,
|
|
|
|
-- * Binary encoding/decoding
|
|
|
|
getBsonDoc, putBsonDoc,
|
2010-01-09 22:49:06 +00:00
|
|
|
)
|
|
|
|
where
|
2010-01-18 21:03:25 +00:00
|
|
|
import Prelude hiding (lookup)
|
|
|
|
|
2010-01-07 03:56:57 +00:00
|
|
|
import Control.Monad
|
|
|
|
import Data.Binary
|
|
|
|
import Data.Binary.Get
|
|
|
|
import Data.Binary.IEEE754
|
|
|
|
import Data.Binary.Put
|
2010-01-19 00:32:44 +00:00
|
|
|
import Data.ByteString.Char8 as C8 hiding (empty)
|
2010-01-07 03:56:57 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import qualified Data.ByteString.Lazy.UTF8 as L8
|
2010-01-18 05:08:14 +00:00
|
|
|
import qualified Data.ByteString.UTF8 as S8
|
|
|
|
import Data.Convertible
|
2010-01-07 03:56:57 +00:00
|
|
|
import Data.Int
|
2010-01-11 01:26:50 +00:00
|
|
|
import qualified Data.Map as Map
|
2010-01-07 03:56:57 +00:00
|
|
|
import qualified Data.List as List
|
|
|
|
import Data.Time.Clock.POSIX
|
2010-01-18 15:57:25 +00:00
|
|
|
import Data.Typeable
|
2010-01-10 02:45:45 +00:00
|
|
|
import Database.MongoDB.Util
|
2010-01-07 03:56:57 +00:00
|
|
|
|
2010-01-18 19:24:14 +00:00
|
|
|
-- | BsonValue is the type that can be used as a key in a 'BsonDoc'.
|
2010-01-17 17:41:24 +00:00
|
|
|
data BsonValue
|
|
|
|
= BsonDouble Double
|
|
|
|
| BsonString L8.ByteString
|
|
|
|
| BsonObject BsonDoc
|
|
|
|
| BsonArray [BsonValue]
|
|
|
|
| BsonUndefined
|
|
|
|
| BsonBinary BinarySubType L.ByteString
|
|
|
|
| BsonObjectId L.ByteString
|
|
|
|
| BsonBool !Bool
|
|
|
|
| BsonDate POSIXTime
|
|
|
|
| BsonNull
|
|
|
|
| BsonRegex L8.ByteString String
|
|
|
|
| BsonSymbol L8.ByteString
|
|
|
|
| BsonInt32 Int32
|
|
|
|
| BsonInt64 Int64
|
|
|
|
| BsonMinKey
|
|
|
|
| BsonMaxKey
|
2010-01-07 03:56:57 +00:00
|
|
|
deriving (Show, Eq, Ord)
|
|
|
|
|
2010-01-18 15:57:25 +00:00
|
|
|
instance Typeable BsonValue where
|
|
|
|
typeOf _ = mkTypeName "BsonValue"
|
|
|
|
|
2010-01-18 19:24:14 +00:00
|
|
|
-- | BSON Document: this is the top-level (but recursive) type that
|
|
|
|
-- all MongoDB collections work in terms of. It is a mapping between
|
|
|
|
-- strings ('Data.ByteString.Lazu.UTF8.ByteString') and 'BsonValue's.
|
|
|
|
-- It can be constructed either from a 'Map' (eg @'BsonDoc' myMap@) or
|
|
|
|
-- from a associative list (eg @'toBsonDoc' myAL@).
|
2010-01-21 04:29:27 +00:00
|
|
|
type BsonDoc = Map.Map L8.ByteString BsonValue
|
2010-01-21 02:55:33 +00:00
|
|
|
|
2010-01-18 23:59:47 +00:00
|
|
|
class BsonDocOps a where
|
|
|
|
-- | Construct a BsonDoc from an associative list
|
|
|
|
toBsonDoc :: [(a, BsonValue)] -> BsonDoc
|
|
|
|
-- | Unwrap BsonDoc to be a Map
|
2010-01-21 04:29:27 +00:00
|
|
|
fromBsonDoc :: BsonDoc -> [(a, BsonValue)]
|
2010-01-18 23:59:47 +00:00
|
|
|
-- | Return the BsonValue for given key, if any.
|
|
|
|
lookup :: a -> BsonDoc -> Maybe BsonValue
|
2010-01-07 03:56:57 +00:00
|
|
|
|
2010-01-19 00:32:44 +00:00
|
|
|
-- | An empty BsonDoc
|
|
|
|
empty :: BsonDoc
|
2010-01-21 04:29:27 +00:00
|
|
|
empty = Map.empty
|
2010-01-19 00:32:44 +00:00
|
|
|
|
2010-01-18 23:59:47 +00:00
|
|
|
instance BsonDocOps L8.ByteString where
|
2010-01-21 04:29:27 +00:00
|
|
|
toBsonDoc = Map.fromList
|
|
|
|
fromBsonDoc = Map.toList
|
2010-01-25 02:58:49 +00:00
|
|
|
lookup = Map.lookup
|
2010-01-18 23:59:47 +00:00
|
|
|
|
|
|
|
instance BsonDocOps String where
|
2010-01-21 04:29:27 +00:00
|
|
|
toBsonDoc = Map.mapKeys L8.fromString .Map.fromList
|
|
|
|
fromBsonDoc = Map.toList . Map.mapKeys L8.toString
|
2010-01-25 02:58:49 +00:00
|
|
|
lookup = Map.lookup . L8.fromString
|
2010-01-18 21:03:25 +00:00
|
|
|
|
2010-01-07 03:56:57 +00:00
|
|
|
data DataType =
|
2010-01-25 02:58:49 +00:00
|
|
|
DataMinKey | -- -1
|
|
|
|
DataNumber | -- 1
|
|
|
|
DataString | -- 2
|
|
|
|
DataObject | -- 3
|
|
|
|
DataArray | -- 4
|
|
|
|
DataBinary | -- 5
|
|
|
|
DataUndefined | -- 6
|
|
|
|
DataOid | -- 7
|
|
|
|
DataBoolean | -- 8
|
|
|
|
DataDate | -- 9
|
|
|
|
DataNull | -- 10
|
|
|
|
DataRegex | -- 11
|
|
|
|
DataRef | -- 12
|
|
|
|
DataCode | -- 13
|
|
|
|
DataSymbol | -- 14
|
|
|
|
DataCodeWScope | -- 15
|
|
|
|
DataInt | -- 16
|
|
|
|
DataTimestamp | -- 17
|
|
|
|
DataLong | -- 18
|
|
|
|
DataMaxKey -- 127
|
2010-01-07 03:56:57 +00:00
|
|
|
deriving (Show, Read, Enum, Eq, Ord)
|
|
|
|
|
|
|
|
toDataType :: Int -> DataType
|
2010-01-25 02:58:49 +00:00
|
|
|
toDataType (-1) = DataMinKey
|
|
|
|
toDataType 127 = DataMaxKey
|
2010-01-07 03:56:57 +00:00
|
|
|
toDataType d = toEnum d
|
|
|
|
|
|
|
|
fromDataType :: DataType -> Int
|
2010-01-25 02:58:49 +00:00
|
|
|
fromDataType DataMinKey = - 1
|
|
|
|
fromDataType DataMaxKey = 127
|
2010-01-07 03:56:57 +00:00
|
|
|
fromDataType d = fromEnum d
|
|
|
|
|
|
|
|
data BinarySubType =
|
2010-01-25 02:58:49 +00:00
|
|
|
BSTUNDEFINED1 |
|
2010-01-07 03:56:57 +00:00
|
|
|
BSTFunction | -- 1
|
|
|
|
BSTByteArray | -- 2
|
|
|
|
BSTUUID | -- 3
|
2010-01-25 02:58:49 +00:00
|
|
|
BSTUNDEFINED2 |
|
2010-01-07 03:56:57 +00:00
|
|
|
BSTMD5 | -- 5
|
|
|
|
BSTUserDefined
|
|
|
|
deriving (Show, Read, Enum, Eq, Ord)
|
|
|
|
|
|
|
|
toBinarySubType :: Int -> BinarySubType
|
|
|
|
toBinarySubType 0x80 = BSTUserDefined
|
|
|
|
toBinarySubType d = toEnum d
|
|
|
|
|
|
|
|
fromBinarySubType :: BinarySubType -> Int
|
|
|
|
fromBinarySubType BSTUserDefined = 0x80
|
|
|
|
fromBinarySubType d = fromEnum d
|
|
|
|
|
2010-01-21 04:29:27 +00:00
|
|
|
getBsonDoc :: Get BsonDoc
|
|
|
|
getBsonDoc = liftM snd getDoc
|
|
|
|
|
|
|
|
putBsonDoc :: BsonDoc -> Put
|
|
|
|
putBsonDoc = putObj
|
2010-01-07 03:56:57 +00:00
|
|
|
|
2010-01-17 17:41:24 +00:00
|
|
|
getVal :: DataType -> Get (Integer, BsonValue)
|
2010-01-25 02:58:49 +00:00
|
|
|
getVal DataNumber = liftM ((,) 8 . BsonDouble) getFloat64le
|
|
|
|
getVal DataString = do
|
2010-01-07 03:56:57 +00:00
|
|
|
sLen1 <- getI32
|
2010-01-17 03:40:22 +00:00
|
|
|
(_sLen2, s) <- getS
|
2010-01-17 17:41:24 +00:00
|
|
|
return (fromIntegral $ 4 + sLen1, BsonString s)
|
2010-01-25 02:58:49 +00:00
|
|
|
getVal DataObject = getDoc >>= \(len, obj) -> return (len, BsonObject obj)
|
|
|
|
getVal DataArray = do
|
2010-01-07 03:56:57 +00:00
|
|
|
(len, arr) <- getRawObj
|
2010-01-11 01:26:50 +00:00
|
|
|
let arr2 = Map.fold (:) [] arr -- reverse and remove key
|
2010-01-17 17:41:24 +00:00
|
|
|
return (len, BsonArray arr2)
|
2010-01-25 02:58:49 +00:00
|
|
|
getVal DataBinary = do
|
2010-01-07 03:56:57 +00:00
|
|
|
skip 4
|
|
|
|
st <- getI8
|
|
|
|
len2 <- getI32
|
|
|
|
bs <- getLazyByteString $ fromIntegral len2
|
2010-01-17 17:41:24 +00:00
|
|
|
return (4 + 1 + 4 + fromIntegral len2, BsonBinary (toBinarySubType st) bs)
|
2010-01-25 02:58:49 +00:00
|
|
|
getVal DataUndefined = return (1, BsonUndefined)
|
|
|
|
getVal DataOid = liftM ((,) 12 . BsonObjectId) $ getLazyByteString 12
|
|
|
|
getVal DataBoolean = liftM ((,) (1::Integer) . BsonBool . (/= (0::Int))) getI8
|
|
|
|
getVal DataDate = liftM ((,) 8 . BsonDate . flip (/) 1000 . realToFrac) getI64
|
|
|
|
getVal DataNull = return (1, BsonNull)
|
|
|
|
getVal DataRegex = fail "DataCode not yet supported" -- TODO
|
|
|
|
getVal DataRef = fail "DataRef is deprecated"
|
|
|
|
getVal DataCode = fail "DataCode not yet supported" -- TODO
|
|
|
|
getVal DataSymbol = do
|
2010-01-17 03:40:22 +00:00
|
|
|
sLen1 <- getI32
|
|
|
|
(_sLen2, s) <- getS
|
2010-01-17 17:41:24 +00:00
|
|
|
return (fromIntegral $ 4 + sLen1, BsonString s)
|
2010-01-25 02:58:49 +00:00
|
|
|
getVal DataCodeWScope = fail "DataCodeWScope not yet supported" -- TODO
|
|
|
|
getVal DataInt = liftM ((,) 4 . BsonInt32 . fromIntegral) getI32
|
|
|
|
getVal DataTimestamp = fail "DataTimestamp not yet supported" -- TODO
|
2010-01-17 03:40:22 +00:00
|
|
|
|
2010-01-25 02:58:49 +00:00
|
|
|
getVal DataLong = liftM ((,) 8 . BsonInt64) getI64
|
|
|
|
getVal DataMinKey = return (0, BsonMinKey)
|
|
|
|
getVal DataMaxKey = return (0, BsonMaxKey)
|
2010-01-07 03:56:57 +00:00
|
|
|
|
2010-01-21 04:29:27 +00:00
|
|
|
getInnerObj :: Int32 -> BsonDoc -> Get BsonDoc
|
|
|
|
getInnerObj 1 obj = return obj
|
2010-01-07 03:56:57 +00:00
|
|
|
getInnerObj bytesLeft obj = do
|
|
|
|
typ <- getDataType
|
|
|
|
(keySz, key) <- getS
|
|
|
|
(valSz, val) <- getVal typ
|
2010-01-10 02:45:45 +00:00
|
|
|
getInnerObj (bytesLeft - 1 - fromIntegral keySz - fromIntegral valSz) $
|
2010-01-25 02:58:49 +00:00
|
|
|
Map.insert key val obj
|
2010-01-07 03:56:57 +00:00
|
|
|
|
2010-01-21 04:29:27 +00:00
|
|
|
getRawObj :: Get (Integer, BsonDoc)
|
2010-01-07 03:56:57 +00:00
|
|
|
getRawObj = do
|
|
|
|
bytes <- getI32
|
2010-01-21 04:29:27 +00:00
|
|
|
obj <- getInnerObj (bytes - 4) empty
|
2010-01-07 03:56:57 +00:00
|
|
|
getNull
|
2010-01-10 02:45:45 +00:00
|
|
|
return (fromIntegral bytes, obj)
|
2010-01-07 03:56:57 +00:00
|
|
|
|
2010-01-17 17:41:24 +00:00
|
|
|
getDoc :: Get (Integer, BsonDoc)
|
2010-01-21 04:29:27 +00:00
|
|
|
getDoc = getRawObj
|
2010-01-07 03:56:57 +00:00
|
|
|
|
2010-01-17 03:40:22 +00:00
|
|
|
getDataType :: Get DataType
|
2010-01-07 03:56:57 +00:00
|
|
|
getDataType = liftM toDataType getI8
|
|
|
|
|
2010-01-17 17:41:24 +00:00
|
|
|
putType :: BsonValue -> Put
|
2010-01-25 02:58:49 +00:00
|
|
|
putType BsonDouble{} = putDataType DataNumber
|
|
|
|
putType BsonString{} = putDataType DataString
|
|
|
|
putType BsonObject{} = putDataType DataObject
|
|
|
|
putType BsonArray{} = putDataType DataArray
|
|
|
|
putType BsonBinary{} = putDataType DataBinary
|
|
|
|
putType BsonUndefined = putDataType DataUndefined
|
|
|
|
putType BsonObjectId{} = putDataType DataOid
|
|
|
|
putType BsonBool{} = putDataType DataBoolean
|
|
|
|
putType BsonDate{} = putDataType DataDate
|
|
|
|
putType BsonNull = putDataType DataNull
|
|
|
|
putType BsonRegex{} = putDataType DataRegex
|
|
|
|
-- putType = putDataType DataRef
|
|
|
|
-- putType = putDataType DataCode
|
|
|
|
putType BsonSymbol{} = putDataType DataSymbol
|
|
|
|
-- putType = putDataType DataCodeWScope
|
|
|
|
putType BsonInt32 {} = putDataType DataInt
|
|
|
|
putType BsonInt64 {} = putDataType DataLong
|
|
|
|
-- putType = putDataType DataTimestamp
|
|
|
|
putType BsonMinKey = putDataType DataMinKey
|
|
|
|
putType BsonMaxKey = putDataType DataMaxKey
|
2010-01-17 17:41:24 +00:00
|
|
|
|
|
|
|
putVal :: BsonValue -> Put
|
|
|
|
putVal (BsonDouble d) = putFloat64le d
|
|
|
|
putVal (BsonString s) = putI32 (fromIntegral $ 1 + L8.length s) >> putS s
|
|
|
|
putVal (BsonObject o) = putObj o
|
|
|
|
putVal (BsonArray es) = putOutterObj bs
|
2010-01-17 03:40:22 +00:00
|
|
|
where bs = runPut $ forM_ (List.zip [(0::Int) .. ] es) $ \(i, e) ->
|
2010-01-25 02:58:49 +00:00
|
|
|
putType e >> putS (L8.fromString $ show i) >> putVal e
|
2010-01-17 17:41:24 +00:00
|
|
|
putVal (BsonBinary t bs)= do putI32 $ fromIntegral $ 4 + L.length bs
|
|
|
|
putI8 $ fromBinarySubType t
|
|
|
|
putI32 $ fromIntegral $ L.length bs
|
|
|
|
putLazyByteString bs
|
|
|
|
putVal BsonUndefined = putNothing
|
|
|
|
putVal (BsonObjectId o) = putLazyByteString o
|
|
|
|
putVal (BsonBool False) = putI8 (0::Int)
|
|
|
|
putVal (BsonBool True) = putI8 (1::Int)
|
|
|
|
putVal (BsonDate pt) = putI64 $ round $ 1000 * (realToFrac pt :: Double)
|
|
|
|
putVal BsonNull = putNothing
|
|
|
|
putVal (BsonRegex r opt)= do putS r
|
|
|
|
putByteString $ pack $ List.sort opt
|
|
|
|
putNull
|
|
|
|
putVal (BsonSymbol s) = putI32 (fromIntegral $ 1 + L8.length s) >> putS s
|
|
|
|
putVal (BsonInt32 i) = putI32 i
|
|
|
|
putVal (BsonInt64 i) = putI64 i
|
|
|
|
putVal BsonMinKey = putNothing
|
|
|
|
putVal BsonMaxKey = putNothing
|
|
|
|
|
|
|
|
putObj :: BsonDoc -> Put
|
2010-01-07 03:56:57 +00:00
|
|
|
putObj obj = putOutterObj bs
|
2010-01-21 04:29:27 +00:00
|
|
|
where bs = runPut $ forM_ (fromBsonDoc obj) $ \(k, v) ->
|
2010-01-07 03:56:57 +00:00
|
|
|
putType v >> putS k >> putVal v
|
|
|
|
|
2010-01-17 03:40:22 +00:00
|
|
|
putOutterObj :: L.ByteString -> Put
|
2010-01-07 03:56:57 +00:00
|
|
|
putOutterObj bytes = do
|
|
|
|
-- the length prefix and null term are included in the length
|
2010-01-10 02:45:45 +00:00
|
|
|
putI32 $ fromIntegral $ 4 + 1 + L.length bytes
|
2010-01-07 03:56:57 +00:00
|
|
|
putLazyByteString bytes
|
|
|
|
putNull
|
|
|
|
|
2010-01-17 03:40:22 +00:00
|
|
|
putDataType :: DataType -> Put
|
2010-01-07 03:56:57 +00:00
|
|
|
putDataType = putI8 . fromDataType
|
2010-01-18 05:08:14 +00:00
|
|
|
|
2010-01-18 16:22:57 +00:00
|
|
|
class BsonConv a b where
|
2010-01-18 19:24:14 +00:00
|
|
|
-- | Convert a BsonValue into a native Haskell type.
|
2010-01-18 16:22:57 +00:00
|
|
|
fromBson :: Convertible a b => a -> b
|
2010-01-18 19:24:14 +00:00
|
|
|
-- | Convert a native Haskell type into a BsonValue.
|
2010-01-18 16:22:57 +00:00
|
|
|
toBson :: Convertible b a => b -> a
|
2010-01-18 15:57:25 +00:00
|
|
|
|
2010-01-18 16:22:57 +00:00
|
|
|
instance BsonConv BsonValue a where
|
|
|
|
fromBson = convert
|
|
|
|
toBson = convert
|
|
|
|
|
|
|
|
instance BsonConv (Maybe BsonValue) (Maybe a) where
|
|
|
|
fromBson = convert
|
|
|
|
toBson = convert
|
2010-01-18 05:08:14 +00:00
|
|
|
|
2010-01-18 15:57:25 +00:00
|
|
|
unsupportedError :: (Typeable a, Convertible BsonValue a) =>
|
|
|
|
BsonValue -> ConvertResult a
|
|
|
|
unsupportedError = convError "Unsupported conversion"
|
|
|
|
|
2010-01-18 05:08:14 +00:00
|
|
|
instance Convertible Double BsonValue where
|
|
|
|
safeConvert = return . BsonDouble
|
|
|
|
|
|
|
|
instance Convertible Float BsonValue where
|
|
|
|
safeConvert = return . BsonDouble . realToFrac
|
|
|
|
|
|
|
|
instance Convertible String BsonValue where
|
|
|
|
safeConvert = return . BsonString . L8.fromString
|
|
|
|
|
|
|
|
instance Convertible L8.ByteString BsonValue where
|
|
|
|
safeConvert = return . BsonString
|
|
|
|
|
|
|
|
instance Convertible S8.ByteString BsonValue where
|
|
|
|
safeConvert = return . BsonString . L.fromChunks . return
|
|
|
|
|
|
|
|
instance Convertible [Double] BsonValue where
|
|
|
|
safeConvert ds = BsonArray `liftM` mapM safeConvert ds
|
|
|
|
|
|
|
|
instance Convertible [Float] BsonValue where
|
|
|
|
safeConvert fs = BsonArray `liftM` mapM safeConvert fs
|
|
|
|
|
|
|
|
instance Convertible [String] BsonValue where
|
|
|
|
safeConvert ss = BsonArray `liftM` mapM safeConvert ss
|
|
|
|
|
|
|
|
instance Convertible [L8.ByteString] BsonValue where
|
|
|
|
safeConvert bs = BsonArray `liftM` mapM safeConvert bs
|
|
|
|
|
|
|
|
instance Convertible [S8.ByteString] BsonValue where
|
|
|
|
safeConvert bs = BsonArray `liftM` mapM safeConvert bs
|
|
|
|
|
2010-01-21 02:55:33 +00:00
|
|
|
instance Convertible BsonDoc BsonValue where
|
|
|
|
safeConvert = return . BsonObject
|
|
|
|
|
2010-01-21 04:29:27 +00:00
|
|
|
instance Convertible (Map.Map String BsonValue) BsonValue where
|
|
|
|
safeConvert = return . BsonObject . Map.mapKeys L8.fromString
|
2010-01-21 02:55:33 +00:00
|
|
|
|
|
|
|
instance Convertible [(L8.ByteString, BsonValue)] BsonValue where
|
|
|
|
safeConvert = return . BsonObject . toBsonDoc
|
|
|
|
|
2010-01-21 04:29:27 +00:00
|
|
|
instance Convertible [(String, BsonValue)] BsonValue where
|
|
|
|
safeConvert = return . BsonObject . toBsonDoc
|
2010-01-21 02:55:33 +00:00
|
|
|
|
2010-01-18 05:08:14 +00:00
|
|
|
instance Convertible [Bool] BsonValue where
|
|
|
|
safeConvert bs = BsonArray `liftM` mapM safeConvert bs
|
|
|
|
|
|
|
|
instance Convertible [POSIXTime] BsonValue where
|
|
|
|
safeConvert ts = BsonArray `liftM` mapM safeConvert ts
|
|
|
|
|
|
|
|
instance Convertible [Int] BsonValue where
|
|
|
|
safeConvert is = BsonArray `liftM` mapM safeConvert is
|
|
|
|
|
|
|
|
instance Convertible [Integer] BsonValue where
|
|
|
|
safeConvert is = BsonArray `liftM` mapM safeConvert is
|
|
|
|
|
|
|
|
instance Convertible [Int32] BsonValue where
|
|
|
|
safeConvert is = BsonArray `liftM` mapM safeConvert is
|
|
|
|
|
|
|
|
instance Convertible [Int64] BsonValue where
|
|
|
|
safeConvert is = BsonArray `liftM` mapM safeConvert is
|
|
|
|
|
|
|
|
instance Convertible POSIXTime BsonValue where
|
|
|
|
safeConvert = return . BsonDate
|
|
|
|
|
|
|
|
instance Convertible Bool BsonValue where
|
|
|
|
safeConvert = return . BsonBool
|
|
|
|
|
|
|
|
instance Convertible Int BsonValue where
|
2010-01-25 02:58:49 +00:00
|
|
|
safeConvert i = if i >= fromIntegral (minBound::Int32) &&
|
|
|
|
i <= fromIntegral (maxBound::Int32)
|
2010-01-18 05:08:14 +00:00
|
|
|
then return $ BsonInt32 $ fromIntegral i
|
|
|
|
else return $ BsonInt64 $ fromIntegral i
|
|
|
|
|
|
|
|
instance Convertible Integer BsonValue where
|
2010-01-25 02:58:49 +00:00
|
|
|
safeConvert i = if i >= fromIntegral (minBound::Int32) &&
|
|
|
|
i <= fromIntegral (maxBound::Int32)
|
2010-01-18 05:08:14 +00:00
|
|
|
then return $ BsonInt32 $ fromIntegral i
|
|
|
|
else return $ BsonInt64 $ fromIntegral i
|
|
|
|
|
|
|
|
instance Convertible Int32 BsonValue where
|
2010-01-25 02:58:49 +00:00
|
|
|
safeConvert = return . BsonInt32
|
2010-01-18 05:08:14 +00:00
|
|
|
|
|
|
|
instance Convertible Int64 BsonValue where
|
2010-01-25 02:58:49 +00:00
|
|
|
safeConvert = return . BsonInt64
|
2010-01-18 15:57:25 +00:00
|
|
|
|
2010-01-18 16:22:57 +00:00
|
|
|
instance (Convertible a BsonValue) =>
|
|
|
|
Convertible (Maybe a) BsonValue where
|
|
|
|
safeConvert Nothing = return BsonNull
|
|
|
|
safeConvert (Just a) = safeConvert a
|
|
|
|
|
2010-01-18 15:57:25 +00:00
|
|
|
instance Convertible BsonValue Double where
|
|
|
|
safeConvert (BsonDouble d) = return d
|
|
|
|
safeConvert (BsonInt32 i) = safeConvert i
|
|
|
|
safeConvert (BsonInt64 i) = safeConvert i
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue Float where
|
|
|
|
safeConvert (BsonDouble d) = safeConvert d
|
|
|
|
safeConvert (BsonInt32 i) = safeConvert i
|
|
|
|
safeConvert (BsonInt64 i) = safeConvert i
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue String where
|
|
|
|
safeConvert (BsonString bs) = return $ L8.toString bs
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue L8.ByteString where
|
|
|
|
safeConvert (BsonString bs) = return bs
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue S8.ByteString where
|
|
|
|
safeConvert (BsonString bs) = return $ C8.concat $ L.toChunks bs
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
2010-01-21 02:55:33 +00:00
|
|
|
instance Convertible BsonValue BsonDoc where
|
|
|
|
safeConvert (BsonObject o) = return o
|
|
|
|
safeConvert v = unsupportedError v
|
2010-01-20 02:30:04 +00:00
|
|
|
|
2010-01-21 02:55:33 +00:00
|
|
|
instance Convertible BsonValue (Map.Map String BsonValue) where
|
2010-01-21 04:29:27 +00:00
|
|
|
safeConvert (BsonObject o) = return $ Map.mapKeys L8.toString o
|
2010-01-21 02:55:33 +00:00
|
|
|
safeConvert v = unsupportedError v
|
2010-01-20 02:30:04 +00:00
|
|
|
|
2010-01-21 02:55:33 +00:00
|
|
|
instance Convertible BsonValue [(String, BsonValue)] where
|
2010-01-21 04:29:27 +00:00
|
|
|
safeConvert (BsonObject o) = return $ fromBsonDoc o
|
2010-01-21 02:55:33 +00:00
|
|
|
safeConvert v = unsupportedError v
|
2010-01-20 02:30:04 +00:00
|
|
|
|
2010-01-21 02:55:33 +00:00
|
|
|
instance Convertible BsonValue [(L8.ByteString, BsonValue)] where
|
2010-01-21 04:29:27 +00:00
|
|
|
safeConvert (BsonObject o) = return $ fromBsonDoc o
|
2010-01-21 02:55:33 +00:00
|
|
|
safeConvert v = unsupportedError v
|
2010-01-20 02:30:04 +00:00
|
|
|
|
2010-01-18 15:57:25 +00:00
|
|
|
instance Convertible BsonValue [Double] where
|
|
|
|
safeConvert (BsonArray a) = mapM safeConvert a
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue [Float] where
|
|
|
|
safeConvert (BsonArray a) = mapM safeConvert a
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue [String] where
|
|
|
|
safeConvert (BsonArray a) = mapM safeConvert a
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue [Bool] where
|
|
|
|
safeConvert (BsonArray a) = mapM safeConvert a
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue [POSIXTime] where
|
|
|
|
safeConvert (BsonArray a) = mapM safeConvert a
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue [Int32] where
|
|
|
|
safeConvert (BsonArray a) = mapM safeConvert a
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue [Int64] where
|
|
|
|
safeConvert (BsonArray a) = mapM safeConvert a
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue Bool where
|
|
|
|
safeConvert (BsonBool b) = return b
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue POSIXTime where
|
|
|
|
safeConvert (BsonDate t) = return t
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue Int where
|
|
|
|
safeConvert (BsonDouble d) = safeConvert d
|
|
|
|
safeConvert (BsonInt32 d) = safeConvert d
|
|
|
|
safeConvert (BsonInt64 d) = safeConvert d
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue Integer where
|
|
|
|
safeConvert (BsonDouble d) = safeConvert d
|
|
|
|
safeConvert (BsonInt32 d) = safeConvert d
|
|
|
|
safeConvert (BsonInt64 d) = safeConvert d
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue Int32 where
|
|
|
|
safeConvert (BsonDouble d) = safeConvert d
|
|
|
|
safeConvert (BsonInt32 d) = return d
|
|
|
|
safeConvert (BsonInt64 d) = safeConvert d
|
|
|
|
safeConvert v = unsupportedError v
|
|
|
|
|
|
|
|
instance Convertible BsonValue Int64 where
|
|
|
|
safeConvert (BsonDouble d) = safeConvert d
|
|
|
|
safeConvert (BsonInt32 d) = safeConvert d
|
|
|
|
safeConvert (BsonInt64 d) = return d
|
|
|
|
safeConvert v = unsupportedError v
|
2010-01-18 16:22:57 +00:00
|
|
|
|
|
|
|
instance (Convertible BsonValue a) =>
|
|
|
|
Convertible (Maybe BsonValue) (Maybe a) where
|
|
|
|
safeConvert Nothing = return Nothing
|
|
|
|
safeConvert (Just a) = liftM Just $ safeConvert a
|