git.iko.soy/typeable/bloodhound
on ff6abcb help sign in
81 lines 3.3 kB history raw
1module Bloodhound.Import
2 ( module X
3 , LByteString
4 , Method
5 , omitNulls
6 , parseNEJSON
7 , parseReadText
8 , readMay
9 , showText
10 , deleteSeveral
11 , oPath
12 ) where
14import Control.Applicative as X (Alternative (..), optional)
15import Control.Exception as X (Exception)
16import Control.Monad as X (MonadPlus (..), forM, (<=<))
17import Control.Monad.Catch as X (MonadCatch, MonadMask,
18 MonadThrow)
19import Control.Monad.Except as X (MonadError)
20import Control.Monad.Fix as X (MonadFix)
21import Control.Monad.IO.Class as X (MonadIO (..))
22import Control.Monad.Reader as X (MonadReader (..),
23 MonadTrans (..), ReaderT (..))
24import Control.Monad.State as X (MonadState)
25import Control.Monad.Writer as X (MonadWriter)
26import Data.Aeson as X
27import Data.Aeson.Types as X (Pair, Parser, emptyObject,
28 parseEither, parseMaybe,
29 typeMismatch)
30import Data.Bifunctor as X (first)
31import Data.Char as X (isNumber)
32import Data.Hashable as X (Hashable)
33import qualified Data.HashMap.Strict as HM
34import Data.List as X (foldl', intercalate, nub)
35import Data.List.NonEmpty as X (NonEmpty (..), toList)
36import Data.Maybe as X (catMaybes, fromMaybe,
37 isNothing, maybeToList)
38import Data.Scientific as X (Scientific)
39import Data.Semigroup as X (Semigroup (..))
40import Data.Text as X (Text)
41import Data.Time.Calendar as X (Day (..), showGregorian)
42import Data.Time.Clock as X (NominalDiffTime, UTCTime)
43import Data.Time.Clock.POSIX as X
45import qualified Data.ByteString.Lazy as BL
46import qualified Data.Text as T
47import qualified Data.Traversable as DT
48import qualified Data.Vector as V
49import qualified Network.HTTP.Types.Method as NHTM
51type LByteString = BL.ByteString
53type Method = NHTM.Method
55readMay :: Read a => String -> Maybe a
56readMay s = case reads s of
57 (a, ""):_ -> Just a
58 _ -> Nothing
60parseReadText :: Read a => Text -> Parser a
61parseReadText = maybe mzero return . readMay . T.unpack
63showText :: Show a => a -> Text
64showText = T.pack . show
66omitNulls :: [(Text, Value)] -> Value
67omitNulls = object . filter notNull where
68 notNull (_, Null) = False
69 notNull (_, Array a) = (not . V.null) a
70 notNull _ = True
72parseNEJSON :: (FromJSON a) => [Value] -> Parser (NonEmpty a)
73parseNEJSON [] = fail "Expected non-empty list"
74parseNEJSON (x:xs) = DT.mapM parseJSON (x :| xs)
76deleteSeveral :: (Eq k, Hashable k) => [k] -> HM.HashMap k v -> HM.HashMap k v
77deleteSeveral ks hm = foldr HM.delete hm ks
79oPath :: ToJSON a => NonEmpty Text -> a -> Value
80oPath (k :| []) v = object [k .= v]
81oPath (k:| (h:t)) v = object [k .= oPath (h :| t) v]