module TD.Data.TestString
  ( TestString(..)    
  , defaultTestString 
  ) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.Text as T

data TestString
  = TestString -- ^ A simple object containing a string; for testing only
    { TestString -> Maybe Text
value :: Maybe T.Text -- ^ String
    }
  deriving (TestString -> TestString -> Bool
(TestString -> TestString -> Bool)
-> (TestString -> TestString -> Bool) -> Eq TestString
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TestString -> TestString -> Bool
== :: TestString -> TestString -> Bool
$c/= :: TestString -> TestString -> Bool
/= :: TestString -> TestString -> Bool
Eq, Int -> TestString -> ShowS
[TestString] -> ShowS
TestString -> String
(Int -> TestString -> ShowS)
-> (TestString -> String)
-> ([TestString] -> ShowS)
-> Show TestString
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TestString -> ShowS
showsPrec :: Int -> TestString -> ShowS
$cshow :: TestString -> String
show :: TestString -> String
$cshowList :: [TestString] -> ShowS
showList :: [TestString] -> ShowS
Show)

instance I.ShortShow TestString where
  shortShow :: TestString -> String
shortShow TestString
    { value :: TestString -> Maybe Text
value = Maybe Text
value_
    }
      = String
"TestString"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"value" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
value_
        ]

instance AT.FromJSON TestString where
  parseJSON :: Value -> Parser TestString
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
"testString" -> Value -> Parser TestString
parseTestString Value
v
      String
_            -> Parser TestString
forall a. Monoid a => a
mempty
    
    where
      parseTestString :: A.Value -> AT.Parser TestString
      parseTestString :: Value -> Parser TestString
parseTestString = String
-> (Object -> Parser TestString) -> Value -> Parser TestString
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TestString" ((Object -> Parser TestString) -> Value -> Parser TestString)
-> (Object -> Parser TestString) -> Value -> Parser TestString
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
value_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"value"
        TestString -> Parser TestString
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TestString -> Parser TestString)
-> TestString -> Parser TestString
forall a b. (a -> b) -> a -> b
$ TestString
          { value :: Maybe Text
value = Maybe Text
value_
          }
  parseJSON Value
_ = Parser TestString
forall a. Monoid a => a
mempty

instance AT.ToJSON TestString where
  toJSON :: TestString -> Value
toJSON TestString
    { value :: TestString -> Maybe Text
value = Maybe Text
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
"testString"
        , Key
"value" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
value_
        ]

defaultTestString :: TestString
defaultTestString :: TestString
defaultTestString =
  TestString
    { value :: Maybe Text
value = Maybe Text
forall a. Maybe a
Nothing
    }