{-# OPTIONS_GHC -fno-warn-orphans #-}
module Database.Bolt.Extras.Utils
(
dummyId
, union
, currentLoc
, exactValues
, exactValuesM
, exact
) where
import Control.Monad.IO.Class (MonadIO (..))
import Data.List (nub)
import Data.Map.Strict as M ((!), (!?))
import qualified Data.Map.Strict as M (union)
import Data.Text (Text)
import Database.Bolt as B (BoltActionT, Node (..), Record,
RecordValue (..), Value (..))
import Language.Haskell.TH (Exp (..), Lit (..), Loc (..), Q,
location)
import Text.Printf (printf)
dummyId :: Int
dummyId = -1
union :: Node -> Node -> Node
(Node _ labels1 props1) `union` (Node _ labels2 props2) = Node dummyId
(nub $ labels1 ++ labels2)
(props1 `M.union` props2)
currentLoc :: Q Exp
currentLoc = do
loc <- location
pure $ LitE $ StringL $ printf "%s:%d: " (loc_module loc) (fst $ loc_start loc)
{-# DEPRECATED exact "This function exists for compatibility, consider using pure exactEither or exactMaybe instead." #-}
exact :: (MonadIO m, RecordValue a) => Value -> m a
exact = either (liftIO . fail . show) pure . exactEither
exactValues :: (MonadIO m, RecordValue a) => Text -> [Record] -> m [a]
exactValues var = mapM (exact . (! var))
exactValuesM :: (MonadIO m, RecordValue a) => Text -> [Record] -> BoltActionT m [Maybe a]
exactValuesM var = mapM (safeExact . (!? var))
where
safeExact :: (MonadIO m, RecordValue a) => Maybe B.Value -> BoltActionT m (Maybe a)
safeExact Nothing = pure Nothing
safeExact (Just (N ())) = pure Nothing
safeExact (Just x ) = Just <$> exact x