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

import Data.Aeson qualified as A
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
showList :: [TxtEncodedVal] -> ShowS
$cshowList :: [TxtEncodedVal] -> ShowS
show :: TxtEncodedVal -> String
$cshow :: TxtEncodedVal -> String
showsPrec :: Int -> TxtEncodedVal -> ShowS
$cshowsPrec :: Int -> TxtEncodedVal -> ShowS
Show, TxtEncodedVal -> TxtEncodedVal -> Bool
(TxtEncodedVal -> TxtEncodedVal -> Bool)
-> (TxtEncodedVal -> TxtEncodedVal -> Bool) -> Eq TxtEncodedVal
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxtEncodedVal -> TxtEncodedVal -> Bool
$c/= :: TxtEncodedVal -> TxtEncodedVal -> Bool
== :: TxtEncodedVal -> TxtEncodedVal -> Bool
$c== :: 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
$cto :: forall x. Rep TxtEncodedVal x -> TxtEncodedVal
$cfrom :: forall x. TxtEncodedVal -> Rep TxtEncodedVal x
Generic)

instance Hashable TxtEncodedVal

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

instance A.FromJSON TxtEncodedVal where
  parseJSON :: Value -> Parser TxtEncodedVal
parseJSON Value
A.Null = TxtEncodedVal -> Parser TxtEncodedVal
forall (f :: * -> *) a. Applicative f => a -> f a
pure TxtEncodedVal
TENull
  parseJSON (A.String Text
t) = TxtEncodedVal -> Parser TxtEncodedVal
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