BsonValue convertible instances so fromBson can unwrap BsonObjects
This commit is contained in:
parent
b413958ef7
commit
66ad002718
1 changed files with 33 additions and 10 deletions
|
@ -88,6 +88,9 @@ newtype BsonDoc = BsonDoc {
|
||||||
}
|
}
|
||||||
deriving (Eq, Ord, Show)
|
deriving (Eq, Ord, Show)
|
||||||
|
|
||||||
|
instance Typeable BsonDoc where
|
||||||
|
typeOf _ = mkTypeName "BsonDoc"
|
||||||
|
|
||||||
class BsonDocOps a where
|
class BsonDocOps a where
|
||||||
-- | Construct a BsonDoc from an associative list
|
-- | Construct a BsonDoc from an associative list
|
||||||
toBsonDoc :: [(a, BsonValue)] -> BsonDoc
|
toBsonDoc :: [(a, BsonValue)] -> BsonDoc
|
||||||
|
@ -338,6 +341,21 @@ instance Convertible [L8.ByteString] BsonValue where
|
||||||
instance Convertible [S8.ByteString] BsonValue where
|
instance Convertible [S8.ByteString] BsonValue where
|
||||||
safeConvert bs = BsonArray `liftM` mapM safeConvert bs
|
safeConvert bs = BsonArray `liftM` mapM safeConvert bs
|
||||||
|
|
||||||
|
instance Convertible BsonDoc BsonValue where
|
||||||
|
safeConvert = return . BsonObject
|
||||||
|
|
||||||
|
instance Convertible [(String, BsonValue)] BsonValue where
|
||||||
|
safeConvert = return . BsonObject . toBsonDoc
|
||||||
|
|
||||||
|
instance Convertible [(L8.ByteString, BsonValue)] BsonValue where
|
||||||
|
safeConvert = return . BsonObject . toBsonDoc
|
||||||
|
|
||||||
|
instance Convertible (Map.Map String BsonValue) BsonValue where
|
||||||
|
safeConvert = return . BsonObject . BsonDoc . Map.mapKeys L8.fromString
|
||||||
|
|
||||||
|
instance Convertible (Map.Map L8.ByteString BsonValue) BsonValue where
|
||||||
|
safeConvert = return . BsonObject . BsonDoc
|
||||||
|
|
||||||
instance Convertible [Bool] BsonValue where
|
instance Convertible [Bool] BsonValue where
|
||||||
safeConvert bs = BsonArray `liftM` mapM safeConvert bs
|
safeConvert bs = BsonArray `liftM` mapM safeConvert bs
|
||||||
|
|
||||||
|
@ -409,20 +427,25 @@ instance Convertible BsonValue S8.ByteString where
|
||||||
safeConvert (BsonString bs) = return $ C8.concat $ L.toChunks bs
|
safeConvert (BsonString bs) = return $ C8.concat $ L.toChunks bs
|
||||||
safeConvert v = unsupportedError v
|
safeConvert v = unsupportedError v
|
||||||
|
|
||||||
instance Convertible BsonDoc BsonValue where
|
instance Convertible BsonValue BsonDoc where
|
||||||
safeConvert = return . BsonObject
|
safeConvert (BsonObject o) = return o
|
||||||
|
safeConvert v = unsupportedError v
|
||||||
|
|
||||||
instance Convertible [(String, BsonValue)] BsonValue where
|
instance Convertible BsonValue (Map.Map L8.ByteString BsonValue) where
|
||||||
safeConvert = return . BsonObject . toBsonDoc
|
safeConvert (BsonObject o) = return $ fromBsonDoc $ o
|
||||||
|
safeConvert v = unsupportedError v
|
||||||
|
|
||||||
instance Convertible [(L8.ByteString, BsonValue)] BsonValue where
|
instance Convertible BsonValue (Map.Map String BsonValue) where
|
||||||
safeConvert = return . BsonObject . toBsonDoc
|
safeConvert (BsonObject o) = return $ fromBsonDoc $ o
|
||||||
|
safeConvert v = unsupportedError v
|
||||||
|
|
||||||
instance Convertible (Map.Map String BsonValue) BsonValue where
|
instance Convertible BsonValue [(String, BsonValue)] where
|
||||||
safeConvert = return . BsonObject . BsonDoc . Map.mapKeys L8.fromString
|
safeConvert (BsonObject o) = return $ Map.toList $ fromBsonDoc o
|
||||||
|
safeConvert v = unsupportedError v
|
||||||
|
|
||||||
instance Convertible (Map.Map L8.ByteString BsonValue) BsonValue where
|
instance Convertible BsonValue [(L8.ByteString, BsonValue)] where
|
||||||
safeConvert = return . BsonObject . BsonDoc
|
safeConvert (BsonObject o) = return $ Map.toList $ fromBsonDoc o
|
||||||
|
safeConvert v = unsupportedError v
|
||||||
|
|
||||||
instance Convertible BsonValue [Double] where
|
instance Convertible BsonValue [Double] where
|
||||||
safeConvert (BsonArray a) = mapM safeConvert a
|
safeConvert (BsonArray a) = mapM safeConvert a
|
||||||
|
|
Loading…
Reference in a new issue