module TD.Data.FilePart
  (FilePart(..)) 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 FilePart
  = FilePart -- ^ Contains a part of a file
    { FilePart -> Maybe ByteString
_data :: Maybe BS.ByteString -- ^ File bytes
    }
  deriving (FilePart -> FilePart -> Bool
(FilePart -> FilePart -> Bool)
-> (FilePart -> FilePart -> Bool) -> Eq FilePart
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FilePart -> FilePart -> Bool
== :: FilePart -> FilePart -> Bool
$c/= :: FilePart -> FilePart -> Bool
/= :: FilePart -> FilePart -> Bool
Eq, Int -> FilePart -> ShowS
[FilePart] -> ShowS
FilePart -> String
(Int -> FilePart -> ShowS)
-> (FilePart -> String) -> ([FilePart] -> ShowS) -> Show FilePart
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FilePart -> ShowS
showsPrec :: Int -> FilePart -> ShowS
$cshow :: FilePart -> String
show :: FilePart -> String
$cshowList :: [FilePart] -> ShowS
showList :: [FilePart] -> ShowS
Show)

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

instance AT.FromJSON FilePart where
  parseJSON :: Value -> Parser FilePart
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
"filePart" -> Value -> Parser FilePart
parseFilePart Value
v
      String
_          -> Parser FilePart
forall a. Monoid a => a
mempty
    
    where
      parseFilePart :: A.Value -> AT.Parser FilePart
      parseFilePart :: Value -> Parser FilePart
parseFilePart = String -> (Object -> Parser FilePart) -> Value -> Parser FilePart
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"FilePart" ((Object -> Parser FilePart) -> Value -> Parser FilePart)
-> (Object -> Parser FilePart) -> Value -> Parser FilePart
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe ByteString
_data_ <- (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
"data"
        FilePart -> Parser FilePart
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FilePart -> Parser FilePart) -> FilePart -> Parser FilePart
forall a b. (a -> b) -> a -> b
$ FilePart
          { _data :: Maybe ByteString
_data = Maybe ByteString
_data_
          }
  parseJSON Value
_ = Parser FilePart
forall a. Monoid a => a
mempty