packages feed

lucienne-0.0.1: Lucienne/Model/Util.hs

module Lucienne.Model.Util
  (maybeAddId, showObjectId, readObjectId, showValue, objectId)
where

import           Data.Bson (Document,Value(ObjId,String),(=:),ObjectId(Oid))
import           Numeric (showHex,readHex)
import qualified Data.CompactString.UTF8 as CS

maybeAddId :: (a -> Maybe ObjectId) -> a -> Document -> Document
maybeAddId getId a document = 
  case getId a of
    Nothing -> document
    Just id -> ("_id" =: id) : document

showObjectId :: ObjectId -> String
showObjectId (Oid a b) = showHex a $ showHex b ""

readObjectId :: String -> ObjectId
readObjectId s = Oid (fst $ head $ readHex $ take 8 s) 
                     (fst $ head $ readHex $ drop 8 s)
                 
showValue :: Value -> String
showValue value =
  case value of
    ObjId objId -> showObjectId objId
    String s    -> CS.unpack s
    
objectId :: Value -> ObjectId
objectId (ObjId objId) = objId