/
Utils.hs
71 lines (54 loc) · 2.2 KB
/
Utils.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module Snap.Extension.DB.MongoDB.Utils where
import Control.Applicative
import Control.Monad
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.ByteString (ByteString)
import Data.Maybe (fromJust)
import qualified Data.Text as T
import qualified Data.CompactString.Internal as CSI
import qualified Data.CompactString.UTF8 as CS
import Data.UString (u)
import qualified Data.Map as Map
import Data.Map (Map)
import Data.Word (Word8)
import Data.Time
import Database.MongoDB
import Database.MongoDB as DB
import Numeric (showHex, readHex)
import Safe
import Snap.Types
import Snap.Auth
import Snap.Extension
------------------------------------------------------------------------------
-- | Convert 'ObjectId' into 'ByteString'
objid2bs :: ObjectId -> ByteString
objid2bs (Oid a b) = B8.pack . showHex a . showChar '-' . showHex b $ ""
------------------------------------------------------------------------------
-- | Convert 'ByteString' into 'ObjectId'
bs2objid :: ByteString -> Maybe ObjectId
bs2objid bs = do
case B8.split '-' bs of
(a':b':_) -> do
a <- fmap fst . headMay . readHex . B8.unpack $ a'
b <- fmap fst . headMay . readHex . B8.unpack $ b'
return $ Oid a b
_ -> Nothing
------------------------------------------------------------------------------
-- | Like 'bs2objid', but may blow with an error if the 'ByteString' can't be
-- converted to an 'ObjectId'
bs2objid' :: ByteString -> ObjectId
bs2objid' = fromJust . bs2objid
bs2cs :: ByteString -> UString
bs2cs = CSI.CS
------------------------------------------------------------------------------
-- | If the 'Document' has an 'ObjectId' in the given field, return it as
-- 'ByteString'
getObjId :: UString -> Document -> Maybe ByteString
getObjId v d = Database.MongoDB.lookup v d >>= fmap objid2bs
-- | Easy lookup from Snap's 'Params'
lp :: ByteString -> Params -> Maybe ByteString
lp n m = Map.lookup n m >>= headMay