{-# LANGUAGE FlexibleInstances #-}
module Database.Bolt.Value.Type where
import Control.Monad.State (StateT (..), evalStateT)
import Data.ByteString (ByteString)
import Data.Map.Strict (Map, fromList)
import Data.Text (Text, pack)
import Data.Word (Word8)
type UnpackT = StateT ByteString
data Structure = Structure { signature :: Word8
, fields :: [Value]
}
deriving (Show, Eq)
class FromStructure a where
fromStructure :: Monad m => Structure -> m a
class ToStructure a where
toStructure :: a -> Structure
class BoltValue a where
pack :: a -> ByteString
unpackT :: Monad m => UnpackT m a
unpack :: Monad m => ByteString -> m a
unpack = evalStateT unpackT
data Value = N ()
| B Bool
| I Int
| F Double
| T Text
| L [Value]
| M (Map Text Value)
| S Structure
deriving (Show, Eq)
class IsValue a where
toValue :: a -> Value
toValueList :: [a] -> Value
toValueList = L . fmap toValue
instance IsValue () where
toValue = N
instance IsValue Int where
toValue = I
instance IsValue Integer where
toValue = I . fromIntegral
instance IsValue Double where
toValue = F
instance IsValue Float where
toValue = F . realToFrac
instance IsValue Text where
toValue = T
instance IsValue Char where
toValue = toValueList . pure
toValueList = T . Data.Text.pack
instance IsValue a => IsValue [a] where
toValue = toValueList
instance IsValue (Map Text Value) where
toValue = M
(=:) :: IsValue a => Text -> a -> (Text, Value)
(=:) key val = (key, toValue val)
props :: [(Text, Value)] -> Map Text Value
props = fromList
data Node = Node { nodeIdentity :: Int
, labels :: [Text]
, nodeProps :: Map Text Value
}
deriving (Show, Eq)
data Relationship = Relationship { relIdentity :: Int
, startNodeId :: Int
, endNodeId :: Int
, relType :: Text
, relProps :: Map Text Value
}
deriving (Show, Eq)
data URelationship = URelationship { urelIdentity :: Int
, urelType :: Text
, urelProps :: Map Text Value
}
deriving (Show, Eq)
data Path = Path { pathNodes :: [Node]
, pathRelationships :: [URelationship]
, pathSequence :: [Int]
}
deriving (Show, Eq)