module Hasura.SQL.Value
  ( TxtEncodedVal (..),
  )
where

import Data.Aeson qualified as J
import Data.Aeson.Types qualified as AT
import Data.Text qualified as T
import Hasura.Prelude

data TxtEncodedVal
  = TENull
  | TELit !T.Text
  deriving (Int -> TxtEncodedVal -> ShowS
[TxtEncodedVal] -> ShowS
TxtEncodedVal -> String
(Int -> TxtEncodedVal -> ShowS)
-> (TxtEncodedVal -> String)
-> ([TxtEncodedVal] -> ShowS)
-> Show TxtEncodedVal
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxtEncodedVal -> ShowS
showsPrec :: Int -> TxtEncodedVal -> ShowS
$cshow :: TxtEncodedVal -> String
show :: TxtEncodedVal -> String
$cshowList :: [TxtEncodedVal] -> ShowS
showList :: [TxtEncodedVal] -> ShowS
Show, TxtEncodedVal -> TxtEncodedVal -> Bool
(TxtEncodedVal -> TxtEncodedVal -> Bool)
-> (TxtEncodedVal -> TxtEncodedVal -> Bool) -> Eq TxtEncodedVal
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxtEncodedVal -> TxtEncodedVal -> Bool
== :: TxtEncodedVal -> TxtEncodedVal -> Bool
$c/= :: TxtEncodedVal -> TxtEncodedVal -> Bool
/= :: TxtEncodedVal -> TxtEncodedVal -> Bool
Eq, (forall x. TxtEncodedVal -> Rep TxtEncodedVal x)
-> (forall x. Rep TxtEncodedVal x -> TxtEncodedVal)
-> Generic TxtEncodedVal
forall x. Rep TxtEncodedVal x -> TxtEncodedVal
forall x. TxtEncodedVal -> Rep TxtEncodedVal x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TxtEncodedVal -> Rep TxtEncodedVal x
from :: forall x. TxtEncodedVal -> Rep TxtEncodedVal x
$cto :: forall x. Rep TxtEncodedVal x -> TxtEncodedVal
to :: forall x. Rep TxtEncodedVal x -> TxtEncodedVal
Generic)

instance Hashable TxtEncodedVal

instance J.ToJSON TxtEncodedVal where
  toJSON :: TxtEncodedVal -> Value
toJSON = \case
    TxtEncodedVal
TENull -> Value
AT.Null
    TELit Text
t -> Text -> Value
AT.String Text
t

instance J.FromJSON TxtEncodedVal where
  parseJSON :: Value -> Parser TxtEncodedVal
parseJSON Value
J.Null = TxtEncodedVal -> Parser TxtEncodedVal
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TxtEncodedVal
TENull
  parseJSON (J.String Text
t) = TxtEncodedVal -> Parser TxtEncodedVal
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TxtEncodedVal -> Parser TxtEncodedVal)
-> TxtEncodedVal -> Parser TxtEncodedVal
forall a b. (a -> b) -> a -> b
$ Text -> TxtEncodedVal
TELit Text
t
  parseJSON Value
v = String -> Value -> Parser TxtEncodedVal
forall a. String -> Value -> Parser a
AT.typeMismatch String
"String" Value
v