module TD.Data.FoundPositions
  (FoundPositions(..)) where

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

data FoundPositions
  = FoundPositions -- ^ Contains 0-based positions of matched objects
    { FoundPositions -> Maybe Int
total_count :: Maybe Int   -- ^ Total number of matched objects
    , FoundPositions -> Maybe [Int]
positions   :: Maybe [Int] -- ^ The positions of the matched objects
    }
  deriving (FoundPositions -> FoundPositions -> Bool
(FoundPositions -> FoundPositions -> Bool)
-> (FoundPositions -> FoundPositions -> Bool) -> Eq FoundPositions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FoundPositions -> FoundPositions -> Bool
== :: FoundPositions -> FoundPositions -> Bool
$c/= :: FoundPositions -> FoundPositions -> Bool
/= :: FoundPositions -> FoundPositions -> Bool
Eq, Int -> FoundPositions -> ShowS
[FoundPositions] -> ShowS
FoundPositions -> String
(Int -> FoundPositions -> ShowS)
-> (FoundPositions -> String)
-> ([FoundPositions] -> ShowS)
-> Show FoundPositions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FoundPositions -> ShowS
showsPrec :: Int -> FoundPositions -> ShowS
$cshow :: FoundPositions -> String
show :: FoundPositions -> String
$cshowList :: [FoundPositions] -> ShowS
showList :: [FoundPositions] -> ShowS
Show)

instance I.ShortShow FoundPositions where
  shortShow :: FoundPositions -> String
shortShow FoundPositions
    { total_count :: FoundPositions -> Maybe Int
total_count = Maybe Int
total_count_
    , positions :: FoundPositions -> Maybe [Int]
positions   = Maybe [Int]
positions_
    }
      = String
"FoundPositions"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"total_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
total_count_
        , String
"positions"   String -> Maybe [Int] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Int]
positions_
        ]

instance AT.FromJSON FoundPositions where
  parseJSON :: Value -> Parser FoundPositions
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
"foundPositions" -> Value -> Parser FoundPositions
parseFoundPositions Value
v
      String
_                -> Parser FoundPositions
forall a. Monoid a => a
mempty
    
    where
      parseFoundPositions :: A.Value -> AT.Parser FoundPositions
      parseFoundPositions :: Value -> Parser FoundPositions
parseFoundPositions = String
-> (Object -> Parser FoundPositions)
-> Value
-> Parser FoundPositions
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"FoundPositions" ((Object -> Parser FoundPositions)
 -> Value -> Parser FoundPositions)
-> (Object -> Parser FoundPositions)
-> Value
-> Parser FoundPositions
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
total_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"total_count"
        Maybe [Int]
positions_   <- Object
o Object -> Key -> Parser (Maybe [Int])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"positions"
        FoundPositions -> Parser FoundPositions
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (FoundPositions -> Parser FoundPositions)
-> FoundPositions -> Parser FoundPositions
forall a b. (a -> b) -> a -> b
$ FoundPositions
          { total_count :: Maybe Int
total_count = Maybe Int
total_count_
          , positions :: Maybe [Int]
positions   = Maybe [Int]
positions_
          }
  parseJSON Value
_ = Parser FoundPositions
forall a. Monoid a => a
mempty