{-# LANGUAGE OverloadedStrings, TypeFamilies #-}
module Data.Greskell.Gremlin
(
Predicate(..),
PredicateA(..),
P,
pNot,
pEq,
pNeq,
pLt,
pLte,
pGt,
pGte,
pInside,
pOutside,
pBetween,
pWithin,
pWithout,
Comparator(..),
ComparatorA(..),
Order,
oDecr,
oIncr,
oShuffle,
) where
import Data.Aeson (Value)
import Data.Monoid ((<>))
import Data.Greskell.GraphSON (GraphSONTyped(..))
import Data.Greskell.Greskell
( Greskell, unsafeGreskellLazy,
toGremlin, toGremlinLazy, unsafeMethodCall, unsafeFunCall
)
class Predicate p where
type PredicateArg p
pAnd :: Greskell p -> Greskell p -> Greskell p
pAnd p1 p2 = unsafeMethodCall p1 "and" [toGremlin p2]
pOr :: Greskell p -> Greskell p -> Greskell p
pOr o1 o2 = unsafeMethodCall o1 "or" [toGremlin o2]
pTest :: Greskell p -> Greskell (PredicateArg p) -> Greskell Bool
pTest p arg = unsafeMethodCall p "test" [toGremlin arg]
pNegate :: Greskell p -> Greskell p
pNegate p = unsafeMethodCall p "negate" []
newtype PredicateA a = PredicateA { unPredicateA :: a -> Bool }
instance Predicate (PredicateA a) where
type PredicateArg (PredicateA a) = a
data P a
instance Predicate (P a) where
type PredicateArg (P a) = a
instance GraphSONTyped (P a) where
gsonTypeFor _ = "g:P"
pNot :: Greskell (P a) -> Greskell (P a)
pNot a = unsafeFunCall "P.not" [toGremlin a]
pEq :: Greskell a -> Greskell (P a)
pEq arg = unsafeFunCall "P.eq" [toGremlin arg]
pNeq :: Greskell a -> Greskell (P a)
pNeq arg = unsafeFunCall "P.neq" [toGremlin arg]
pLt :: Greskell a -> Greskell (P a)
pLt arg = unsafeFunCall "P.lt" [toGremlin arg]
pLte :: Greskell a -> Greskell (P a)
pLte arg = unsafeFunCall "P.lte" [toGremlin arg]
pGt :: Greskell a -> Greskell (P a)
pGt arg = unsafeFunCall "P.gt" [toGremlin arg]
pGte :: Greskell a -> Greskell (P a)
pGte arg = unsafeFunCall "P.gte" [toGremlin arg]
pInside :: Greskell a -> Greskell a -> Greskell (P a)
pInside a b = unsafeFunCall "P.inside" $ map toGremlin [a, b]
pOutside :: Greskell a -> Greskell a -> Greskell (P a)
pOutside a b = unsafeFunCall "P.outside" $ map toGremlin [a, b]
pBetween :: Greskell a -> Greskell a -> Greskell (P a)
pBetween a b = unsafeFunCall "P.between" $ map toGremlin [a, b]
pWithin :: [Greskell a] -> Greskell (P a)
pWithin = unsafeFunCall "P.within" . map toGremlin
pWithout :: [Greskell a] -> Greskell (P a)
pWithout = unsafeFunCall "P.without" . map toGremlin
class Comparator c where
type CompareArg c
cCompare :: Greskell c -> Greskell (CompareArg c) -> Greskell (CompareArg c) -> Greskell Int
cCompare cmp a b = unsafeMethodCall cmp "compare" $ map toGremlin [a, b]
cReversed :: Greskell c -> Greskell c
cReversed cmp = unsafeMethodCall cmp "reversed" []
cThenComparing :: Greskell c -> Greskell c -> Greskell c
cThenComparing cmp1 cmp2 = unsafeMethodCall cmp1 "thenComparing" [toGremlin cmp2]
newtype ComparatorA a = ComparatorA { unComparatorA :: a -> a -> Int }
instance Comparator (ComparatorA a) where
type CompareArg (ComparatorA a) = a
data Order a
instance Comparator (Order a) where
type CompareArg (Order a) = a
instance GraphSONTyped (Order a) where
gsonTypeFor _ = "g:Order"
oDecr :: Greskell (Order a)
oDecr = unsafeGreskellLazy "Order.decr"
oIncr :: Greskell (Order a)
oIncr = unsafeGreskellLazy "Order.incr"
oShuffle :: Greskell (Order a)
oShuffle = unsafeGreskellLazy "Order.shuffle"