{-# LANGUAGE StandaloneKindSignatures #-}

module Hasura.Backends.DataConnector.IR.Name
  ( Name (..),
    NameType (..),
  )
where

--------------------------------------------------------------------------------

import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey)
import Data.Kind (Type)
import Data.Text.Extended (ToTxt)
import Hasura.Backends.DataConnector.API qualified as API
import Hasura.Base.ErrorValue qualified as ErrorValue
import Hasura.Base.ToErrorValue
import Hasura.Incremental (Cacheable)
import Hasura.Prelude
import Witch qualified

--------------------------------------------------------------------------------

-- | A tagged, opaque wrapper around 'Text' that provides a number of derived
-- derived instances (primarily as required by the @Backend@ typeclass).
--
-- This wrapper is indexed by 'NameType' so that different "names" can be
-- represented as semantically distinct types without the boilerplate of
-- actually defining these wrappers separately.
type Name :: NameType -> Type
newtype Name ty = Name {Name ty -> Text
unName :: Text}
  deriving stock (Typeable (Name ty)
DataType
Constr
Typeable (Name ty)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Name ty -> c (Name ty))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Name ty))
-> (Name ty -> Constr)
-> (Name ty -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Name ty)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Name ty)))
-> ((forall b. Data b => b -> b) -> Name ty -> Name ty)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Name ty -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Name ty -> r)
-> (forall u. (forall d. Data d => d -> u) -> Name ty -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Name ty -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Name ty -> m (Name ty))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Name ty -> m (Name ty))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Name ty -> m (Name ty))
-> Data (Name ty)
Name ty -> DataType
Name ty -> Constr
(forall b. Data b => b -> b) -> Name ty -> Name ty
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Name ty -> c (Name ty)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Name ty)
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Name ty -> u
forall u. (forall d. Data d => d -> u) -> Name ty -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Name ty -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Name ty -> r
forall (ty :: NameType). Typeable ty => Typeable (Name ty)
forall (ty :: NameType). Typeable ty => Name ty -> DataType
forall (ty :: NameType). Typeable ty => Name ty -> Constr
forall (ty :: NameType).
Typeable ty =>
(forall b. Data b => b -> b) -> Name ty -> Name ty
forall (ty :: NameType) u.
Typeable ty =>
Int -> (forall d. Data d => d -> u) -> Name ty -> u
forall (ty :: NameType) u.
Typeable ty =>
(forall d. Data d => d -> u) -> Name ty -> [u]
forall (ty :: NameType) r r'.
Typeable ty =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Name ty -> r
forall (ty :: NameType) r r'.
Typeable ty =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Name ty -> r
forall (ty :: NameType) (m :: * -> *).
(Typeable ty, Monad m) =>
(forall d. Data d => d -> m d) -> Name ty -> m (Name ty)
forall (ty :: NameType) (m :: * -> *).
(Typeable ty, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Name ty -> m (Name ty)
forall (ty :: NameType) (c :: * -> *).
Typeable ty =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Name ty)
forall (ty :: NameType) (c :: * -> *).
Typeable ty =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Name ty -> c (Name ty)
forall (ty :: NameType) (t :: * -> *) (c :: * -> *).
(Typeable ty, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Name ty))
forall (ty :: NameType) (t :: * -> * -> *) (c :: * -> *).
(Typeable ty, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Name ty))
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Name ty -> m (Name ty)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Name ty -> m (Name ty)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Name ty)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Name ty -> c (Name ty)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Name ty))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Name ty))
$cName :: Constr
$tName :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Name ty -> m (Name ty)
$cgmapMo :: forall (ty :: NameType) (m :: * -> *).
(Typeable ty, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Name ty -> m (Name ty)
gmapMp :: (forall d. Data d => d -> m d) -> Name ty -> m (Name ty)
$cgmapMp :: forall (ty :: NameType) (m :: * -> *).
(Typeable ty, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Name ty -> m (Name ty)
gmapM :: (forall d. Data d => d -> m d) -> Name ty -> m (Name ty)
$cgmapM :: forall (ty :: NameType) (m :: * -> *).
(Typeable ty, Monad m) =>
(forall d. Data d => d -> m d) -> Name ty -> m (Name ty)
gmapQi :: Int -> (forall d. Data d => d -> u) -> Name ty -> u
$cgmapQi :: forall (ty :: NameType) u.
Typeable ty =>
Int -> (forall d. Data d => d -> u) -> Name ty -> u
gmapQ :: (forall d. Data d => d -> u) -> Name ty -> [u]
$cgmapQ :: forall (ty :: NameType) u.
Typeable ty =>
(forall d. Data d => d -> u) -> Name ty -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Name ty -> r
$cgmapQr :: forall (ty :: NameType) r r'.
Typeable ty =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Name ty -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Name ty -> r
$cgmapQl :: forall (ty :: NameType) r r'.
Typeable ty =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Name ty -> r
gmapT :: (forall b. Data b => b -> b) -> Name ty -> Name ty
$cgmapT :: forall (ty :: NameType).
Typeable ty =>
(forall b. Data b => b -> b) -> Name ty -> Name ty
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Name ty))
$cdataCast2 :: forall (ty :: NameType) (t :: * -> * -> *) (c :: * -> *).
(Typeable ty, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Name ty))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (Name ty))
$cdataCast1 :: forall (ty :: NameType) (t :: * -> *) (c :: * -> *).
(Typeable ty, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Name ty))
dataTypeOf :: Name ty -> DataType
$cdataTypeOf :: forall (ty :: NameType). Typeable ty => Name ty -> DataType
toConstr :: Name ty -> Constr
$ctoConstr :: forall (ty :: NameType). Typeable ty => Name ty -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Name ty)
$cgunfold :: forall (ty :: NameType) (c :: * -> *).
Typeable ty =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Name ty)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Name ty -> c (Name ty)
$cgfoldl :: forall (ty :: NameType) (c :: * -> *).
Typeable ty =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Name ty -> c (Name ty)
$cp1Data :: forall (ty :: NameType). Typeable ty => Typeable (Name ty)
Data, Name ty -> Name ty -> Bool
(Name ty -> Name ty -> Bool)
-> (Name ty -> Name ty -> Bool) -> Eq (Name ty)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (ty :: NameType). Name ty -> Name ty -> Bool
/= :: Name ty -> Name ty -> Bool
$c/= :: forall (ty :: NameType). Name ty -> Name ty -> Bool
== :: Name ty -> Name ty -> Bool
$c== :: forall (ty :: NameType). Name ty -> Name ty -> Bool
Eq, (forall x. Name ty -> Rep (Name ty) x)
-> (forall x. Rep (Name ty) x -> Name ty) -> Generic (Name ty)
forall x. Rep (Name ty) x -> Name ty
forall x. Name ty -> Rep (Name ty) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (ty :: NameType) x. Rep (Name ty) x -> Name ty
forall (ty :: NameType) x. Name ty -> Rep (Name ty) x
$cto :: forall (ty :: NameType) x. Rep (Name ty) x -> Name ty
$cfrom :: forall (ty :: NameType) x. Name ty -> Rep (Name ty) x
Generic, Eq (Name ty)
Eq (Name ty)
-> (Name ty -> Name ty -> Ordering)
-> (Name ty -> Name ty -> Bool)
-> (Name ty -> Name ty -> Bool)
-> (Name ty -> Name ty -> Bool)
-> (Name ty -> Name ty -> Bool)
-> (Name ty -> Name ty -> Name ty)
-> (Name ty -> Name ty -> Name ty)
-> Ord (Name ty)
Name ty -> Name ty -> Bool
Name ty -> Name ty -> Ordering
Name ty -> Name ty -> Name ty
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
forall (ty :: NameType). Eq (Name ty)
forall (ty :: NameType). Name ty -> Name ty -> Bool
forall (ty :: NameType). Name ty -> Name ty -> Ordering
forall (ty :: NameType). Name ty -> Name ty -> Name ty
min :: Name ty -> Name ty -> Name ty
$cmin :: forall (ty :: NameType). Name ty -> Name ty -> Name ty
max :: Name ty -> Name ty -> Name ty
$cmax :: forall (ty :: NameType). Name ty -> Name ty -> Name ty
>= :: Name ty -> Name ty -> Bool
$c>= :: forall (ty :: NameType). Name ty -> Name ty -> Bool
> :: Name ty -> Name ty -> Bool
$c> :: forall (ty :: NameType). Name ty -> Name ty -> Bool
<= :: Name ty -> Name ty -> Bool
$c<= :: forall (ty :: NameType). Name ty -> Name ty -> Bool
< :: Name ty -> Name ty -> Bool
$c< :: forall (ty :: NameType). Name ty -> Name ty -> Bool
compare :: Name ty -> Name ty -> Ordering
$ccompare :: forall (ty :: NameType). Name ty -> Name ty -> Ordering
$cp1Ord :: forall (ty :: NameType). Eq (Name ty)
Ord, Int -> Name ty -> ShowS
[Name ty] -> ShowS
Name ty -> String
(Int -> Name ty -> ShowS)
-> (Name ty -> String) -> ([Name ty] -> ShowS) -> Show (Name ty)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (ty :: NameType). Int -> Name ty -> ShowS
forall (ty :: NameType). [Name ty] -> ShowS
forall (ty :: NameType). Name ty -> String
showList :: [Name ty] -> ShowS
$cshowList :: forall (ty :: NameType). [Name ty] -> ShowS
show :: Name ty -> String
$cshow :: forall (ty :: NameType). Name ty -> String
showsPrec :: Int -> Name ty -> ShowS
$cshowsPrec :: forall (ty :: NameType). Int -> Name ty -> ShowS
Show)
  deriving newtype
    ( Eq (Name ty)
Eq (Name ty)
-> (Accesses -> Name ty -> Name ty -> Bool) -> Cacheable (Name ty)
Accesses -> Name ty -> Name ty -> Bool
forall a. Eq a -> (Accesses -> a -> a -> Bool) -> Cacheable a
forall (ty :: NameType). Eq (Name ty)
forall (ty :: NameType). Accesses -> Name ty -> Name ty -> Bool
unchanged :: Accesses -> Name ty -> Name ty -> Bool
$cunchanged :: forall (ty :: NameType). Accesses -> Name ty -> Name ty -> Bool
$cp1Cacheable :: forall (ty :: NameType). Eq (Name ty)
Cacheable,
      Value -> Parser [Name ty]
Value -> Parser (Name ty)
(Value -> Parser (Name ty))
-> (Value -> Parser [Name ty]) -> FromJSON (Name ty)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
forall (ty :: NameType). Value -> Parser [Name ty]
forall (ty :: NameType). Value -> Parser (Name ty)
parseJSONList :: Value -> Parser [Name ty]
$cparseJSONList :: forall (ty :: NameType). Value -> Parser [Name ty]
parseJSON :: Value -> Parser (Name ty)
$cparseJSON :: forall (ty :: NameType). Value -> Parser (Name ty)
FromJSON,
      FromJSONKeyFunction [Name ty]
FromJSONKeyFunction (Name ty)
FromJSONKeyFunction (Name ty)
-> FromJSONKeyFunction [Name ty] -> FromJSONKey (Name ty)
forall a.
FromJSONKeyFunction a -> FromJSONKeyFunction [a] -> FromJSONKey a
forall (ty :: NameType). FromJSONKeyFunction [Name ty]
forall (ty :: NameType). FromJSONKeyFunction (Name ty)
fromJSONKeyList :: FromJSONKeyFunction [Name ty]
$cfromJSONKeyList :: forall (ty :: NameType). FromJSONKeyFunction [Name ty]
fromJSONKey :: FromJSONKeyFunction (Name ty)
$cfromJSONKey :: forall (ty :: NameType). FromJSONKeyFunction (Name ty)
FromJSONKey,
      Int -> Name ty -> Int
Name ty -> Int
(Int -> Name ty -> Int) -> (Name ty -> Int) -> Hashable (Name ty)
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
forall (ty :: NameType). Int -> Name ty -> Int
forall (ty :: NameType). Name ty -> Int
hash :: Name ty -> Int
$chash :: forall (ty :: NameType). Name ty -> Int
hashWithSalt :: Int -> Name ty -> Int
$chashWithSalt :: forall (ty :: NameType). Int -> Name ty -> Int
Hashable,
      Name ty -> ()
(Name ty -> ()) -> NFData (Name ty)
forall a. (a -> ()) -> NFData a
forall (ty :: NameType). Name ty -> ()
rnf :: Name ty -> ()
$crnf :: forall (ty :: NameType). Name ty -> ()
NFData,
      [Name ty] -> Value
[Name ty] -> Encoding
Name ty -> Value
Name ty -> Encoding
(Name ty -> Value)
-> (Name ty -> Encoding)
-> ([Name ty] -> Value)
-> ([Name ty] -> Encoding)
-> ToJSON (Name ty)
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
forall (ty :: NameType). [Name ty] -> Value
forall (ty :: NameType). [Name ty] -> Encoding
forall (ty :: NameType). Name ty -> Value
forall (ty :: NameType). Name ty -> Encoding
toEncodingList :: [Name ty] -> Encoding
$ctoEncodingList :: forall (ty :: NameType). [Name ty] -> Encoding
toJSONList :: [Name ty] -> Value
$ctoJSONList :: forall (ty :: NameType). [Name ty] -> Value
toEncoding :: Name ty -> Encoding
$ctoEncoding :: forall (ty :: NameType). Name ty -> Encoding
toJSON :: Name ty -> Value
$ctoJSON :: forall (ty :: NameType). Name ty -> Value
ToJSON,
      ToJSONKeyFunction [Name ty]
ToJSONKeyFunction (Name ty)
ToJSONKeyFunction (Name ty)
-> ToJSONKeyFunction [Name ty] -> ToJSONKey (Name ty)
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
forall (ty :: NameType). ToJSONKeyFunction [Name ty]
forall (ty :: NameType). ToJSONKeyFunction (Name ty)
toJSONKeyList :: ToJSONKeyFunction [Name ty]
$ctoJSONKeyList :: forall (ty :: NameType). ToJSONKeyFunction [Name ty]
toJSONKey :: ToJSONKeyFunction (Name ty)
$ctoJSONKey :: forall (ty :: NameType). ToJSONKeyFunction (Name ty)
ToJSONKey,
      Name ty -> Text
(Name ty -> Text) -> ToTxt (Name ty)
forall a. (a -> Text) -> ToTxt a
forall (ty :: NameType). Name ty -> Text
toTxt :: Name ty -> Text
$ctoTxt :: forall (ty :: NameType). Name ty -> Text
ToTxt
    )

instance ToErrorValue (Name ty) where
  toErrorValue :: Name ty -> ErrorMessage
toErrorValue = Text -> ErrorMessage
ErrorValue.squote (Text -> ErrorMessage)
-> (Name ty -> Text) -> Name ty -> ErrorMessage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name ty -> Text
forall (ty :: NameType). Name ty -> Text
unName

instance Witch.From API.ColumnName (Name 'Column) where
  from :: ColumnName -> Name 'Column
from (API.ColumnName Text
n) = Text -> Name 'Column
forall (ty :: NameType). Text -> Name ty
Name Text
n

instance Witch.From (Name 'Column) API.ColumnName where
  from :: Name 'Column -> ColumnName
from (Name Text
n) = Text -> ColumnName
API.ColumnName Text
n

instance Witch.From API.RelationshipName (Name 'Relationship) where
  from :: RelationshipName -> Name 'Relationship
from (API.RelationshipName Text
n) = Text -> Name 'Relationship
forall (ty :: NameType). Text -> Name ty
Name Text
n

instance Witch.From (Name 'Relationship) API.RelationshipName where
  from :: Name 'Relationship -> RelationshipName
from (Name Text
n) = Text -> RelationshipName
API.RelationshipName Text
n

instance Witch.From Text (Name 'Column) where
  from :: Text -> Name 'Column
from = Text -> Name 'Column
coerce

instance Witch.From Text (Name 'Relationship) where
  from :: Text -> Name 'Relationship
from = Text -> Name 'Relationship
coerce

-- | The "type" of "name" that the 'Name' type is meant to provide a textual
-- representation for.
--
-- In other words: an enumeration of all the types for which 'Name' acts as a
-- shared abstraction.
data NameType
  = Column
  | Relationship