module TD.Data.TestInt
( TestInt(..)
, defaultTestInt
) where
import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
data TestInt
= TestInt
{ TestInt -> Maybe Int
value :: Maybe Int
}
deriving (TestInt -> TestInt -> Bool
(TestInt -> TestInt -> Bool)
-> (TestInt -> TestInt -> Bool) -> Eq TestInt
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TestInt -> TestInt -> Bool
== :: TestInt -> TestInt -> Bool
$c/= :: TestInt -> TestInt -> Bool
/= :: TestInt -> TestInt -> Bool
Eq, Int -> TestInt -> ShowS
[TestInt] -> ShowS
TestInt -> String
(Int -> TestInt -> ShowS)
-> (TestInt -> String) -> ([TestInt] -> ShowS) -> Show TestInt
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TestInt -> ShowS
showsPrec :: Int -> TestInt -> ShowS
$cshow :: TestInt -> String
show :: TestInt -> String
$cshowList :: [TestInt] -> ShowS
showList :: [TestInt] -> ShowS
Show)
instance I.ShortShow TestInt where
shortShow :: TestInt -> String
shortShow TestInt
{ value :: TestInt -> Maybe Int
value = Maybe Int
value_
}
= String
"TestInt"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"value" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
value_
]
instance AT.FromJSON TestInt where
parseJSON :: Value -> Parser TestInt
parseJSON v :: Value
v@(AT.Object Object
obj) = do
String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String
case String
t of
String
"testInt" -> Value -> Parser TestInt
parseTestInt Value
v
String
_ -> Parser TestInt
forall a. Monoid a => a
mempty
where
parseTestInt :: A.Value -> AT.Parser TestInt
parseTestInt :: Value -> Parser TestInt
parseTestInt = String -> (Object -> Parser TestInt) -> Value -> Parser TestInt
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TestInt" ((Object -> Parser TestInt) -> Value -> Parser TestInt)
-> (Object -> Parser TestInt) -> Value -> Parser TestInt
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Int
value_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"value"
TestInt -> Parser TestInt
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TestInt -> Parser TestInt) -> TestInt -> Parser TestInt
forall a b. (a -> b) -> a -> b
$ TestInt
{ value :: Maybe Int
value = Maybe Int
value_
}
parseJSON Value
_ = Parser TestInt
forall a. Monoid a => a
mempty
instance AT.ToJSON TestInt where
toJSON :: TestInt -> Value
toJSON TestInt
{ value :: TestInt -> Maybe Int
value = Maybe Int
value_
}
= [Pair] -> Value
A.object
[ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"testInt"
, Key
"value" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
value_
]
defaultTestInt :: TestInt
defaultTestInt :: TestInt
defaultTestInt =
TestInt
{ value :: Maybe Int
value = Maybe Int
forall a. Maybe a
Nothing
}