module Hasura.RQL.Types.ComputedField.Name
  ( ComputedFieldName (..),
    computedFieldNameToText,
    fromComputedField,
  )
where

import Autodocodec (HasCodec (codec), dimapCodec)
import Data.Aeson
import Data.Text.Extended (ToTxt)
import Data.Text.NonEmpty (NonEmptyText (unNonEmptyText))
import Database.PG.Query qualified as PG
import Hasura.Prelude
import Hasura.RQL.Types.Common (FieldName (..))

newtype ComputedFieldName = ComputedFieldName {ComputedFieldName -> NonEmptyText
unComputedFieldName :: NonEmptyText}
  deriving (Int -> ComputedFieldName -> ShowS
[ComputedFieldName] -> ShowS
ComputedFieldName -> String
(Int -> ComputedFieldName -> ShowS)
-> (ComputedFieldName -> String)
-> ([ComputedFieldName] -> ShowS)
-> Show ComputedFieldName
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ComputedFieldName -> ShowS
showsPrec :: Int -> ComputedFieldName -> ShowS
$cshow :: ComputedFieldName -> String
show :: ComputedFieldName -> String
$cshowList :: [ComputedFieldName] -> ShowS
showList :: [ComputedFieldName] -> ShowS
Show, ComputedFieldName -> ComputedFieldName -> Bool
(ComputedFieldName -> ComputedFieldName -> Bool)
-> (ComputedFieldName -> ComputedFieldName -> Bool)
-> Eq ComputedFieldName
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ComputedFieldName -> ComputedFieldName -> Bool
== :: ComputedFieldName -> ComputedFieldName -> Bool
$c/= :: ComputedFieldName -> ComputedFieldName -> Bool
/= :: ComputedFieldName -> ComputedFieldName -> Bool
Eq, Eq ComputedFieldName
Eq ComputedFieldName
-> (ComputedFieldName -> ComputedFieldName -> Ordering)
-> (ComputedFieldName -> ComputedFieldName -> Bool)
-> (ComputedFieldName -> ComputedFieldName -> Bool)
-> (ComputedFieldName -> ComputedFieldName -> Bool)
-> (ComputedFieldName -> ComputedFieldName -> Bool)
-> (ComputedFieldName -> ComputedFieldName -> ComputedFieldName)
-> (ComputedFieldName -> ComputedFieldName -> ComputedFieldName)
-> Ord ComputedFieldName
ComputedFieldName -> ComputedFieldName -> Bool
ComputedFieldName -> ComputedFieldName -> Ordering
ComputedFieldName -> ComputedFieldName -> ComputedFieldName
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ComputedFieldName -> ComputedFieldName -> Ordering
compare :: ComputedFieldName -> ComputedFieldName -> Ordering
$c< :: ComputedFieldName -> ComputedFieldName -> Bool
< :: ComputedFieldName -> ComputedFieldName -> Bool
$c<= :: ComputedFieldName -> ComputedFieldName -> Bool
<= :: ComputedFieldName -> ComputedFieldName -> Bool
$c> :: ComputedFieldName -> ComputedFieldName -> Bool
> :: ComputedFieldName -> ComputedFieldName -> Bool
$c>= :: ComputedFieldName -> ComputedFieldName -> Bool
>= :: ComputedFieldName -> ComputedFieldName -> Bool
$cmax :: ComputedFieldName -> ComputedFieldName -> ComputedFieldName
max :: ComputedFieldName -> ComputedFieldName -> ComputedFieldName
$cmin :: ComputedFieldName -> ComputedFieldName -> ComputedFieldName
min :: ComputedFieldName -> ComputedFieldName -> ComputedFieldName
Ord, ComputedFieldName -> ()
(ComputedFieldName -> ()) -> NFData ComputedFieldName
forall a. (a -> ()) -> NFData a
$crnf :: ComputedFieldName -> ()
rnf :: ComputedFieldName -> ()
NFData, Value -> Parser [ComputedFieldName]
Value -> Parser ComputedFieldName
(Value -> Parser ComputedFieldName)
-> (Value -> Parser [ComputedFieldName])
-> FromJSON ComputedFieldName
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: Value -> Parser ComputedFieldName
parseJSON :: Value -> Parser ComputedFieldName
$cparseJSONList :: Value -> Parser [ComputedFieldName]
parseJSONList :: Value -> Parser [ComputedFieldName]
FromJSON, [ComputedFieldName] -> Value
[ComputedFieldName] -> Encoding
ComputedFieldName -> Value
ComputedFieldName -> Encoding
(ComputedFieldName -> Value)
-> (ComputedFieldName -> Encoding)
-> ([ComputedFieldName] -> Value)
-> ([ComputedFieldName] -> Encoding)
-> ToJSON ComputedFieldName
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: ComputedFieldName -> Value
toJSON :: ComputedFieldName -> Value
$ctoEncoding :: ComputedFieldName -> Encoding
toEncoding :: ComputedFieldName -> Encoding
$ctoJSONList :: [ComputedFieldName] -> Value
toJSONList :: [ComputedFieldName] -> Value
$ctoEncodingList :: [ComputedFieldName] -> Encoding
toEncodingList :: [ComputedFieldName] -> Encoding
ToJSON, ToJSONKeyFunction [ComputedFieldName]
ToJSONKeyFunction ComputedFieldName
ToJSONKeyFunction ComputedFieldName
-> ToJSONKeyFunction [ComputedFieldName]
-> ToJSONKey ComputedFieldName
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
$ctoJSONKey :: ToJSONKeyFunction ComputedFieldName
toJSONKey :: ToJSONKeyFunction ComputedFieldName
$ctoJSONKeyList :: ToJSONKeyFunction [ComputedFieldName]
toJSONKeyList :: ToJSONKeyFunction [ComputedFieldName]
ToJSONKey, ComputedFieldName -> PrepArg
(ComputedFieldName -> PrepArg) -> ToPrepArg ComputedFieldName
forall a. (a -> PrepArg) -> ToPrepArg a
$ctoPrepVal :: ComputedFieldName -> PrepArg
toPrepVal :: ComputedFieldName -> PrepArg
PG.ToPrepArg, ComputedFieldName -> Text
(ComputedFieldName -> Text) -> ToTxt ComputedFieldName
forall a. (a -> Text) -> ToTxt a
$ctoTxt :: ComputedFieldName -> Text
toTxt :: ComputedFieldName -> Text
ToTxt, Eq ComputedFieldName
Eq ComputedFieldName
-> (Int -> ComputedFieldName -> Int)
-> (ComputedFieldName -> Int)
-> Hashable ComputedFieldName
Int -> ComputedFieldName -> Int
ComputedFieldName -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> ComputedFieldName -> Int
hashWithSalt :: Int -> ComputedFieldName -> Int
$chash :: ComputedFieldName -> Int
hash :: ComputedFieldName -> Int
Hashable, Maybe ByteString -> Either Text ComputedFieldName
(Maybe ByteString -> Either Text ComputedFieldName)
-> FromCol ComputedFieldName
forall a. (Maybe ByteString -> Either Text a) -> FromCol a
$cfromCol :: Maybe ByteString -> Either Text ComputedFieldName
fromCol :: Maybe ByteString -> Either Text ComputedFieldName
PG.FromCol, (forall x. ComputedFieldName -> Rep ComputedFieldName x)
-> (forall x. Rep ComputedFieldName x -> ComputedFieldName)
-> Generic ComputedFieldName
forall x. Rep ComputedFieldName x -> ComputedFieldName
forall x. ComputedFieldName -> Rep ComputedFieldName x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ComputedFieldName -> Rep ComputedFieldName x
from :: forall x. ComputedFieldName -> Rep ComputedFieldName x
$cto :: forall x. Rep ComputedFieldName x -> ComputedFieldName
to :: forall x. Rep ComputedFieldName x -> ComputedFieldName
Generic)

instance HasCodec ComputedFieldName where
  codec :: JSONCodec ComputedFieldName
codec = (NonEmptyText -> ComputedFieldName)
-> (ComputedFieldName -> NonEmptyText)
-> Codec Value NonEmptyText NonEmptyText
-> JSONCodec ComputedFieldName
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
dimapCodec NonEmptyText -> ComputedFieldName
ComputedFieldName ComputedFieldName -> NonEmptyText
unComputedFieldName Codec Value NonEmptyText NonEmptyText
forall value. HasCodec value => JSONCodec value
codec

computedFieldNameToText :: ComputedFieldName -> Text
computedFieldNameToText :: ComputedFieldName -> Text
computedFieldNameToText = NonEmptyText -> Text
unNonEmptyText (NonEmptyText -> Text)
-> (ComputedFieldName -> NonEmptyText) -> ComputedFieldName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ComputedFieldName -> NonEmptyText
unComputedFieldName

fromComputedField :: ComputedFieldName -> FieldName
fromComputedField :: ComputedFieldName -> FieldName
fromComputedField = Text -> FieldName
FieldName (Text -> FieldName)
-> (ComputedFieldName -> Text) -> ComputedFieldName -> FieldName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ComputedFieldName -> Text
computedFieldNameToText