module TD.Data.TestBytes
  (TestBytes(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.ByteString as BS

data TestBytes
  = TestBytes -- ^ A simple object containing a sequence of bytes; for testing only
    { TestBytes -> Maybe ByteString
value :: Maybe BS.ByteString -- ^ Bytes
    }
  deriving (TestBytes -> TestBytes -> Bool
(TestBytes -> TestBytes -> Bool)
-> (TestBytes -> TestBytes -> Bool) -> Eq TestBytes
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TestBytes -> TestBytes -> Bool
== :: TestBytes -> TestBytes -> Bool
$c/= :: TestBytes -> TestBytes -> Bool
/= :: TestBytes -> TestBytes -> Bool
Eq, Int -> TestBytes -> ShowS
[TestBytes] -> ShowS
TestBytes -> String
(Int -> TestBytes -> ShowS)
-> (TestBytes -> String)
-> ([TestBytes] -> ShowS)
-> Show TestBytes
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TestBytes -> ShowS
showsPrec :: Int -> TestBytes -> ShowS
$cshow :: TestBytes -> String
show :: TestBytes -> String
$cshowList :: [TestBytes] -> ShowS
showList :: [TestBytes] -> ShowS
Show)

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

instance AT.FromJSON TestBytes where
  parseJSON :: Value -> Parser TestBytes
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
"testBytes" -> Value -> Parser TestBytes
parseTestBytes Value
v
      String
_           -> Parser TestBytes
forall a. Monoid a => a
mempty
    
    where
      parseTestBytes :: A.Value -> AT.Parser TestBytes
      parseTestBytes :: Value -> Parser TestBytes
parseTestBytes = String -> (Object -> Parser TestBytes) -> Value -> Parser TestBytes
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TestBytes" ((Object -> Parser TestBytes) -> Value -> Parser TestBytes)
-> (Object -> Parser TestBytes) -> Value -> Parser TestBytes
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe ByteString
value_ <- (String -> ByteString) -> Maybe String -> Maybe ByteString
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> ByteString
I.readBytes (Maybe String -> Maybe ByteString)
-> Parser (Maybe String) -> Parser (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"value"
        TestBytes -> Parser TestBytes
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TestBytes -> Parser TestBytes) -> TestBytes -> Parser TestBytes
forall a b. (a -> b) -> a -> b
$ TestBytes
          { value :: Maybe ByteString
value = Maybe ByteString
value_
          }
  parseJSON Value
_ = Parser TestBytes
forall a. Monoid a => a
mempty