{-# LANGUAGE OverloadedStrings #-}

module Database.Bloodhound.Internal.Versions.Common.Types.Query.Prefix
  ( PrefixQuery (..),
  )
where

import Database.Bloodhound.Internal.Utils.Imports
import Database.Bloodhound.Internal.Versions.Common.Types.Newtypes
import Database.Bloodhound.Internal.Versions.Common.Types.Query.Commons
import GHC.Generics

data PrefixQuery = PrefixQuery
  { PrefixQuery -> FieldName
prefixQueryField :: FieldName,
    PrefixQuery -> Text
prefixQueryPrefixValue :: Text,
    PrefixQuery -> Maybe Boost
prefixQueryBoost :: Maybe Boost
  }
  deriving stock (PrefixQuery -> PrefixQuery -> Bool
(PrefixQuery -> PrefixQuery -> Bool)
-> (PrefixQuery -> PrefixQuery -> Bool) -> Eq PrefixQuery
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PrefixQuery -> PrefixQuery -> Bool
== :: PrefixQuery -> PrefixQuery -> Bool
$c/= :: PrefixQuery -> PrefixQuery -> Bool
/= :: PrefixQuery -> PrefixQuery -> Bool
Eq, Int -> PrefixQuery -> ShowS
[PrefixQuery] -> ShowS
PrefixQuery -> String
(Int -> PrefixQuery -> ShowS)
-> (PrefixQuery -> String)
-> ([PrefixQuery] -> ShowS)
-> Show PrefixQuery
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PrefixQuery -> ShowS
showsPrec :: Int -> PrefixQuery -> ShowS
$cshow :: PrefixQuery -> String
show :: PrefixQuery -> String
$cshowList :: [PrefixQuery] -> ShowS
showList :: [PrefixQuery] -> ShowS
Show, (forall x. PrefixQuery -> Rep PrefixQuery x)
-> (forall x. Rep PrefixQuery x -> PrefixQuery)
-> Generic PrefixQuery
forall x. Rep PrefixQuery x -> PrefixQuery
forall x. PrefixQuery -> Rep PrefixQuery x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PrefixQuery -> Rep PrefixQuery x
from :: forall x. PrefixQuery -> Rep PrefixQuery x
$cto :: forall x. Rep PrefixQuery x -> PrefixQuery
to :: forall x. Rep PrefixQuery x -> PrefixQuery
Generic)

instance ToJSON PrefixQuery where
  toJSON :: PrefixQuery -> Value
toJSON (PrefixQuery (FieldName Text
fieldName) Text
queryValue Maybe Boost
boost) =
    [Pair] -> Value
object [Text -> Key
fromText Text
fieldName Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= [Pair] -> Value
omitNulls [Pair]
base]
    where
      base :: [Pair]
base =
        [ Key
"value" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
queryValue,
          Key
"boost" Key -> Maybe Boost -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe Boost
boost
        ]

instance FromJSON PrefixQuery where
  parseJSON :: Value -> Parser PrefixQuery
parseJSON = String
-> (Object -> Parser PrefixQuery) -> Value -> Parser PrefixQuery
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"PrefixQuery" Object -> Parser PrefixQuery
parse
    where
      parse :: Object -> Parser PrefixQuery
parse = (FieldName -> Object -> Parser PrefixQuery)
-> Object -> Parser PrefixQuery
forall (m :: * -> *) a.
(Monad m, MonadFail m) =>
(FieldName -> Object -> m a) -> Object -> m a
fieldTagged ((FieldName -> Object -> Parser PrefixQuery)
 -> Object -> Parser PrefixQuery)
-> (FieldName -> Object -> Parser PrefixQuery)
-> Object
-> Parser PrefixQuery
forall a b. (a -> b) -> a -> b
$ \FieldName
fn Object
o ->
        FieldName -> Text -> Maybe Boost -> PrefixQuery
PrefixQuery FieldName
fn
          (Text -> Maybe Boost -> PrefixQuery)
-> Parser Text -> Parser (Maybe Boost -> PrefixQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"value"
          Parser (Maybe Boost -> PrefixQuery)
-> Parser (Maybe Boost) -> Parser PrefixQuery
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe Boost)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"boost"