{-# LANGUAGE OverloadedStrings, TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
module Data.Greskell.Greskell
(
Greskell,
ToGreskell(..),
toGremlin,
toGremlinLazy,
string,
true,
false,
list,
single,
number,
value,
valueInt,
gvalue,
gvalueInt,
unsafeGreskell,
unsafeGreskellLazy,
unsafeFunCall,
unsafeMethodCall
) where
import Data.Aeson (Value)
import qualified Data.Aeson as Aeson
import Data.Bifunctor (bimap)
import Data.Foldable (toList)
import qualified Data.HashMap.Lazy as HM
import Data.Monoid (Monoid(..))
import Data.Ratio (numerator, denominator, Rational)
import Data.Scientific (Scientific, coefficient, base10Exponent)
import Data.Semigroup (Semigroup(..))
import Data.String (IsString(..))
import Data.List (intersperse)
import Data.Text (Text, pack, unpack)
import qualified Data.Text.Lazy as TL
import Data.Greskell.GraphSON (GValue, GValueBody(..), nonTypedGValue)
newtype Greskell a = Greskell { Greskell a -> Text
unGreskell :: TL.Text }
deriving (Int -> Greskell a -> ShowS
[Greskell a] -> ShowS
Greskell a -> String
(Int -> Greskell a -> ShowS)
-> (Greskell a -> String)
-> ([Greskell a] -> ShowS)
-> Show (Greskell a)
forall a. Int -> Greskell a -> ShowS
forall a. [Greskell a] -> ShowS
forall a. Greskell a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Greskell a] -> ShowS
$cshowList :: forall a. [Greskell a] -> ShowS
show :: Greskell a -> String
$cshow :: forall a. Greskell a -> String
showsPrec :: Int -> Greskell a -> ShowS
$cshowsPrec :: forall a. Int -> Greskell a -> ShowS
Show,Greskell a -> Greskell a -> Bool
(Greskell a -> Greskell a -> Bool)
-> (Greskell a -> Greskell a -> Bool) -> Eq (Greskell a)
forall a. Greskell a -> Greskell a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Greskell a -> Greskell a -> Bool
$c/= :: forall a. Greskell a -> Greskell a -> Bool
== :: Greskell a -> Greskell a -> Bool
$c== :: forall a. Greskell a -> Greskell a -> Bool
Eq,Eq (Greskell a)
Eq (Greskell a)
-> (Greskell a -> Greskell a -> Ordering)
-> (Greskell a -> Greskell a -> Bool)
-> (Greskell a -> Greskell a -> Bool)
-> (Greskell a -> Greskell a -> Bool)
-> (Greskell a -> Greskell a -> Bool)
-> (Greskell a -> Greskell a -> Greskell a)
-> (Greskell a -> Greskell a -> Greskell a)
-> Ord (Greskell a)
Greskell a -> Greskell a -> Bool
Greskell a -> Greskell a -> Ordering
Greskell a -> Greskell a -> Greskell a
forall a. Eq (Greskell a)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Greskell a -> Greskell a -> Bool
forall a. Greskell a -> Greskell a -> Ordering
forall a. Greskell a -> Greskell a -> Greskell a
min :: Greskell a -> Greskell a -> Greskell a
$cmin :: forall a. Greskell a -> Greskell a -> Greskell a
max :: Greskell a -> Greskell a -> Greskell a
$cmax :: forall a. Greskell a -> Greskell a -> Greskell a
>= :: Greskell a -> Greskell a -> Bool
$c>= :: forall a. Greskell a -> Greskell a -> Bool
> :: Greskell a -> Greskell a -> Bool
$c> :: forall a. Greskell a -> Greskell a -> Bool
<= :: Greskell a -> Greskell a -> Bool
$c<= :: forall a. Greskell a -> Greskell a -> Bool
< :: Greskell a -> Greskell a -> Bool
$c< :: forall a. Greskell a -> Greskell a -> Bool
compare :: Greskell a -> Greskell a -> Ordering
$ccompare :: forall a. Greskell a -> Greskell a -> Ordering
$cp1Ord :: forall a. Eq (Greskell a)
Ord)
instance IsString a => IsString (Greskell a) where
fromString :: String -> Greskell a
fromString = Text -> Greskell a
forall a. Text -> Greskell a
Greskell (Text -> Greskell a) -> (String -> Text) -> String -> Greskell a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
TL.pack (String -> Text) -> ShowS -> String -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
escapeDQuotes
instance Functor Greskell where
fmap :: (a -> b) -> Greskell a -> Greskell b
fmap a -> b
_ = Text -> Greskell b
forall a. Text -> Greskell a
Greskell (Text -> Greskell b)
-> (Greskell a -> Text) -> Greskell a -> Greskell b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Greskell a -> Text
forall a. Greskell a -> Text
unGreskell
instance Num a => Num (Greskell a) where
+ :: Greskell a -> Greskell a -> Greskell a
(+) = Text -> Greskell a -> Greskell a -> Greskell a
forall a. Text -> Greskell a -> Greskell a -> Greskell a
biOp Text
"+"
(-) = Text -> Greskell a -> Greskell a -> Greskell a
forall a. Text -> Greskell a -> Greskell a -> Greskell a
biOp Text
"-"
* :: Greskell a -> Greskell a -> Greskell a
(*) = Text -> Greskell a -> Greskell a -> Greskell a
forall a. Text -> Greskell a -> Greskell a -> Greskell a
biOp Text
"*"
negate :: Greskell a -> Greskell a
negate (Greskell Text
a) = Text -> Greskell a
forall a. Text -> Greskell a
Greskell (Text
"-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
paren Text
a)
abs :: Greskell a -> Greskell a
abs (Greskell Text
a) = Text -> Greskell a
forall a. Text -> Greskell a
Greskell (Text
"java.lang.Math.abs" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
paren Text
a)
signum :: Greskell a -> Greskell a
signum (Greskell Text
a) = Text -> Greskell a
forall a. Text -> Greskell a
Greskell (Text
"java.lang.Long.signum" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
paren Text
a)
fromInteger :: Integer -> Greskell a
fromInteger Integer
val = Text -> Greskell a
forall a. Text -> Greskell a
Greskell (String -> Text
TL.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Integer -> String
forall a. Show a => a -> String
show Integer
val)
instance Fractional a => Fractional (Greskell a) where
/ :: Greskell a -> Greskell a -> Greskell a
(/) = Text -> Greskell a -> Greskell a -> Greskell a
forall a. Text -> Greskell a -> Greskell a -> Greskell a
biOp Text
"/"
recip :: Greskell a -> Greskell a
recip (Greskell Text
a) = Text -> Greskell a
forall a. Text -> Greskell a
Greskell (Text
"1.0/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
paren Text
a)
fromRational :: Rational -> Greskell a
fromRational Rational
rat = Text -> Greskell a
forall a. Text -> Greskell a
Greskell (Text -> Greskell a) -> Text -> Greskell a
forall a b. (a -> b) -> a -> b
$ (Rational -> Integer) -> Text
scriptOf Rational -> Integer
forall a. Ratio a -> a
numerator Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".0/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (Rational -> Integer) -> Text
scriptOf Rational -> Integer
forall a. Ratio a -> a
denominator
where
scriptOf :: (Rational -> Integer) -> Text
scriptOf Rational -> Integer
accessor = String -> Text
TL.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Integer -> String
forall a. Show a => a -> String
show (Integer -> String) -> Integer -> String
forall a b. (a -> b) -> a -> b
$ Rational -> Integer
accessor Rational
rat
instance IsString a => Semigroup (Greskell a) where
<> :: Greskell a -> Greskell a -> Greskell a
(<>) = Text -> Greskell a -> Greskell a -> Greskell a
forall a. Text -> Greskell a -> Greskell a -> Greskell a
biOp Text
"+"
instance IsString a => Monoid (Greskell a) where
mempty :: Greskell a
mempty = String -> Greskell a
forall a. IsString a => String -> a
fromString String
""
mappend :: Greskell a -> Greskell a -> Greskell a
mappend = Greskell a -> Greskell a -> Greskell a
forall a. Semigroup a => a -> a -> a
(<>)
class ToGreskell a where
type GreskellReturn a
toGreskell :: a -> Greskell (GreskellReturn a)
instance ToGreskell (Greskell a) where
type GreskellReturn (Greskell a) = a
toGreskell :: Greskell a -> Greskell (GreskellReturn (Greskell a))
toGreskell = Greskell a -> Greskell (GreskellReturn (Greskell a))
forall a. a -> a
id
biOp :: TL.Text -> Greskell a -> Greskell a -> Greskell a
biOp :: Text -> Greskell a -> Greskell a -> Greskell a
biOp Text
operator (Greskell Text
a) (Greskell Text
b) = Text -> Greskell a
forall a. Text -> Greskell a
Greskell (Text -> Text
paren Text
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
operator Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
paren Text
b)
paren :: TL.Text -> TL.Text
paren :: Text -> Text
paren Text
t = Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
escapeDQuotes :: String -> String
escapeDQuotes :: ShowS
escapeDQuotes String
orig = (Char
'"' Char -> ShowS
forall a. a -> [a] -> [a]
: (Char -> String
esc (Char -> String) -> ShowS
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String
orig)) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"\""
where
esc :: Char -> String
esc Char
c = case Char
c of
Char
'\n' -> String
"\\n"
Char
'\r' -> String
"\\r"
Char
'\t' -> String
"\\t"
Char
'\\' -> String
"\\\\"
Char
'"' -> String
"\\\""
Char
'$' -> String
"\\$"
Char
x -> [Char
x]
unsafeGreskell :: Text
-> Greskell a
unsafeGreskell :: Text -> Greskell a
unsafeGreskell = Text -> Greskell a
forall a. Text -> Greskell a
Greskell (Text -> Greskell a) -> (Text -> Text) -> Text -> Greskell a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
TL.fromStrict
unsafeGreskellLazy :: TL.Text
-> Greskell a
unsafeGreskellLazy :: Text -> Greskell a
unsafeGreskellLazy = Text -> Greskell a
forall a. Text -> Greskell a
Greskell
string :: Text -> Greskell Text
string :: Text -> Greskell Text
string = String -> Greskell Text
forall a. IsString a => String -> a
fromString (String -> Greskell Text)
-> (Text -> String) -> Text -> Greskell Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
unpack
true :: Greskell Bool
true :: Greskell Bool
true = Text -> Greskell Bool
forall a. Text -> Greskell a
unsafeGreskell Text
"true"
false :: Greskell Bool
false :: Greskell Bool
false = Text -> Greskell Bool
forall a. Text -> Greskell a
unsafeGreskell Text
"false"
list :: [Greskell a] -> Greskell [a]
list :: [Greskell a] -> Greskell [a]
list [Greskell a]
gs = Text -> Greskell [a]
forall a. Text -> Greskell a
unsafeGreskellLazy (Text -> Greskell [a]) -> Text -> Greskell [a]
forall a b. (a -> b) -> a -> b
$ (Text
"[" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
TL.intercalate Text
"," [Text]
gs_txt Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]")
where
gs_txt :: [Text]
gs_txt = (Greskell a -> Text) -> [Greskell a] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Greskell a -> Text
forall a. ToGreskell a => a -> Text
toGremlinLazy [Greskell a]
gs
single :: Greskell a -> Greskell [a]
single :: Greskell a -> Greskell [a]
single Greskell a
g = [Greskell a] -> Greskell [a]
forall a. [Greskell a] -> Greskell [a]
list [Greskell a
g]
number :: Scientific -> Greskell Scientific
number :: Scientific -> Greskell Scientific
number = Text -> Greskell Scientific
forall a. Text -> Greskell a
unsafeGreskell (Text -> Greskell Scientific)
-> (Scientific -> Text) -> Scientific -> Greskell Scientific
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack (String -> Text) -> (Scientific -> String) -> Scientific -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scientific -> String
forall a. Show a => a -> String
show
value :: Value -> Greskell Value
value :: Value -> Greskell Value
value Value
Aeson.Null = Text -> Greskell Value
forall a. Text -> Greskell a
unsafeGreskellLazy Text
"null"
value (Aeson.Bool Bool
b) = Greskell Bool -> Greskell Value
forall a. Greskell a -> Greskell Value
unsafeToValue (if Bool
b then Greskell Bool
true else Greskell Bool
false)
value (Aeson.Number Scientific
sci) = Greskell Scientific -> Greskell Value
forall a. Greskell a -> Greskell Value
unsafeToValue (Greskell Scientific -> Greskell Value)
-> Greskell Scientific -> Greskell Value
forall a b. (a -> b) -> a -> b
$ Scientific -> Greskell Scientific
number Scientific
sci
value (Aeson.String Text
s) = Greskell Text -> Greskell Value
forall a. Greskell a -> Greskell Value
unsafeToValue (Greskell Text -> Greskell Value)
-> Greskell Text -> Greskell Value
forall a b. (a -> b) -> a -> b
$ Text -> Greskell Text
string Text
s
value (Aeson.Array Array
v) = Greskell [Value] -> Greskell Value
forall a. Greskell a -> Greskell Value
unsafeToValue (Greskell [Value] -> Greskell Value)
-> Greskell [Value] -> Greskell Value
forall a b. (a -> b) -> a -> b
$ [Greskell Value] -> Greskell [Value]
forall a. [Greskell a] -> Greskell [a]
list ([Greskell Value] -> Greskell [Value])
-> [Greskell Value] -> Greskell [Value]
forall a b. (a -> b) -> a -> b
$ (Value -> Greskell Value) -> [Value] -> [Greskell Value]
forall a b. (a -> b) -> [a] -> [b]
map Value -> Greskell Value
value ([Value] -> [Greskell Value]) -> [Value] -> [Greskell Value]
forall a b. (a -> b) -> a -> b
$ Array -> [Value]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Array
v
value (Aeson.Object Object
obj)
| Object -> Bool
forall k v. HashMap k v -> Bool
HM.null Object
obj = Text -> Greskell Value
forall a. Text -> Greskell a
unsafeGreskellLazy Text
"[:]"
| Bool
otherwise = Text -> Greskell Value
forall a. Text -> Greskell a
unsafeGreskellLazy (Text -> Greskell Value) -> Text -> Greskell Value
forall a b. (a -> b) -> a -> b
$ [(Text, Value)] -> Text
toGroovyMap ([(Text, Value)] -> Text) -> [(Text, Value)] -> Text
forall a b. (a -> b) -> a -> b
$ Object -> [(Text, Value)]
forall k v. HashMap k v -> [(k, v)]
HM.toList Object
obj
where
toGroovyMap :: [(Text, Value)] -> Text
toGroovyMap [(Text, Value)]
pairs = Text
"[" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
TL.intercalate Text
"," (((Text, Value) -> Text) -> [(Text, Value)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text, Value) -> Text
toPairText [(Text, Value)]
pairs) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]"
toPairText :: (Text, Value) -> Text
toPairText (Text
key, Value
val) = (Greskell Text -> Text
forall a. ToGreskell a => a -> Text
toGremlinLazy (Greskell Text -> Text) -> Greskell Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Greskell Text
string Text
key) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (Greskell Value -> Text
forall a. ToGreskell a => a -> Text
toGremlinLazy (Greskell Value -> Text) -> Greskell Value -> Text
forall a b. (a -> b) -> a -> b
$ Value -> Greskell Value
value Value
val)
valueInt :: Integral a => a -> Greskell Value
valueInt :: a -> Greskell Value
valueInt a
n = (Integer -> Value) -> Greskell Integer -> Greskell Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Value
toValue (Greskell Integer -> Greskell Value)
-> Greskell Integer -> Greskell Value
forall a b. (a -> b) -> a -> b
$ a -> Greskell Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n
where
toValue :: Integer -> Value
toValue :: Integer -> Value
toValue = Value -> Integer -> Value
forall a b. a -> b -> a
const Value
Aeson.Null
gvalue :: Value -> Greskell GValue
gvalue :: Value -> Greskell GValue
gvalue = (Value -> GValue) -> Greskell Value -> Greskell GValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value -> GValue
forall p. p -> GValue
phantomToGValue (Greskell Value -> Greskell GValue)
-> (Value -> Greskell Value) -> Value -> Greskell GValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Greskell Value
value
where
phantomToGValue :: p -> GValue
phantomToGValue p
_ = GValueBody -> GValue
nonTypedGValue (GValueBody -> GValue) -> GValueBody -> GValue
forall a b. (a -> b) -> a -> b
$ GValueBody
GNull
gvalueInt :: Integral a => a -> Greskell GValue
gvalueInt :: a -> Greskell GValue
gvalueInt a
n = (Integer -> GValue) -> Greskell Integer -> Greskell GValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> GValue
toGValue (Greskell Integer -> Greskell GValue)
-> Greskell Integer -> Greskell GValue
forall a b. (a -> b) -> a -> b
$ a -> Greskell Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n
where
toGValue :: Integer -> GValue
toGValue :: Integer -> GValue
toGValue = GValue -> Integer -> GValue
forall a b. a -> b -> a
const (GValue -> Integer -> GValue) -> GValue -> Integer -> GValue
forall a b. (a -> b) -> a -> b
$ GValueBody -> GValue
nonTypedGValue (GValueBody -> GValue) -> GValueBody -> GValue
forall a b. (a -> b) -> a -> b
$ GValueBody
GNull
unsafeToValue :: Greskell a -> Greskell Value
unsafeToValue :: Greskell a -> Greskell Value
unsafeToValue = (a -> Value) -> Greskell a -> Greskell Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Value -> a -> Value
forall a b. a -> b -> a
const Value
Aeson.Null)
toGremlin :: ToGreskell a => a -> Text
toGremlin :: a -> Text
toGremlin = Text -> Text
TL.toStrict (Text -> Text) -> (a -> Text) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Greskell (GreskellReturn a) -> Text
forall a. Greskell a -> Text
unGreskell (Greskell (GreskellReturn a) -> Text)
-> (a -> Greskell (GreskellReturn a)) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Greskell (GreskellReturn a)
forall a. ToGreskell a => a -> Greskell (GreskellReturn a)
toGreskell
toGremlinLazy :: ToGreskell a => a -> TL.Text
toGremlinLazy :: a -> Text
toGremlinLazy = Greskell (GreskellReturn a) -> Text
forall a. Greskell a -> Text
unGreskell (Greskell (GreskellReturn a) -> Text)
-> (a -> Greskell (GreskellReturn a)) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Greskell (GreskellReturn a)
forall a. ToGreskell a => a -> Greskell (GreskellReturn a)
toGreskell
unsafeFunCallText :: Text -> [Text] -> Text
unsafeFunCallText :: Text -> [Text] -> Text
unsafeFunCallText Text
fun_name [Text]
args = Text
fun_name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
args_g Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
where
args_g :: Text
args_g = [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
intersperse Text
"," [Text]
args
unsafeFunCall :: Text
-> [Text]
-> Greskell a
unsafeFunCall :: Text -> [Text] -> Greskell a
unsafeFunCall Text
fun_name [Text]
args = Text -> Greskell a
forall a. Text -> Greskell a
unsafeGreskell (Text -> Greskell a) -> Text -> Greskell a
forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> Text
unsafeFunCallText Text
fun_name [Text]
args
unsafeMethodCall :: Greskell a
-> Text
-> [Text]
-> Greskell b
unsafeMethodCall :: Greskell a -> Text -> [Text] -> Greskell b
unsafeMethodCall Greskell a
target Text
name [Text]
args = Text -> Greskell b
forall a. Text -> Greskell a
unsafeGreskell (Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Greskell a -> Text
forall a. ToGreskell a => a -> Text
toGremlin Greskell a
target Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
unsafeFunCallText Text
name [Text]
args)