add BsonDocOps class so document can be specified with String keys

This commit is contained in:
Scott R. Parish 2010-01-18 17:59:47 -06:00
parent 121dcbfd93
commit 30925bc90f

View file

@ -84,18 +84,28 @@ instance Typeable BsonValue where
-- It can be constructed either from a 'Map' (eg @'BsonDoc' myMap@) or -- It can be constructed either from a 'Map' (eg @'BsonDoc' myMap@) or
-- from a associative list (eg @'toBsonDoc' myAL@). -- from a associative list (eg @'toBsonDoc' myAL@).
newtype BsonDoc = BsonDoc { newtype BsonDoc = BsonDoc {
fromBsonDoc :: Map.Map L8.ByteString BsonValue bdFromBsonDoc :: Map.Map L8.ByteString BsonValue
} }
deriving (Eq, Ord, Show) deriving (Eq, Ord, Show)
-- | Construct a 'BsonDoc' out of an associative list. class BsonDocOps a where
toBsonDoc :: [(L8.ByteString, BsonValue)] -> BsonDoc -- | Construct a BsonDoc from an associative list
toBsonDoc = BsonDoc . Map.fromList toBsonDoc :: [(a, BsonValue)] -> BsonDoc
-- | Unwrap BsonDoc to be a Map
fromBsonDoc :: BsonDoc -> Map.Map a BsonValue
-- | Return the BsonValue for given key, if any. -- | Return the BsonValue for given key, if any.
lookup :: L8.ByteString -> BsonDoc -> Maybe BsonValue lookup :: a -> BsonDoc -> Maybe BsonValue
instance BsonDocOps L8.ByteString where
toBsonDoc = BsonDoc . Map.fromList
fromBsonDoc = bdFromBsonDoc
lookup k = Map.lookup k . fromBsonDoc lookup k = Map.lookup k . fromBsonDoc
instance BsonDocOps String where
toBsonDoc = BsonDoc . Map.mapKeys L8.fromString .Map.fromList
fromBsonDoc = Map.mapKeys L8.toString . bdFromBsonDoc
lookup k = Map.lookup (L8.fromString k) . fromBsonDoc
data DataType = data DataType =
Data_min_key | -- -1 Data_min_key | -- -1
Data_number | -- 1 Data_number | -- 1