{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}

-- | In order to avoid circular dependencies while splitting
-- 'Hasura.RQL.Types.Metadata' into multiple modules, some definitions must be
-- moved out of that module. This module is the bucket for definitions that have
-- not been specifically moved elsewhere.
module Hasura.RQL.Types.Metadata.Common
  ( Actions,
    BackendConfigWrapper (..),
    BackendSourceMetadata (..),
    CatalogState (..),
    CatalogStateType (..),
    ComputedFieldMetadata (..),
    ComputedFields,
    CronTriggers,
    Endpoints,
    EventTriggers,
    FunctionMetadata (..),
    Functions,
    GetCatalogState (..),
    InheritedRoles,
    Permissions,
    QueryCollections,
    Relationships,
    SchemaRemoteRelationships,
    RemoteSchemaMetadata (..),
    RemoteSchemaPermissionMetadata (..),
    RemoteSchemas,
    RemoteSchemaTypeRelationships (..),
    SetCatalogState (..),
    SourceMetadata (..),
    Sources,
    TableMetadata (..),
    Tables,
    backendSourceMetadataCodec,
    fmComment,
    fmConfiguration,
    fmFunction,
    fmPermissions,
    getSourceName,
    mkSourceMetadata,
    mkTableMeta,
    parseNonSourcesMetadata,
    rsmComment,
    rsmDefinition,
    rsmName,
    rsmPermissions,
    rsmRemoteRelationships,
    rspmComment,
    rspmDefinition,
    rspmRole,
    rstrsName,
    rstrsRelationships,
    smConfiguration,
    smFunctions,
    smKind,
    smName,
    smQueryTags,
    smTables,
    smCustomization,
    sourcesCodec,
    tmArrayRelationships,
    tmComputedFields,
    tmConfiguration,
    tmDeletePermissions,
    tmApolloFederationConfig,
    tmEventTriggers,
    tmInsertPermissions,
    tmIsEnum,
    tmObjectRelationships,
    tmRemoteRelationships,
    tmSelectPermissions,
    tmTable,
    tmUpdatePermissions,
    toSourceMetadata,
  )
where

import Autodocodec hiding (object, (.=))
import Autodocodec qualified as AC
import Control.Lens hiding (set, (.=))
import Data.Aeson.Casing
import Data.Aeson.Extended (FromJSONWithContext (..))
import Data.Aeson.KeyMap qualified as KM
import Data.Aeson.TH
import Data.Aeson.Types
import Data.HashMap.Strict.InsOrd.Autodocodec (sortedElemsCodec, sortedElemsCodecWith)
import Data.HashMap.Strict.InsOrd.Extended qualified as OM
import Data.HashSet qualified as HS
import Data.List.Extended qualified as L
import Data.Maybe (fromJust)
import Data.Text qualified as T
import Data.Text.Extended qualified as T
import Hasura.Incremental (Cacheable)
import Hasura.Metadata.DTO.Placeholder (placeholderCodecViaJSON)
import Hasura.Prelude
import Hasura.RQL.Types.Action
import Hasura.RQL.Types.Allowlist
import Hasura.RQL.Types.ApiLimit
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.ComputedField
import Hasura.RQL.Types.CustomTypes
import Hasura.RQL.Types.Endpoint
import Hasura.RQL.Types.EventTrigger
import Hasura.RQL.Types.Function
import Hasura.RQL.Types.GraphqlSchemaIntrospection
import Hasura.RQL.Types.Permission
import Hasura.RQL.Types.QueryCollection
import Hasura.RQL.Types.QueryTags
import Hasura.RQL.Types.Relationships.Local
import Hasura.RQL.Types.Relationships.Remote
import Hasura.RQL.Types.RemoteSchema
import Hasura.RQL.Types.Roles
import Hasura.RQL.Types.ScheduledTrigger
import Hasura.RQL.Types.SourceCustomization
import Hasura.RQL.Types.Table
import Hasura.SQL.AnyBackend qualified as AB
import Hasura.SQL.Backend
import Hasura.SQL.Tag (BackendTag, HasTag (backendTag), reify)
import Hasura.Session
import Language.GraphQL.Draft.Syntax qualified as G

-- | Parse a list of objects into a map from a derived key,
-- failing if the list has duplicates.
parseListAsMap ::
  (Hashable k, Eq k, T.ToTxt k) =>
  Text ->
  (a -> k) ->
  Parser [a] ->
  Parser (InsOrdHashMap k a)
parseListAsMap :: Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
things a -> k
mapFn Parser [a]
listP = do
  [a]
list <- Parser [a]
listP
  let duplicates :: [k]
duplicates = HashSet k -> [k]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (HashSet k -> [k]) -> HashSet k -> [k]
forall a b. (a -> b) -> a -> b
$ [k] -> HashSet k
forall a. (Eq a, Hashable a) => [a] -> HashSet a
L.duplicates ([k] -> HashSet k) -> [k] -> HashSet k
forall a b. (a -> b) -> a -> b
$ (a -> k) -> [a] -> [k]
forall a b. (a -> b) -> [a] -> [b]
map a -> k
mapFn [a]
list
  Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([k] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [k]
duplicates) (Parser () -> Parser ()) -> Parser () -> Parser ()
forall a b. (a -> b) -> a -> b
$
    String -> Parser ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser ()) -> String -> Parser ()
forall a b. (a -> b) -> a -> b
$
      Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$
        Text
"multiple declarations exist for the following " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
things Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
": "
          Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [k] -> Text
forall t (f :: * -> *). (ToTxt t, Foldable f) => f t -> Text
T.commaSeparated [k]
duplicates
  InsOrdHashMap k a -> Parser (InsOrdHashMap k a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InsOrdHashMap k a -> Parser (InsOrdHashMap k a))
-> InsOrdHashMap k a -> Parser (InsOrdHashMap k a)
forall a b. (a -> b) -> a -> b
$ (a -> k) -> [a] -> InsOrdHashMap k a
forall k a.
(Eq k, Hashable k) =>
(a -> k) -> [a] -> InsOrdHashMap k a
oMapFromL a -> k
mapFn [a]
list

data ComputedFieldMetadata b = ComputedFieldMetadata
  { ComputedFieldMetadata b -> ComputedFieldName
_cfmName :: ComputedFieldName,
    ComputedFieldMetadata b -> ComputedFieldDefinition b
_cfmDefinition :: ComputedFieldDefinition b,
    ComputedFieldMetadata b -> Comment
_cfmComment :: Comment
  }
  deriving ((forall x.
 ComputedFieldMetadata b -> Rep (ComputedFieldMetadata b) x)
-> (forall x.
    Rep (ComputedFieldMetadata b) x -> ComputedFieldMetadata b)
-> Generic (ComputedFieldMetadata b)
forall x.
Rep (ComputedFieldMetadata b) x -> ComputedFieldMetadata b
forall x.
ComputedFieldMetadata b -> Rep (ComputedFieldMetadata b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) x.
Rep (ComputedFieldMetadata b) x -> ComputedFieldMetadata b
forall (b :: BackendType) x.
ComputedFieldMetadata b -> Rep (ComputedFieldMetadata b) x
$cto :: forall (b :: BackendType) x.
Rep (ComputedFieldMetadata b) x -> ComputedFieldMetadata b
$cfrom :: forall (b :: BackendType) x.
ComputedFieldMetadata b -> Rep (ComputedFieldMetadata b) x
Generic)

deriving instance (Backend b) => Show (ComputedFieldMetadata b)

deriving instance (Backend b) => Eq (ComputedFieldMetadata b)

instance (Backend b) => Cacheable (ComputedFieldMetadata b)

instance (Backend b) => ToJSON (ComputedFieldMetadata b) where
  toJSON :: ComputedFieldMetadata b -> Value
toJSON ComputedFieldMetadata {Comment
ComputedFieldDefinition b
ComputedFieldName
_cfmComment :: Comment
_cfmDefinition :: ComputedFieldDefinition b
_cfmName :: ComputedFieldName
_cfmComment :: forall (b :: BackendType). ComputedFieldMetadata b -> Comment
_cfmDefinition :: forall (b :: BackendType).
ComputedFieldMetadata b -> ComputedFieldDefinition b
_cfmName :: forall (b :: BackendType).
ComputedFieldMetadata b -> ComputedFieldName
..} =
    [Pair] -> Value
object
      [ Key
"name" Key -> ComputedFieldName -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= ComputedFieldName
_cfmName,
        Key
"definition" Key -> ComputedFieldDefinition b -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= ComputedFieldDefinition b
_cfmDefinition,
        Key
"comment" Key -> Comment -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Comment
_cfmComment
      ]

instance (Backend b) => FromJSON (ComputedFieldMetadata b) where
  parseJSON :: Value -> Parser (ComputedFieldMetadata b)
parseJSON = String
-> (Object -> Parser (ComputedFieldMetadata b))
-> Value
-> Parser (ComputedFieldMetadata b)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"ComputedFieldMetadata" ((Object -> Parser (ComputedFieldMetadata b))
 -> Value -> Parser (ComputedFieldMetadata b))
-> (Object -> Parser (ComputedFieldMetadata b))
-> Value
-> Parser (ComputedFieldMetadata b)
forall a b. (a -> b) -> a -> b
$ \Object
obj ->
    ComputedFieldName
-> ComputedFieldDefinition b -> Comment -> ComputedFieldMetadata b
forall (b :: BackendType).
ComputedFieldName
-> ComputedFieldDefinition b -> Comment -> ComputedFieldMetadata b
ComputedFieldMetadata
      (ComputedFieldName
 -> ComputedFieldDefinition b -> Comment -> ComputedFieldMetadata b)
-> Parser ComputedFieldName
-> Parser
     (ComputedFieldDefinition b -> Comment -> ComputedFieldMetadata b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
obj Object -> Key -> Parser ComputedFieldName
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
      Parser
  (ComputedFieldDefinition b -> Comment -> ComputedFieldMetadata b)
-> Parser (ComputedFieldDefinition b)
-> Parser (Comment -> ComputedFieldMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Key -> Parser (ComputedFieldDefinition b)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"definition"
      Parser (Comment -> ComputedFieldMetadata b)
-> Parser Comment -> Parser (ComputedFieldMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Key -> Parser (Maybe Comment)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"comment" Parser (Maybe Comment) -> Comment -> Parser Comment
forall a. Parser (Maybe a) -> a -> Parser a
.!= Comment
Automatic

data RemoteSchemaPermissionMetadata = RemoteSchemaPermissionMetadata
  { RemoteSchemaPermissionMetadata -> RoleName
_rspmRole :: RoleName,
    RemoteSchemaPermissionMetadata -> RemoteSchemaPermissionDefinition
_rspmDefinition :: RemoteSchemaPermissionDefinition,
    RemoteSchemaPermissionMetadata -> Maybe Text
_rspmComment :: Maybe Text
  }
  deriving (Int -> RemoteSchemaPermissionMetadata -> ShowS
[RemoteSchemaPermissionMetadata] -> ShowS
RemoteSchemaPermissionMetadata -> String
(Int -> RemoteSchemaPermissionMetadata -> ShowS)
-> (RemoteSchemaPermissionMetadata -> String)
-> ([RemoteSchemaPermissionMetadata] -> ShowS)
-> Show RemoteSchemaPermissionMetadata
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RemoteSchemaPermissionMetadata] -> ShowS
$cshowList :: [RemoteSchemaPermissionMetadata] -> ShowS
show :: RemoteSchemaPermissionMetadata -> String
$cshow :: RemoteSchemaPermissionMetadata -> String
showsPrec :: Int -> RemoteSchemaPermissionMetadata -> ShowS
$cshowsPrec :: Int -> RemoteSchemaPermissionMetadata -> ShowS
Show, RemoteSchemaPermissionMetadata
-> RemoteSchemaPermissionMetadata -> Bool
(RemoteSchemaPermissionMetadata
 -> RemoteSchemaPermissionMetadata -> Bool)
-> (RemoteSchemaPermissionMetadata
    -> RemoteSchemaPermissionMetadata -> Bool)
-> Eq RemoteSchemaPermissionMetadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RemoteSchemaPermissionMetadata
-> RemoteSchemaPermissionMetadata -> Bool
$c/= :: RemoteSchemaPermissionMetadata
-> RemoteSchemaPermissionMetadata -> Bool
== :: RemoteSchemaPermissionMetadata
-> RemoteSchemaPermissionMetadata -> Bool
$c== :: RemoteSchemaPermissionMetadata
-> RemoteSchemaPermissionMetadata -> Bool
Eq, (forall x.
 RemoteSchemaPermissionMetadata
 -> Rep RemoteSchemaPermissionMetadata x)
-> (forall x.
    Rep RemoteSchemaPermissionMetadata x
    -> RemoteSchemaPermissionMetadata)
-> Generic RemoteSchemaPermissionMetadata
forall x.
Rep RemoteSchemaPermissionMetadata x
-> RemoteSchemaPermissionMetadata
forall x.
RemoteSchemaPermissionMetadata
-> Rep RemoteSchemaPermissionMetadata x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep RemoteSchemaPermissionMetadata x
-> RemoteSchemaPermissionMetadata
$cfrom :: forall x.
RemoteSchemaPermissionMetadata
-> Rep RemoteSchemaPermissionMetadata x
Generic)

instance Cacheable RemoteSchemaPermissionMetadata

$(deriveJSON hasuraJSON {omitNothingFields = True} ''RemoteSchemaPermissionMetadata)
$(makeLenses ''RemoteSchemaPermissionMetadata)

type Relationships a = InsOrdHashMap RelName a

type ComputedFields b = InsOrdHashMap ComputedFieldName (ComputedFieldMetadata b)

type RemoteRelationships = InsOrdHashMap RelName RemoteRelationship

type SchemaRemoteRelationships = InsOrdHashMap G.Name RemoteSchemaTypeRelationships

type Permissions a = InsOrdHashMap RoleName a

type EventTriggers b = InsOrdHashMap TriggerName (EventTriggerConf b)

data RemoteSchemaTypeRelationships = RemoteSchemaTypeRelationships
  { RemoteSchemaTypeRelationships -> Name
_rstrsName :: G.Name,
    RemoteSchemaTypeRelationships -> RemoteRelationships
_rstrsRelationships :: RemoteRelationships
  }
  deriving (Int -> RemoteSchemaTypeRelationships -> ShowS
[RemoteSchemaTypeRelationships] -> ShowS
RemoteSchemaTypeRelationships -> String
(Int -> RemoteSchemaTypeRelationships -> ShowS)
-> (RemoteSchemaTypeRelationships -> String)
-> ([RemoteSchemaTypeRelationships] -> ShowS)
-> Show RemoteSchemaTypeRelationships
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RemoteSchemaTypeRelationships] -> ShowS
$cshowList :: [RemoteSchemaTypeRelationships] -> ShowS
show :: RemoteSchemaTypeRelationships -> String
$cshow :: RemoteSchemaTypeRelationships -> String
showsPrec :: Int -> RemoteSchemaTypeRelationships -> ShowS
$cshowsPrec :: Int -> RemoteSchemaTypeRelationships -> ShowS
Show, RemoteSchemaTypeRelationships
-> RemoteSchemaTypeRelationships -> Bool
(RemoteSchemaTypeRelationships
 -> RemoteSchemaTypeRelationships -> Bool)
-> (RemoteSchemaTypeRelationships
    -> RemoteSchemaTypeRelationships -> Bool)
-> Eq RemoteSchemaTypeRelationships
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RemoteSchemaTypeRelationships
-> RemoteSchemaTypeRelationships -> Bool
$c/= :: RemoteSchemaTypeRelationships
-> RemoteSchemaTypeRelationships -> Bool
== :: RemoteSchemaTypeRelationships
-> RemoteSchemaTypeRelationships -> Bool
$c== :: RemoteSchemaTypeRelationships
-> RemoteSchemaTypeRelationships -> Bool
Eq, (forall x.
 RemoteSchemaTypeRelationships
 -> Rep RemoteSchemaTypeRelationships x)
-> (forall x.
    Rep RemoteSchemaTypeRelationships x
    -> RemoteSchemaTypeRelationships)
-> Generic RemoteSchemaTypeRelationships
forall x.
Rep RemoteSchemaTypeRelationships x
-> RemoteSchemaTypeRelationships
forall x.
RemoteSchemaTypeRelationships
-> Rep RemoteSchemaTypeRelationships x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep RemoteSchemaTypeRelationships x
-> RemoteSchemaTypeRelationships
$cfrom :: forall x.
RemoteSchemaTypeRelationships
-> Rep RemoteSchemaTypeRelationships x
Generic)

instance FromJSON RemoteSchemaTypeRelationships where
  parseJSON :: Value -> Parser RemoteSchemaTypeRelationships
parseJSON = String
-> (Object -> Parser RemoteSchemaTypeRelationships)
-> Value
-> Parser RemoteSchemaTypeRelationships
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"RemoteSchemaMetadata" \Object
obj ->
    Name -> RemoteRelationships -> RemoteSchemaTypeRelationships
RemoteSchemaTypeRelationships
      (Name -> RemoteRelationships -> RemoteSchemaTypeRelationships)
-> Parser Name
-> Parser (RemoteRelationships -> RemoteSchemaTypeRelationships)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
obj Object -> Key -> Parser Name
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type_name"
      Parser (RemoteRelationships -> RemoteSchemaTypeRelationships)
-> Parser RemoteRelationships
-> Parser RemoteSchemaTypeRelationships
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((RemoteRelationship -> RelName)
-> [RemoteRelationship] -> RemoteRelationships
forall k a.
(Eq k, Hashable k) =>
(a -> k) -> [a] -> InsOrdHashMap k a
oMapFromL RemoteRelationship -> RelName
_rrName ([RemoteRelationship] -> RemoteRelationships)
-> Parser [RemoteRelationship] -> Parser RemoteRelationships
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
obj Object -> Key -> Parser (Maybe [RemoteRelationship])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"relationships" Parser (Maybe [RemoteRelationship])
-> [RemoteRelationship] -> Parser [RemoteRelationship]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [])

instance ToJSON RemoteSchemaTypeRelationships where
  toJSON :: RemoteSchemaTypeRelationships -> Value
toJSON RemoteSchemaTypeRelationships {RemoteRelationships
Name
_rstrsRelationships :: RemoteRelationships
_rstrsName :: Name
_rstrsRelationships :: RemoteSchemaTypeRelationships -> RemoteRelationships
_rstrsName :: RemoteSchemaTypeRelationships -> Name
..} =
    [Pair] -> Value
object
      [ Key
"type_name" Key -> Name -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Name
_rstrsName,
        Key
"relationships" Key -> [RemoteRelationship] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= RemoteRelationships -> [RemoteRelationship]
forall k v. InsOrdHashMap k v -> [v]
OM.elems RemoteRelationships
_rstrsRelationships
      ]

instance Cacheable RemoteSchemaTypeRelationships

data RemoteSchemaMetadata = RemoteSchemaMetadata
  { RemoteSchemaMetadata -> RemoteSchemaName
_rsmName :: RemoteSchemaName,
    RemoteSchemaMetadata -> RemoteSchemaDef
_rsmDefinition :: RemoteSchemaDef,
    RemoteSchemaMetadata -> Maybe Text
_rsmComment :: Maybe Text,
    RemoteSchemaMetadata -> [RemoteSchemaPermissionMetadata]
_rsmPermissions :: [RemoteSchemaPermissionMetadata],
    RemoteSchemaMetadata -> SchemaRemoteRelationships
_rsmRemoteRelationships :: SchemaRemoteRelationships
  }
  deriving (Int -> RemoteSchemaMetadata -> ShowS
[RemoteSchemaMetadata] -> ShowS
RemoteSchemaMetadata -> String
(Int -> RemoteSchemaMetadata -> ShowS)
-> (RemoteSchemaMetadata -> String)
-> ([RemoteSchemaMetadata] -> ShowS)
-> Show RemoteSchemaMetadata
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RemoteSchemaMetadata] -> ShowS
$cshowList :: [RemoteSchemaMetadata] -> ShowS
show :: RemoteSchemaMetadata -> String
$cshow :: RemoteSchemaMetadata -> String
showsPrec :: Int -> RemoteSchemaMetadata -> ShowS
$cshowsPrec :: Int -> RemoteSchemaMetadata -> ShowS
Show, RemoteSchemaMetadata -> RemoteSchemaMetadata -> Bool
(RemoteSchemaMetadata -> RemoteSchemaMetadata -> Bool)
-> (RemoteSchemaMetadata -> RemoteSchemaMetadata -> Bool)
-> Eq RemoteSchemaMetadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RemoteSchemaMetadata -> RemoteSchemaMetadata -> Bool
$c/= :: RemoteSchemaMetadata -> RemoteSchemaMetadata -> Bool
== :: RemoteSchemaMetadata -> RemoteSchemaMetadata -> Bool
$c== :: RemoteSchemaMetadata -> RemoteSchemaMetadata -> Bool
Eq, (forall x. RemoteSchemaMetadata -> Rep RemoteSchemaMetadata x)
-> (forall x. Rep RemoteSchemaMetadata x -> RemoteSchemaMetadata)
-> Generic RemoteSchemaMetadata
forall x. Rep RemoteSchemaMetadata x -> RemoteSchemaMetadata
forall x. RemoteSchemaMetadata -> Rep RemoteSchemaMetadata x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RemoteSchemaMetadata x -> RemoteSchemaMetadata
$cfrom :: forall x. RemoteSchemaMetadata -> Rep RemoteSchemaMetadata x
Generic)

instance Cacheable RemoteSchemaMetadata

instance FromJSON RemoteSchemaMetadata where
  parseJSON :: Value -> Parser RemoteSchemaMetadata
parseJSON = String
-> (Object -> Parser RemoteSchemaMetadata)
-> Value
-> Parser RemoteSchemaMetadata
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"RemoteSchemaMetadata" \Object
obj ->
    RemoteSchemaName
-> RemoteSchemaDef
-> Maybe Text
-> [RemoteSchemaPermissionMetadata]
-> SchemaRemoteRelationships
-> RemoteSchemaMetadata
RemoteSchemaMetadata
      (RemoteSchemaName
 -> RemoteSchemaDef
 -> Maybe Text
 -> [RemoteSchemaPermissionMetadata]
 -> SchemaRemoteRelationships
 -> RemoteSchemaMetadata)
-> Parser RemoteSchemaName
-> Parser
     (RemoteSchemaDef
      -> Maybe Text
      -> [RemoteSchemaPermissionMetadata]
      -> SchemaRemoteRelationships
      -> RemoteSchemaMetadata)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
obj Object -> Key -> Parser RemoteSchemaName
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
      Parser
  (RemoteSchemaDef
   -> Maybe Text
   -> [RemoteSchemaPermissionMetadata]
   -> SchemaRemoteRelationships
   -> RemoteSchemaMetadata)
-> Parser RemoteSchemaDef
-> Parser
     (Maybe Text
      -> [RemoteSchemaPermissionMetadata]
      -> SchemaRemoteRelationships
      -> RemoteSchemaMetadata)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Key -> Parser RemoteSchemaDef
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"definition"
      Parser
  (Maybe Text
   -> [RemoteSchemaPermissionMetadata]
   -> SchemaRemoteRelationships
   -> RemoteSchemaMetadata)
-> Parser (Maybe Text)
-> Parser
     ([RemoteSchemaPermissionMetadata]
      -> SchemaRemoteRelationships -> RemoteSchemaMetadata)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"comment"
      Parser
  ([RemoteSchemaPermissionMetadata]
   -> SchemaRemoteRelationships -> RemoteSchemaMetadata)
-> Parser [RemoteSchemaPermissionMetadata]
-> Parser (SchemaRemoteRelationships -> RemoteSchemaMetadata)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj Object -> Key -> Parser (Maybe [RemoteSchemaPermissionMetadata])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"permissions" Parser (Maybe [RemoteSchemaPermissionMetadata])
-> [RemoteSchemaPermissionMetadata]
-> Parser [RemoteSchemaPermissionMetadata]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [RemoteSchemaPermissionMetadata]
forall a. Monoid a => a
mempty
      Parser (SchemaRemoteRelationships -> RemoteSchemaMetadata)
-> Parser SchemaRemoteRelationships -> Parser RemoteSchemaMetadata
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((RemoteSchemaTypeRelationships -> Name)
-> [RemoteSchemaTypeRelationships] -> SchemaRemoteRelationships
forall k a.
(Eq k, Hashable k) =>
(a -> k) -> [a] -> InsOrdHashMap k a
oMapFromL RemoteSchemaTypeRelationships -> Name
_rstrsName ([RemoteSchemaTypeRelationships] -> SchemaRemoteRelationships)
-> Parser [RemoteSchemaTypeRelationships]
-> Parser SchemaRemoteRelationships
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
obj Object -> Key -> Parser (Maybe [RemoteSchemaTypeRelationships])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"remote_relationships" Parser (Maybe [RemoteSchemaTypeRelationships])
-> [RemoteSchemaTypeRelationships]
-> Parser [RemoteSchemaTypeRelationships]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [])

instance ToJSON RemoteSchemaMetadata where
  toJSON :: RemoteSchemaMetadata -> Value
toJSON RemoteSchemaMetadata {[RemoteSchemaPermissionMetadata]
Maybe Text
SchemaRemoteRelationships
RemoteSchemaName
RemoteSchemaDef
_rsmRemoteRelationships :: SchemaRemoteRelationships
_rsmPermissions :: [RemoteSchemaPermissionMetadata]
_rsmComment :: Maybe Text
_rsmDefinition :: RemoteSchemaDef
_rsmName :: RemoteSchemaName
_rsmRemoteRelationships :: RemoteSchemaMetadata -> SchemaRemoteRelationships
_rsmPermissions :: RemoteSchemaMetadata -> [RemoteSchemaPermissionMetadata]
_rsmComment :: RemoteSchemaMetadata -> Maybe Text
_rsmDefinition :: RemoteSchemaMetadata -> RemoteSchemaDef
_rsmName :: RemoteSchemaMetadata -> RemoteSchemaName
..} =
    [Pair] -> Value
object
      [ Key
"name" Key -> RemoteSchemaName -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= RemoteSchemaName
_rsmName,
        Key
"definition" Key -> RemoteSchemaDef -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= RemoteSchemaDef
_rsmDefinition,
        Key
"comment" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Maybe Text
_rsmComment,
        Key
"permissions" Key -> [RemoteSchemaPermissionMetadata] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [RemoteSchemaPermissionMetadata]
_rsmPermissions,
        Key
"remote_relationships" Key -> [RemoteSchemaTypeRelationships] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= SchemaRemoteRelationships -> [RemoteSchemaTypeRelationships]
forall k v. InsOrdHashMap k v -> [v]
OM.elems SchemaRemoteRelationships
_rsmRemoteRelationships
      ]

$(makeLenses ''RemoteSchemaTypeRelationships)
$(makeLenses ''RemoteSchemaMetadata)

data TableMetadata b = TableMetadata
  { TableMetadata b -> TableName b
_tmTable :: TableName b,
    TableMetadata b -> Bool
_tmIsEnum :: Bool,
    TableMetadata b -> TableConfig b
_tmConfiguration :: TableConfig b,
    TableMetadata b -> Relationships (ObjRelDef b)
_tmObjectRelationships :: Relationships (ObjRelDef b),
    TableMetadata b -> Relationships (ArrRelDef b)
_tmArrayRelationships :: Relationships (ArrRelDef b),
    TableMetadata b -> ComputedFields b
_tmComputedFields :: ComputedFields b,
    TableMetadata b -> RemoteRelationships
_tmRemoteRelationships :: RemoteRelationships,
    TableMetadata b -> Permissions (InsPermDef b)
_tmInsertPermissions :: Permissions (InsPermDef b),
    TableMetadata b -> Permissions (SelPermDef b)
_tmSelectPermissions :: Permissions (SelPermDef b),
    TableMetadata b -> Permissions (UpdPermDef b)
_tmUpdatePermissions :: Permissions (UpdPermDef b),
    TableMetadata b -> Permissions (DelPermDef b)
_tmDeletePermissions :: Permissions (DelPermDef b),
    TableMetadata b -> EventTriggers b
_tmEventTriggers :: EventTriggers b,
    TableMetadata b -> Maybe ApolloFederationConfig
_tmApolloFederationConfig :: Maybe ApolloFederationConfig
  }
  deriving ((forall x. TableMetadata b -> Rep (TableMetadata b) x)
-> (forall x. Rep (TableMetadata b) x -> TableMetadata b)
-> Generic (TableMetadata b)
forall x. Rep (TableMetadata b) x -> TableMetadata b
forall x. TableMetadata b -> Rep (TableMetadata b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) x.
Rep (TableMetadata b) x -> TableMetadata b
forall (b :: BackendType) x.
TableMetadata b -> Rep (TableMetadata b) x
$cto :: forall (b :: BackendType) x.
Rep (TableMetadata b) x -> TableMetadata b
$cfrom :: forall (b :: BackendType) x.
TableMetadata b -> Rep (TableMetadata b) x
Generic)

deriving instance (Backend b) => Show (TableMetadata b)

deriving instance (Backend b) => Eq (TableMetadata b)

instance (Backend b) => Cacheable (TableMetadata b)

instance (Backend b) => ToJSON (TableMetadata b) where
  toJSON :: TableMetadata b -> Value
toJSON = Options -> TableMetadata b -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
hasuraJSON

-- TODO: Write a proper codec for 'TableMetadata'
instance (Backend b) => HasCodec (TableMetadata b) where
  codec :: JSONCodec (TableMetadata b)
codec = Text -> JSONCodec (TableMetadata b) -> JSONCodec (TableMetadata b)
forall input output.
Text -> ValueCodec input output -> ValueCodec input output
named (HasTag b => Text
forall (b :: BackendType). HasTag b => Text
codecNamePrefix @b Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"TableMetadata") JSONCodec (TableMetadata b)
forall a. (FromJSON a, ToJSON a) => JSONCodec a
placeholderCodecViaJSON

codecNamePrefix :: forall b. (HasTag b) => Text
codecNamePrefix :: Text
codecNamePrefix = Text -> Text
T.toTitle (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ BackendType -> Text
forall a. ToTxt a => a -> Text
T.toTxt (BackendType -> Text) -> BackendType -> Text
forall a b. (a -> b) -> a -> b
$ BackendTag b -> BackendType
forall (b :: BackendType). BackendTag b -> BackendType
reify (BackendTag b -> BackendType) -> BackendTag b -> BackendType
forall a b. (a -> b) -> a -> b
$ HasTag b => BackendTag b
forall (b :: BackendType). HasTag b => BackendTag b
backendTag @b

$(makeLenses ''TableMetadata)

mkTableMeta :: TableName b -> Bool -> TableConfig b -> TableMetadata b
mkTableMeta :: TableName b -> Bool -> TableConfig b -> TableMetadata b
mkTableMeta TableName b
qt Bool
isEnum TableConfig b
config =
  TableName b
-> Bool
-> TableConfig b
-> Relationships (ObjRelDef b)
-> Relationships (ArrRelDef b)
-> ComputedFields b
-> RemoteRelationships
-> Permissions (InsPermDef b)
-> Permissions (SelPermDef b)
-> Permissions (UpdPermDef b)
-> Permissions (DelPermDef b)
-> EventTriggers b
-> Maybe ApolloFederationConfig
-> TableMetadata b
forall (b :: BackendType).
TableName b
-> Bool
-> TableConfig b
-> Relationships (ObjRelDef b)
-> Relationships (ArrRelDef b)
-> ComputedFields b
-> RemoteRelationships
-> Permissions (InsPermDef b)
-> Permissions (SelPermDef b)
-> Permissions (UpdPermDef b)
-> Permissions (DelPermDef b)
-> EventTriggers b
-> Maybe ApolloFederationConfig
-> TableMetadata b
TableMetadata
    TableName b
qt
    Bool
isEnum
    TableConfig b
config
    Relationships (ObjRelDef b)
forall a. Monoid a => a
mempty
    Relationships (ArrRelDef b)
forall a. Monoid a => a
mempty
    ComputedFields b
forall a. Monoid a => a
mempty
    RemoteRelationships
forall a. Monoid a => a
mempty
    Permissions (InsPermDef b)
forall a. Monoid a => a
mempty
    Permissions (SelPermDef b)
forall a. Monoid a => a
mempty
    Permissions (UpdPermDef b)
forall a. Monoid a => a
mempty
    Permissions (DelPermDef b)
forall a. Monoid a => a
mempty
    EventTriggers b
forall a. Monoid a => a
mempty
    Maybe ApolloFederationConfig
forall a. Maybe a
Nothing

instance (Backend b) => FromJSON (TableMetadata b) where
  parseJSON :: Value -> Parser (TableMetadata b)
parseJSON = String
-> (Object -> Parser (TableMetadata b))
-> Value
-> Parser (TableMetadata b)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Object" ((Object -> Parser (TableMetadata b))
 -> Value -> Parser (TableMetadata b))
-> (Object -> Parser (TableMetadata b))
-> Value
-> Parser (TableMetadata b)
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
    let unexpectedKeys :: HashSet Key
unexpectedKeys = Object -> HashSet Key
getUnexpectedKeys Object
o
    Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (HashSet Key -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null HashSet Key
unexpectedKeys) (Parser () -> Parser ()) -> Parser () -> Parser ()
forall a b. (a -> b) -> a -> b
$
      String -> Parser ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser ()) -> String -> Parser ()
forall a b. (a -> b) -> a -> b
$
        String
"unexpected keys when parsing TableMetadata : "
          String -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Key] -> String
forall a. Show a => a -> String
show (HashSet Key -> [Key]
forall a. HashSet a -> [a]
HS.toList HashSet Key
unexpectedKeys)

    TableName b
-> Bool
-> TableConfig b
-> Relationships (ObjRelDef b)
-> Relationships (ArrRelDef b)
-> ComputedFields b
-> RemoteRelationships
-> Permissions (InsPermDef b)
-> Permissions (SelPermDef b)
-> Permissions (UpdPermDef b)
-> Permissions (DelPermDef b)
-> EventTriggers b
-> Maybe ApolloFederationConfig
-> TableMetadata b
forall (b :: BackendType).
TableName b
-> Bool
-> TableConfig b
-> Relationships (ObjRelDef b)
-> Relationships (ArrRelDef b)
-> ComputedFields b
-> RemoteRelationships
-> Permissions (InsPermDef b)
-> Permissions (SelPermDef b)
-> Permissions (UpdPermDef b)
-> Permissions (DelPermDef b)
-> EventTriggers b
-> Maybe ApolloFederationConfig
-> TableMetadata b
TableMetadata
      (TableName b
 -> Bool
 -> TableConfig b
 -> Relationships (ObjRelDef b)
 -> Relationships (ArrRelDef b)
 -> ComputedFields b
 -> RemoteRelationships
 -> Permissions (InsPermDef b)
 -> Permissions (SelPermDef b)
 -> Permissions (UpdPermDef b)
 -> Permissions (DelPermDef b)
 -> EventTriggers b
 -> Maybe ApolloFederationConfig
 -> TableMetadata b)
-> Parser (TableName b)
-> Parser
     (Bool
      -> TableConfig b
      -> Relationships (ObjRelDef b)
      -> Relationships (ArrRelDef b)
      -> ComputedFields b
      -> RemoteRelationships
      -> Permissions (InsPermDef b)
      -> Permissions (SelPermDef b)
      -> Permissions (UpdPermDef b)
      -> Permissions (DelPermDef b)
      -> EventTriggers b
      -> Maybe ApolloFederationConfig
      -> TableMetadata b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (TableName b)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
tableKey
      Parser
  (Bool
   -> TableConfig b
   -> Relationships (ObjRelDef b)
   -> Relationships (ArrRelDef b)
   -> ComputedFields b
   -> RemoteRelationships
   -> Permissions (InsPermDef b)
   -> Permissions (SelPermDef b)
   -> Permissions (UpdPermDef b)
   -> Permissions (DelPermDef b)
   -> EventTriggers b
   -> Maybe ApolloFederationConfig
   -> TableMetadata b)
-> Parser Bool
-> Parser
     (TableConfig b
      -> Relationships (ObjRelDef b)
      -> Relationships (ArrRelDef b)
      -> ComputedFields b
      -> RemoteRelationships
      -> Permissions (InsPermDef b)
      -> Permissions (SelPermDef b)
      -> Permissions (UpdPermDef b)
      -> Permissions (DelPermDef b)
      -> EventTriggers b
      -> Maybe ApolloFederationConfig
      -> TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
isEnumKey Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
False
      Parser
  (TableConfig b
   -> Relationships (ObjRelDef b)
   -> Relationships (ArrRelDef b)
   -> ComputedFields b
   -> RemoteRelationships
   -> Permissions (InsPermDef b)
   -> Permissions (SelPermDef b)
   -> Permissions (UpdPermDef b)
   -> Permissions (DelPermDef b)
   -> EventTriggers b
   -> Maybe ApolloFederationConfig
   -> TableMetadata b)
-> Parser (TableConfig b)
-> Parser
     (Relationships (ObjRelDef b)
      -> Relationships (ArrRelDef b)
      -> ComputedFields b
      -> RemoteRelationships
      -> Permissions (InsPermDef b)
      -> Permissions (SelPermDef b)
      -> Permissions (UpdPermDef b)
      -> Permissions (DelPermDef b)
      -> EventTriggers b
      -> Maybe ApolloFederationConfig
      -> TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe (TableConfig b))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
configKey Parser (Maybe (TableConfig b))
-> TableConfig b -> Parser (TableConfig b)
forall a. Parser (Maybe a) -> a -> Parser a
.!= TableConfig b
forall (b :: BackendType). TableConfig b
emptyTableConfig
      Parser
  (Relationships (ObjRelDef b)
   -> Relationships (ArrRelDef b)
   -> ComputedFields b
   -> RemoteRelationships
   -> Permissions (InsPermDef b)
   -> Permissions (SelPermDef b)
   -> Permissions (UpdPermDef b)
   -> Permissions (DelPermDef b)
   -> EventTriggers b
   -> Maybe ApolloFederationConfig
   -> TableMetadata b)
-> Parser (Relationships (ObjRelDef b))
-> Parser
     (Relationships (ArrRelDef b)
      -> ComputedFields b
      -> RemoteRelationships
      -> Permissions (InsPermDef b)
      -> Permissions (SelPermDef b)
      -> Permissions (UpdPermDef b)
      -> Permissions (DelPermDef b)
      -> EventTriggers b
      -> Maybe ApolloFederationConfig
      -> TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> (ObjRelDef b -> RelName)
-> Parser [ObjRelDef b]
-> Parser (Relationships (ObjRelDef b))
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"object relationships" ObjRelDef b -> RelName
forall a. RelDef a -> RelName
_rdName (Object
o Object -> Key -> Parser (Maybe [ObjRelDef b])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
orKey Parser (Maybe [ObjRelDef b])
-> [ObjRelDef b] -> Parser [ObjRelDef b]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [])
      Parser
  (Relationships (ArrRelDef b)
   -> ComputedFields b
   -> RemoteRelationships
   -> Permissions (InsPermDef b)
   -> Permissions (SelPermDef b)
   -> Permissions (UpdPermDef b)
   -> Permissions (DelPermDef b)
   -> EventTriggers b
   -> Maybe ApolloFederationConfig
   -> TableMetadata b)
-> Parser (Relationships (ArrRelDef b))
-> Parser
     (ComputedFields b
      -> RemoteRelationships
      -> Permissions (InsPermDef b)
      -> Permissions (SelPermDef b)
      -> Permissions (UpdPermDef b)
      -> Permissions (DelPermDef b)
      -> EventTriggers b
      -> Maybe ApolloFederationConfig
      -> TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> (ArrRelDef b -> RelName)
-> Parser [ArrRelDef b]
-> Parser (Relationships (ArrRelDef b))
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"array relationships" ArrRelDef b -> RelName
forall a. RelDef a -> RelName
_rdName (Object
o Object -> Key -> Parser (Maybe [ArrRelDef b])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
arKey Parser (Maybe [ArrRelDef b])
-> [ArrRelDef b] -> Parser [ArrRelDef b]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [])
      Parser
  (ComputedFields b
   -> RemoteRelationships
   -> Permissions (InsPermDef b)
   -> Permissions (SelPermDef b)
   -> Permissions (UpdPermDef b)
   -> Permissions (DelPermDef b)
   -> EventTriggers b
   -> Maybe ApolloFederationConfig
   -> TableMetadata b)
-> Parser (ComputedFields b)
-> Parser
     (RemoteRelationships
      -> Permissions (InsPermDef b)
      -> Permissions (SelPermDef b)
      -> Permissions (UpdPermDef b)
      -> Permissions (DelPermDef b)
      -> EventTriggers b
      -> Maybe ApolloFederationConfig
      -> TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> (ComputedFieldMetadata b -> ComputedFieldName)
-> Parser [ComputedFieldMetadata b]
-> Parser (ComputedFields b)
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"computed fields" ComputedFieldMetadata b -> ComputedFieldName
forall (b :: BackendType).
ComputedFieldMetadata b -> ComputedFieldName
_cfmName (Object
o Object -> Key -> Parser (Maybe [ComputedFieldMetadata b])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
cfKey Parser (Maybe [ComputedFieldMetadata b])
-> [ComputedFieldMetadata b] -> Parser [ComputedFieldMetadata b]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [])
      Parser
  (RemoteRelationships
   -> Permissions (InsPermDef b)
   -> Permissions (SelPermDef b)
   -> Permissions (UpdPermDef b)
   -> Permissions (DelPermDef b)
   -> EventTriggers b
   -> Maybe ApolloFederationConfig
   -> TableMetadata b)
-> Parser RemoteRelationships
-> Parser
     (Permissions (InsPermDef b)
      -> Permissions (SelPermDef b)
      -> Permissions (UpdPermDef b)
      -> Permissions (DelPermDef b)
      -> EventTriggers b
      -> Maybe ApolloFederationConfig
      -> TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> (RemoteRelationship -> RelName)
-> Parser [RemoteRelationship]
-> Parser RemoteRelationships
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"remote relationships" RemoteRelationship -> RelName
_rrName (Object
o Object -> Key -> Parser (Maybe [RemoteRelationship])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
rrKey Parser (Maybe [RemoteRelationship])
-> [RemoteRelationship] -> Parser [RemoteRelationship]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [])
      Parser
  (Permissions (InsPermDef b)
   -> Permissions (SelPermDef b)
   -> Permissions (UpdPermDef b)
   -> Permissions (DelPermDef b)
   -> EventTriggers b
   -> Maybe ApolloFederationConfig
   -> TableMetadata b)
-> Parser (Permissions (InsPermDef b))
-> Parser
     (Permissions (SelPermDef b)
      -> Permissions (UpdPermDef b)
      -> Permissions (DelPermDef b)
      -> EventTriggers b
      -> Maybe ApolloFederationConfig
      -> TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> (InsPermDef b -> RoleName)
-> Parser [InsPermDef b]
-> Parser (Permissions (InsPermDef b))
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"insert permissions" InsPermDef b -> RoleName
forall (b :: BackendType) (perm :: BackendType -> *).
PermDef b perm -> RoleName
_pdRole (Object
o Object -> Key -> Parser (Maybe [InsPermDef b])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
ipKey Parser (Maybe [InsPermDef b])
-> [InsPermDef b] -> Parser [InsPermDef b]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [])
      Parser
  (Permissions (SelPermDef b)
   -> Permissions (UpdPermDef b)
   -> Permissions (DelPermDef b)
   -> EventTriggers b
   -> Maybe ApolloFederationConfig
   -> TableMetadata b)
-> Parser (Permissions (SelPermDef b))
-> Parser
     (Permissions (UpdPermDef b)
      -> Permissions (DelPermDef b)
      -> EventTriggers b
      -> Maybe ApolloFederationConfig
      -> TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> (SelPermDef b -> RoleName)
-> Parser [SelPermDef b]
-> Parser (Permissions (SelPermDef b))
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"select permissions" SelPermDef b -> RoleName
forall (b :: BackendType) (perm :: BackendType -> *).
PermDef b perm -> RoleName
_pdRole (Object
o Object -> Key -> Parser (Maybe [SelPermDef b])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
spKey Parser (Maybe [SelPermDef b])
-> [SelPermDef b] -> Parser [SelPermDef b]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [])
      Parser
  (Permissions (UpdPermDef b)
   -> Permissions (DelPermDef b)
   -> EventTriggers b
   -> Maybe ApolloFederationConfig
   -> TableMetadata b)
-> Parser (Permissions (UpdPermDef b))
-> Parser
     (Permissions (DelPermDef b)
      -> EventTriggers b
      -> Maybe ApolloFederationConfig
      -> TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> (UpdPermDef b -> RoleName)
-> Parser [UpdPermDef b]
-> Parser (Permissions (UpdPermDef b))
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"update permissions" UpdPermDef b -> RoleName
forall (b :: BackendType) (perm :: BackendType -> *).
PermDef b perm -> RoleName
_pdRole (Object
o Object -> Key -> Parser (Maybe [UpdPermDef b])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
upKey Parser (Maybe [UpdPermDef b])
-> [UpdPermDef b] -> Parser [UpdPermDef b]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [])
      Parser
  (Permissions (DelPermDef b)
   -> EventTriggers b
   -> Maybe ApolloFederationConfig
   -> TableMetadata b)
-> Parser (Permissions (DelPermDef b))
-> Parser
     (EventTriggers b
      -> Maybe ApolloFederationConfig -> TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> (DelPermDef b -> RoleName)
-> Parser [DelPermDef b]
-> Parser (Permissions (DelPermDef b))
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"delete permissions" DelPermDef b -> RoleName
forall (b :: BackendType) (perm :: BackendType -> *).
PermDef b perm -> RoleName
_pdRole (Object
o Object -> Key -> Parser (Maybe [DelPermDef b])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
dpKey Parser (Maybe [DelPermDef b])
-> [DelPermDef b] -> Parser [DelPermDef b]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [])
      Parser
  (EventTriggers b
   -> Maybe ApolloFederationConfig -> TableMetadata b)
-> Parser (EventTriggers b)
-> Parser (Maybe ApolloFederationConfig -> TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> (EventTriggerConf b -> TriggerName)
-> Parser [EventTriggerConf b]
-> Parser (EventTriggers b)
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"event triggers" EventTriggerConf b -> TriggerName
forall (b :: BackendType). EventTriggerConf b -> TriggerName
etcName (Object
o Object -> Key -> Parser (Maybe [EventTriggerConf b])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
etKey Parser (Maybe [EventTriggerConf b])
-> [EventTriggerConf b] -> Parser [EventTriggerConf b]
forall a. Parser (Maybe a) -> a -> Parser a
.!= [])
      Parser (Maybe ApolloFederationConfig -> TableMetadata b)
-> Parser (Maybe ApolloFederationConfig)
-> Parser (TableMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe ApolloFederationConfig)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
enableAFKey
    where
      tableKey :: Key
tableKey = Key
"table"
      isEnumKey :: Key
isEnumKey = Key
"is_enum"
      configKey :: Key
configKey = Key
"configuration"
      orKey :: Key
orKey = Key
"object_relationships"
      arKey :: Key
arKey = Key
"array_relationships"
      ipKey :: Key
ipKey = Key
"insert_permissions"
      spKey :: Key
spKey = Key
"select_permissions"
      upKey :: Key
upKey = Key
"update_permissions"
      dpKey :: Key
dpKey = Key
"delete_permissions"
      etKey :: Key
etKey = Key
"event_triggers"
      cfKey :: Key
cfKey = Key
"computed_fields"
      rrKey :: Key
rrKey = Key
"remote_relationships"
      enableAFKey :: Key
enableAFKey = Key
"apollo_federation_config"

      getUnexpectedKeys :: Object -> HashSet Key
getUnexpectedKeys Object
o =
        [Key] -> HashSet Key
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HS.fromList (Object -> [Key]
forall v. KeyMap v -> [Key]
KM.keys Object
o) HashSet Key -> HashSet Key -> HashSet Key
forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
`HS.difference` HashSet Key
expectedKeySet

      expectedKeySet :: HashSet Key
expectedKeySet =
        [Key] -> HashSet Key
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HS.fromList
          [ Key
tableKey,
            Key
isEnumKey,
            Key
configKey,
            Key
orKey,
            Key
arKey,
            Key
ipKey,
            Key
spKey,
            Key
upKey,
            Key
dpKey,
            Key
etKey,
            Key
cfKey,
            Key
rrKey,
            Key
enableAFKey
          ]

data FunctionMetadata b = FunctionMetadata
  { FunctionMetadata b -> FunctionName b
_fmFunction :: FunctionName b,
    FunctionMetadata b -> FunctionConfig
_fmConfiguration :: FunctionConfig,
    FunctionMetadata b -> [FunctionPermissionInfo]
_fmPermissions :: [FunctionPermissionInfo],
    FunctionMetadata b -> Maybe Text
_fmComment :: Maybe Text
  }
  deriving ((forall x. FunctionMetadata b -> Rep (FunctionMetadata b) x)
-> (forall x. Rep (FunctionMetadata b) x -> FunctionMetadata b)
-> Generic (FunctionMetadata b)
forall x. Rep (FunctionMetadata b) x -> FunctionMetadata b
forall x. FunctionMetadata b -> Rep (FunctionMetadata b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) x.
Rep (FunctionMetadata b) x -> FunctionMetadata b
forall (b :: BackendType) x.
FunctionMetadata b -> Rep (FunctionMetadata b) x
$cto :: forall (b :: BackendType) x.
Rep (FunctionMetadata b) x -> FunctionMetadata b
$cfrom :: forall (b :: BackendType) x.
FunctionMetadata b -> Rep (FunctionMetadata b) x
Generic)

deriving instance (Backend b) => Show (FunctionMetadata b)

deriving instance (Backend b) => Eq (FunctionMetadata b)

instance (Backend b) => Cacheable (FunctionMetadata b)

instance (Backend b) => ToJSON (FunctionMetadata b) where
  toJSON :: FunctionMetadata b -> Value
toJSON = Options -> FunctionMetadata b -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
hasuraJSON

$(makeLenses ''FunctionMetadata)

instance (Backend b) => FromJSON (FunctionMetadata b) where
  parseJSON :: Value -> Parser (FunctionMetadata b)
parseJSON = String
-> (Object -> Parser (FunctionMetadata b))
-> Value
-> Parser (FunctionMetadata b)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"FunctionMetadata" ((Object -> Parser (FunctionMetadata b))
 -> Value -> Parser (FunctionMetadata b))
-> (Object -> Parser (FunctionMetadata b))
-> Value
-> Parser (FunctionMetadata b)
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    FunctionName b
-> FunctionConfig
-> [FunctionPermissionInfo]
-> Maybe Text
-> FunctionMetadata b
forall (b :: BackendType).
FunctionName b
-> FunctionConfig
-> [FunctionPermissionInfo]
-> Maybe Text
-> FunctionMetadata b
FunctionMetadata
      (FunctionName b
 -> FunctionConfig
 -> [FunctionPermissionInfo]
 -> Maybe Text
 -> FunctionMetadata b)
-> Parser (FunctionName b)
-> Parser
     (FunctionConfig
      -> [FunctionPermissionInfo] -> Maybe Text -> FunctionMetadata b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (FunctionName b)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"function"
      Parser
  (FunctionConfig
   -> [FunctionPermissionInfo] -> Maybe Text -> FunctionMetadata b)
-> Parser FunctionConfig
-> Parser
     ([FunctionPermissionInfo] -> Maybe Text -> FunctionMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe FunctionConfig)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"configuration" Parser (Maybe FunctionConfig)
-> FunctionConfig -> Parser FunctionConfig
forall a. Parser (Maybe a) -> a -> Parser a
.!= FunctionConfig
emptyFunctionConfig
      Parser
  ([FunctionPermissionInfo] -> Maybe Text -> FunctionMetadata b)
-> Parser [FunctionPermissionInfo]
-> Parser (Maybe Text -> FunctionMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe [FunctionPermissionInfo])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"permissions" Parser (Maybe [FunctionPermissionInfo])
-> [FunctionPermissionInfo] -> Parser [FunctionPermissionInfo]
forall a. Parser (Maybe a) -> a -> Parser a
.!= []
      Parser (Maybe Text -> FunctionMetadata b)
-> Parser (Maybe Text) -> Parser (FunctionMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"comment"

-- TODO: Write a proper codec for 'FunctionMetadata'
instance (Backend b) => HasCodec (FunctionMetadata b) where
  codec :: JSONCodec (FunctionMetadata b)
codec = Text
-> JSONCodec (FunctionMetadata b) -> JSONCodec (FunctionMetadata b)
forall input output.
Text -> ValueCodec input output -> ValueCodec input output
named (HasTag b => Text
forall (b :: BackendType). HasTag b => Text
codecNamePrefix @b Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"FunctionMetadata") (JSONCodec (FunctionMetadata b) -> JSONCodec (FunctionMetadata b))
-> JSONCodec (FunctionMetadata b) -> JSONCodec (FunctionMetadata b)
forall a b. (a -> b) -> a -> b
$ JSONCodec (FunctionMetadata b)
forall a. (FromJSON a, ToJSON a) => JSONCodec a
placeholderCodecViaJSON

type Tables b = InsOrdHashMap (TableName b) (TableMetadata b)

type Functions b = InsOrdHashMap (FunctionName b) (FunctionMetadata b)

type RemoteSchemas = InsOrdHashMap RemoteSchemaName RemoteSchemaMetadata

type Endpoints = InsOrdHashMap EndpointName CreateEndpoint

type Actions = InsOrdHashMap ActionName ActionMetadata

type CronTriggers = InsOrdHashMap TriggerName CronTriggerMetadata

type InheritedRoles = InsOrdHashMap RoleName InheritedRole

-- | Source configuration for a source of backend type @b@ as stored in the Metadata DB.
data SourceMetadata b = SourceMetadata
  { SourceMetadata b -> SourceName
_smName :: SourceName,
    SourceMetadata b -> BackendSourceKind b
_smKind :: BackendSourceKind b,
    SourceMetadata b -> Tables b
_smTables :: Tables b,
    SourceMetadata b -> Functions b
_smFunctions :: Functions b,
    SourceMetadata b -> SourceConnConfiguration b
_smConfiguration :: SourceConnConfiguration b,
    SourceMetadata b -> Maybe QueryTagsConfig
_smQueryTags :: Maybe QueryTagsConfig,
    SourceMetadata b -> SourceCustomization
_smCustomization :: SourceCustomization
  }
  deriving ((forall x. SourceMetadata b -> Rep (SourceMetadata b) x)
-> (forall x. Rep (SourceMetadata b) x -> SourceMetadata b)
-> Generic (SourceMetadata b)
forall x. Rep (SourceMetadata b) x -> SourceMetadata b
forall x. SourceMetadata b -> Rep (SourceMetadata b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) x.
Rep (SourceMetadata b) x -> SourceMetadata b
forall (b :: BackendType) x.
SourceMetadata b -> Rep (SourceMetadata b) x
$cto :: forall (b :: BackendType) x.
Rep (SourceMetadata b) x -> SourceMetadata b
$cfrom :: forall (b :: BackendType) x.
SourceMetadata b -> Rep (SourceMetadata b) x
Generic)

$(makeLenses ''SourceMetadata)

deriving instance (Backend b) => Show (SourceMetadata b)

deriving instance (Backend b) => Eq (SourceMetadata b)

instance (Backend b) => Cacheable (SourceMetadata b)

instance (Backend b) => FromJSONWithContext (BackendSourceKind b) (SourceMetadata b) where
  parseJSONWithContext :: BackendSourceKind b -> Value -> Parser (SourceMetadata b)
parseJSONWithContext BackendSourceKind b
_smKind = String
-> (Object -> Parser (SourceMetadata b))
-> Value
-> Parser (SourceMetadata b)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Object" ((Object -> Parser (SourceMetadata b))
 -> Value -> Parser (SourceMetadata b))
-> (Object -> Parser (SourceMetadata b))
-> Value
-> Parser (SourceMetadata b)
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
    SourceName
_smName <- Object
o Object -> Key -> Parser SourceName
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
    InsOrdHashMap (TableName b) (TableMetadata b)
_smTables <- (TableMetadata b -> TableName b)
-> [TableMetadata b]
-> InsOrdHashMap (TableName b) (TableMetadata b)
forall k a.
(Eq k, Hashable k) =>
(a -> k) -> [a] -> InsOrdHashMap k a
oMapFromL TableMetadata b -> TableName b
forall (b :: BackendType). TableMetadata b -> TableName b
_tmTable ([TableMetadata b]
 -> InsOrdHashMap (TableName b) (TableMetadata b))
-> Parser [TableMetadata b]
-> Parser (InsOrdHashMap (TableName b) (TableMetadata b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser [TableMetadata b]
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"tables"
    InsOrdHashMap (FunctionName b) (FunctionMetadata b)
_smFunctions <- (FunctionMetadata b -> FunctionName b)
-> [FunctionMetadata b]
-> InsOrdHashMap (FunctionName b) (FunctionMetadata b)
forall k a.
(Eq k, Hashable k) =>
(a -> k) -> [a] -> InsOrdHashMap k a
oMapFromL FunctionMetadata b -> FunctionName b
forall (b :: BackendType). FunctionMetadata b -> FunctionName b
_fmFunction ([FunctionMetadata b]
 -> InsOrdHashMap (FunctionName b) (FunctionMetadata b))
-> Parser [FunctionMetadata b]
-> Parser (InsOrdHashMap (FunctionName b) (FunctionMetadata b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe [FunctionMetadata b])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"functions" Parser (Maybe [FunctionMetadata b])
-> [FunctionMetadata b] -> Parser [FunctionMetadata b]
forall a. Parser (Maybe a) -> a -> Parser a
.!= []
    SourceConnConfiguration b
_smConfiguration <- Object
o Object -> Key -> Parser (SourceConnConfiguration b)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"configuration"
    Maybe QueryTagsConfig
_smQueryTags <- Object
o Object -> Key -> Parser (Maybe QueryTagsConfig)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"query_tags"
    SourceCustomization
_smCustomization <- Object
o Object -> Key -> Parser (Maybe SourceCustomization)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"customization" Parser (Maybe SourceCustomization)
-> SourceCustomization -> Parser SourceCustomization
forall a. Parser (Maybe a) -> a -> Parser a
.!= SourceCustomization
emptySourceCustomization
    SourceMetadata b -> Parser (SourceMetadata b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure SourceMetadata :: forall (b :: BackendType).
SourceName
-> BackendSourceKind b
-> Tables b
-> Functions b
-> SourceConnConfiguration b
-> Maybe QueryTagsConfig
-> SourceCustomization
-> SourceMetadata b
SourceMetadata {Maybe QueryTagsConfig
InsOrdHashMap (TableName b) (TableMetadata b)
InsOrdHashMap (FunctionName b) (FunctionMetadata b)
SourceName
BackendSourceKind b
SourceConnConfiguration b
SourceCustomization
_smCustomization :: SourceCustomization
_smQueryTags :: Maybe QueryTagsConfig
_smConfiguration :: SourceConnConfiguration b
_smFunctions :: InsOrdHashMap (FunctionName b) (FunctionMetadata b)
_smTables :: InsOrdHashMap (TableName b) (TableMetadata b)
_smName :: SourceName
_smKind :: BackendSourceKind b
_smCustomization :: SourceCustomization
_smQueryTags :: Maybe QueryTagsConfig
_smConfiguration :: SourceConnConfiguration b
_smFunctions :: InsOrdHashMap (FunctionName b) (FunctionMetadata b)
_smTables :: InsOrdHashMap (TableName b) (TableMetadata b)
_smKind :: BackendSourceKind b
_smName :: SourceName
..}

backendSourceMetadataCodec :: JSONCodec BackendSourceMetadata
backendSourceMetadataCodec :: JSONCodec BackendSourceMetadata
backendSourceMetadataCodec =
  Text
-> JSONCodec BackendSourceMetadata
-> JSONCodec BackendSourceMetadata
forall input output.
Text -> ValueCodec input output -> ValueCodec input output
named Text
"SourceMetadata" (JSONCodec BackendSourceMetadata
 -> JSONCodec BackendSourceMetadata)
-> JSONCodec BackendSourceMetadata
-> JSONCodec BackendSourceMetadata
forall a b. (a -> b) -> a -> b
$
    -- Attempt to match against @SourceMetadata@ codecs for each native backend
    -- type. If none match then apply the @SourceMetadata DataConnector@ codec.
    -- DataConnector is the fallback case because the possible values for its
    -- @_smKind@ property are not statically-known so it is difficult to
    -- unambiguously distinguish a native source value from a dataconnector
    -- source.
    [(BackendSourceMetadata -> Maybe BackendSourceMetadata,
  JSONCodec BackendSourceMetadata)]
-> JSONCodec BackendSourceMetadata
-> JSONCodec BackendSourceMetadata
forall input context output.
[(input -> Maybe input, Codec context input output)]
-> Codec context input output -> Codec context input output
disjointMatchChoicesCodec
      (BackendType
-> (BackendSourceMetadata -> Maybe BackendSourceMetadata,
    JSONCodec BackendSourceMetadata)
matcherWithBackendCodec (BackendType
 -> (BackendSourceMetadata -> Maybe BackendSourceMetadata,
     JSONCodec BackendSourceMetadata))
-> [BackendType]
-> [(BackendSourceMetadata -> Maybe BackendSourceMetadata,
     JSONCodec BackendSourceMetadata)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (BackendType -> Bool) -> [BackendType] -> [BackendType]
forall a. (a -> Bool) -> [a] -> [a]
filter (BackendType -> BackendType -> Bool
forall a. Eq a => a -> a -> Bool
/= BackendType
DataConnector) [BackendType]
supportedBackends) -- list of codecs to try
      (BackendTag 'DataConnector -> JSONCodec BackendSourceMetadata
forall (b :: BackendType).
Backend b =>
BackendTag b -> JSONCodec BackendSourceMetadata
mkCodec (HasTag 'DataConnector => BackendTag 'DataConnector
forall (b :: BackendType). HasTag b => BackendTag b
backendTag @('DataConnector))) -- codec for fallback case
  where
    matcherWithBackendCodec :: BackendType -> (BackendSourceMetadata -> Maybe BackendSourceMetadata, JSONCodec BackendSourceMetadata)
    matcherWithBackendCodec :: BackendType
-> (BackendSourceMetadata -> Maybe BackendSourceMetadata,
    JSONCodec BackendSourceMetadata)
matcherWithBackendCodec BackendType
backendType =
      (BackendType -> BackendSourceMetadata -> Maybe BackendSourceMetadata
matches BackendType
backendType, AnyBackend BackendTag
-> (forall (b :: BackendType).
    Backend b =>
    BackendTag b -> JSONCodec BackendSourceMetadata)
-> JSONCodec BackendSourceMetadata
forall (c :: BackendType -> Constraint) (i :: BackendType -> *) r.
AllBackendsSatisfy c =>
AnyBackend i -> (forall (b :: BackendType). c b => i b -> r) -> r
AB.dispatchAnyBackend @Backend (BackendType -> AnyBackend BackendTag
AB.liftTag BackendType
backendType) forall (b :: BackendType).
Backend b =>
BackendTag b -> JSONCodec BackendSourceMetadata
mkCodec)

    mkCodec :: forall b. Backend b => (BackendTag b) -> JSONCodec BackendSourceMetadata
    mkCodec :: BackendTag b -> JSONCodec BackendSourceMetadata
mkCodec BackendTag b
_ = JSONCodec (SourceMetadata b) -> JSONCodec BackendSourceMetadata
forall (b :: BackendType).
HasTag b =>
JSONCodec (SourceMetadata b) -> JSONCodec BackendSourceMetadata
anySourceMetadataCodec (JSONCodec (SourceMetadata b) -> JSONCodec BackendSourceMetadata)
-> JSONCodec (SourceMetadata b) -> JSONCodec BackendSourceMetadata
forall a b. (a -> b) -> a -> b
$ HasCodec (SourceMetadata b) => JSONCodec (SourceMetadata b)
forall value. HasCodec value => JSONCodec value
codec @(SourceMetadata b)

    matches :: BackendType -> BackendSourceMetadata -> Maybe BackendSourceMetadata
    matches :: BackendType -> BackendSourceMetadata -> Maybe BackendSourceMetadata
matches BackendType
backendType BackendSourceMetadata
input =
      if BackendSourceMetadata -> BackendType
runBackendType BackendSourceMetadata
input BackendType -> BackendType -> Bool
forall a. Eq a => a -> a -> Bool
== BackendType
backendType
        then BackendSourceMetadata -> Maybe BackendSourceMetadata
forall a. a -> Maybe a
Just BackendSourceMetadata
input
        else Maybe BackendSourceMetadata
forall a. Maybe a
Nothing

    runBackendType :: BackendSourceMetadata -> BackendType
    runBackendType :: BackendSourceMetadata -> BackendType
runBackendType (BackendSourceMetadata AnyBackend SourceMetadata
input) = AnyBackend SourceMetadata
-> (forall (b :: BackendType). SourceMetadata b -> BackendType)
-> BackendType
forall (i :: BackendType -> *) r.
AnyBackend i -> (forall (b :: BackendType). i b -> r) -> r
AB.runBackend AnyBackend SourceMetadata
input \SourceMetadata b
sourceMeta ->
      BackendSourceKind b -> BackendType
forall (b :: BackendType). BackendSourceKind b -> BackendType
backendTypeFromBackendSourceKind (BackendSourceKind b -> BackendType)
-> BackendSourceKind b -> BackendType
forall a b. (a -> b) -> a -> b
$ SourceMetadata b -> BackendSourceKind b
forall (b :: BackendType). SourceMetadata b -> BackendSourceKind b
_smKind SourceMetadata b
sourceMeta

anySourceMetadataCodec :: (HasTag b) => JSONCodec (SourceMetadata b) -> JSONCodec BackendSourceMetadata
anySourceMetadataCodec :: JSONCodec (SourceMetadata b) -> JSONCodec BackendSourceMetadata
anySourceMetadataCodec = (SourceMetadata b -> BackendSourceMetadata)
-> (BackendSourceMetadata -> SourceMetadata b)
-> JSONCodec (SourceMetadata b)
-> JSONCodec BackendSourceMetadata
forall oldOutput newOutput newInput oldInput context.
(oldOutput -> newOutput)
-> (newInput -> oldInput)
-> Codec context oldInput oldOutput
-> Codec context newInput newOutput
dimapCodec SourceMetadata b -> BackendSourceMetadata
forall (b :: BackendType).
HasTag b =>
SourceMetadata b -> BackendSourceMetadata
dec BackendSourceMetadata -> SourceMetadata b
forall (b :: BackendType).
HasTag b =>
BackendSourceMetadata -> SourceMetadata b
enc
  where
    dec :: HasTag b => SourceMetadata b -> BackendSourceMetadata
    dec :: SourceMetadata b -> BackendSourceMetadata
dec = AnyBackend SourceMetadata -> BackendSourceMetadata
BackendSourceMetadata (AnyBackend SourceMetadata -> BackendSourceMetadata)
-> (SourceMetadata b -> AnyBackend SourceMetadata)
-> SourceMetadata b
-> BackendSourceMetadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceMetadata b -> AnyBackend SourceMetadata
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
AB.mkAnyBackend

    -- This encoding function is partial, but that should be ok.
    enc :: HasTag b => BackendSourceMetadata -> SourceMetadata b
    enc :: BackendSourceMetadata -> SourceMetadata b
enc BackendSourceMetadata
input = Maybe (SourceMetadata b) -> SourceMetadata b
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe (SourceMetadata b) -> SourceMetadata b)
-> Maybe (SourceMetadata b) -> SourceMetadata b
forall a b. (a -> b) -> a -> b
$ AnyBackend SourceMetadata -> Maybe (SourceMetadata b)
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
AnyBackend i -> Maybe (i b)
AB.unpackAnyBackend (AnyBackend SourceMetadata -> Maybe (SourceMetadata b))
-> AnyBackend SourceMetadata -> Maybe (SourceMetadata b)
forall a b. (a -> b) -> a -> b
$ BackendSourceMetadata -> AnyBackend SourceMetadata
unBackendSourceMetadata BackendSourceMetadata
input

instance Backend b => HasCodec (SourceMetadata b) where
  codec :: JSONCodec (SourceMetadata b)
codec =
    Text
-> ObjectCodec (SourceMetadata b) (SourceMetadata b)
-> JSONCodec (SourceMetadata b)
forall input output.
Text -> ObjectCodec input output -> ValueCodec input output
AC.object (HasTag b => Text
forall (b :: BackendType). HasTag b => Text
codecNamePrefix @b Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"SourceMetadata") (ObjectCodec (SourceMetadata b) (SourceMetadata b)
 -> JSONCodec (SourceMetadata b))
-> ObjectCodec (SourceMetadata b) (SourceMetadata b)
-> JSONCodec (SourceMetadata b)
forall a b. (a -> b) -> a -> b
$
      SourceName
-> BackendSourceKind b
-> InsOrdHashMap (TableName b) (TableMetadata b)
-> InsOrdHashMap (FunctionName b) (FunctionMetadata b)
-> SourceConnConfiguration b
-> Maybe QueryTagsConfig
-> SourceCustomization
-> SourceMetadata b
forall (b :: BackendType).
SourceName
-> BackendSourceKind b
-> Tables b
-> Functions b
-> SourceConnConfiguration b
-> Maybe QueryTagsConfig
-> SourceCustomization
-> SourceMetadata b
SourceMetadata
        (SourceName
 -> BackendSourceKind b
 -> InsOrdHashMap (TableName b) (TableMetadata b)
 -> InsOrdHashMap (FunctionName b) (FunctionMetadata b)
 -> SourceConnConfiguration b
 -> Maybe QueryTagsConfig
 -> SourceCustomization
 -> SourceMetadata b)
-> Codec Object (SourceMetadata b) SourceName
-> Codec
     Object
     (SourceMetadata b)
     (BackendSourceKind b
      -> InsOrdHashMap (TableName b) (TableMetadata b)
      -> InsOrdHashMap (FunctionName b) (FunctionMetadata b)
      -> SourceConnConfiguration b
      -> Maybe QueryTagsConfig
      -> SourceCustomization
      -> SourceMetadata b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> ObjectCodec SourceName SourceName
forall output. HasCodec output => Text -> ObjectCodec output output
requiredField' Text
"name" ObjectCodec SourceName SourceName
-> (SourceMetadata b -> SourceName)
-> Codec Object (SourceMetadata b) SourceName
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.== SourceMetadata b -> SourceName
forall (b :: BackendType). SourceMetadata b -> SourceName
_smName
        Codec
  Object
  (SourceMetadata b)
  (BackendSourceKind b
   -> InsOrdHashMap (TableName b) (TableMetadata b)
   -> InsOrdHashMap (FunctionName b) (FunctionMetadata b)
   -> SourceConnConfiguration b
   -> Maybe QueryTagsConfig
   -> SourceCustomization
   -> SourceMetadata b)
-> Codec Object (SourceMetadata b) (BackendSourceKind b)
-> Codec
     Object
     (SourceMetadata b)
     (InsOrdHashMap (TableName b) (TableMetadata b)
      -> InsOrdHashMap (FunctionName b) (FunctionMetadata b)
      -> SourceConnConfiguration b
      -> Maybe QueryTagsConfig
      -> SourceCustomization
      -> SourceMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> ObjectCodec (BackendSourceKind b) (BackendSourceKind b)
forall output. HasCodec output => Text -> ObjectCodec output output
requiredField' Text
"kind" ObjectCodec (BackendSourceKind b) (BackendSourceKind b)
-> (SourceMetadata b -> BackendSourceKind b)
-> Codec Object (SourceMetadata b) (BackendSourceKind b)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.== SourceMetadata b -> BackendSourceKind b
forall (b :: BackendType). SourceMetadata b -> BackendSourceKind b
_smKind
        Codec
  Object
  (SourceMetadata b)
  (InsOrdHashMap (TableName b) (TableMetadata b)
   -> InsOrdHashMap (FunctionName b) (FunctionMetadata b)
   -> SourceConnConfiguration b
   -> Maybe QueryTagsConfig
   -> SourceCustomization
   -> SourceMetadata b)
-> Codec
     Object
     (SourceMetadata b)
     (InsOrdHashMap (TableName b) (TableMetadata b))
-> Codec
     Object
     (SourceMetadata b)
     (InsOrdHashMap (FunctionName b) (FunctionMetadata b)
      -> SourceConnConfiguration b
      -> Maybe QueryTagsConfig
      -> SourceCustomization
      -> SourceMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> ValueCodec
     (InsOrdHashMap (TableName b) (TableMetadata b))
     (InsOrdHashMap (TableName b) (TableMetadata b))
-> ObjectCodec
     (InsOrdHashMap (TableName b) (TableMetadata b))
     (InsOrdHashMap (TableName b) (TableMetadata b))
forall input output.
Text -> ValueCodec input output -> ObjectCodec input output
requiredFieldWith' Text
"tables" ((TableMetadata b -> TableName b)
-> ValueCodec
     (InsOrdHashMap (TableName b) (TableMetadata b))
     (InsOrdHashMap (TableName b) (TableMetadata b))
forall a k.
(HasCodec a, Hashable k, Ord k) =>
(a -> k) -> JSONCodec (InsOrdHashMap k a)
sortedElemsCodec TableMetadata b -> TableName b
forall (b :: BackendType). TableMetadata b -> TableName b
_tmTable) ObjectCodec
  (InsOrdHashMap (TableName b) (TableMetadata b))
  (InsOrdHashMap (TableName b) (TableMetadata b))
-> (SourceMetadata b
    -> InsOrdHashMap (TableName b) (TableMetadata b))
-> Codec
     Object
     (SourceMetadata b)
     (InsOrdHashMap (TableName b) (TableMetadata b))
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.== SourceMetadata b -> InsOrdHashMap (TableName b) (TableMetadata b)
forall (b :: BackendType). SourceMetadata b -> Tables b
_smTables
        Codec
  Object
  (SourceMetadata b)
  (InsOrdHashMap (FunctionName b) (FunctionMetadata b)
   -> SourceConnConfiguration b
   -> Maybe QueryTagsConfig
   -> SourceCustomization
   -> SourceMetadata b)
-> Codec
     Object
     (SourceMetadata b)
     (InsOrdHashMap (FunctionName b) (FunctionMetadata b))
-> Codec
     Object
     (SourceMetadata b)
     (SourceConnConfiguration b
      -> Maybe QueryTagsConfig
      -> SourceCustomization
      -> SourceMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> JSONCodec (InsOrdHashMap (FunctionName b) (FunctionMetadata b))
-> InsOrdHashMap (FunctionName b) (FunctionMetadata b)
-> ObjectCodec
     (InsOrdHashMap (FunctionName b) (FunctionMetadata b))
     (InsOrdHashMap (FunctionName b) (FunctionMetadata b))
forall output.
Eq output =>
Text -> JSONCodec output -> output -> ObjectCodec output output
optionalFieldOrNullWithOmittedDefaultWith' Text
"functions" ((FunctionMetadata b -> FunctionName b)
-> JSONCodec (InsOrdHashMap (FunctionName b) (FunctionMetadata b))
forall a k.
(HasCodec a, Hashable k, Ord k) =>
(a -> k) -> JSONCodec (InsOrdHashMap k a)
sortedElemsCodec FunctionMetadata b -> FunctionName b
forall (b :: BackendType). FunctionMetadata b -> FunctionName b
_fmFunction) ([(FunctionName b, FunctionMetadata b)]
-> InsOrdHashMap (FunctionName b) (FunctionMetadata b)
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
OM.fromList []) ObjectCodec
  (InsOrdHashMap (FunctionName b) (FunctionMetadata b))
  (InsOrdHashMap (FunctionName b) (FunctionMetadata b))
-> (SourceMetadata b
    -> InsOrdHashMap (FunctionName b) (FunctionMetadata b))
-> Codec
     Object
     (SourceMetadata b)
     (InsOrdHashMap (FunctionName b) (FunctionMetadata b))
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.== SourceMetadata b
-> InsOrdHashMap (FunctionName b) (FunctionMetadata b)
forall (b :: BackendType). SourceMetadata b -> Functions b
_smFunctions
        Codec
  Object
  (SourceMetadata b)
  (SourceConnConfiguration b
   -> Maybe QueryTagsConfig
   -> SourceCustomization
   -> SourceMetadata b)
-> Codec Object (SourceMetadata b) (SourceConnConfiguration b)
-> Codec
     Object
     (SourceMetadata b)
     (Maybe QueryTagsConfig -> SourceCustomization -> SourceMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> ObjectCodec
     (SourceConnConfiguration b) (SourceConnConfiguration b)
forall output. HasCodec output => Text -> ObjectCodec output output
requiredField' Text
"configuration" ObjectCodec (SourceConnConfiguration b) (SourceConnConfiguration b)
-> (SourceMetadata b -> SourceConnConfiguration b)
-> Codec Object (SourceMetadata b) (SourceConnConfiguration b)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.== SourceMetadata b -> SourceConnConfiguration b
forall (b :: BackendType).
SourceMetadata b -> SourceConnConfiguration b
_smConfiguration
        Codec
  Object
  (SourceMetadata b)
  (Maybe QueryTagsConfig -> SourceCustomization -> SourceMetadata b)
-> Codec Object (SourceMetadata b) (Maybe QueryTagsConfig)
-> Codec
     Object (SourceMetadata b) (SourceCustomization -> SourceMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> ValueCodec QueryTagsConfig QueryTagsConfig
-> ObjectCodec (Maybe QueryTagsConfig) (Maybe QueryTagsConfig)
forall input output.
Text
-> ValueCodec input output
-> ObjectCodec (Maybe input) (Maybe output)
optionalFieldOrNullWith' Text
"query_tags" ValueCodec QueryTagsConfig QueryTagsConfig
forall a. (FromJSON a, ToJSON a) => JSONCodec a
placeholderCodecViaJSON ObjectCodec (Maybe QueryTagsConfig) (Maybe QueryTagsConfig)
-> (SourceMetadata b -> Maybe QueryTagsConfig)
-> Codec Object (SourceMetadata b) (Maybe QueryTagsConfig)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.== SourceMetadata b -> Maybe QueryTagsConfig
forall (b :: BackendType).
SourceMetadata b -> Maybe QueryTagsConfig
_smQueryTags -- TODO: replace placeholder
        Codec
  Object (SourceMetadata b) (SourceCustomization -> SourceMetadata b)
-> Codec Object (SourceMetadata b) SourceCustomization
-> ObjectCodec (SourceMetadata b) (SourceMetadata b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> SourceCustomization
-> ObjectCodec SourceCustomization SourceCustomization
forall output.
(Eq output, HasCodec output) =>
Text -> output -> ObjectCodec output output
optionalFieldOrNullWithOmittedDefault' Text
"customization" SourceCustomization
emptySourceCustomization ObjectCodec SourceCustomization SourceCustomization
-> (SourceMetadata b -> SourceCustomization)
-> Codec Object (SourceMetadata b) SourceCustomization
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.== SourceMetadata b -> SourceCustomization
forall (b :: BackendType). SourceMetadata b -> SourceCustomization
_smCustomization
    where
      .== :: ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
(.==) = ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
(AC..=)

mkSourceMetadata ::
  forall (b :: BackendType).
  Backend b =>
  SourceName ->
  BackendSourceKind b ->
  SourceConnConfiguration b ->
  SourceCustomization ->
  BackendSourceMetadata
mkSourceMetadata :: SourceName
-> BackendSourceKind b
-> SourceConnConfiguration b
-> SourceCustomization
-> BackendSourceMetadata
mkSourceMetadata SourceName
name BackendSourceKind b
backendSourceKind SourceConnConfiguration b
config SourceCustomization
customization =
  AnyBackend SourceMetadata -> BackendSourceMetadata
BackendSourceMetadata (AnyBackend SourceMetadata -> BackendSourceMetadata)
-> AnyBackend SourceMetadata -> BackendSourceMetadata
forall a b. (a -> b) -> a -> b
$
    SourceMetadata b -> AnyBackend SourceMetadata
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
AB.mkAnyBackend (SourceMetadata b -> AnyBackend SourceMetadata)
-> SourceMetadata b -> AnyBackend SourceMetadata
forall a b. (a -> b) -> a -> b
$
      SourceName
-> BackendSourceKind b
-> Tables b
-> Functions b
-> SourceConnConfiguration b
-> Maybe QueryTagsConfig
-> SourceCustomization
-> SourceMetadata b
forall (b :: BackendType).
SourceName
-> BackendSourceKind b
-> Tables b
-> Functions b
-> SourceConnConfiguration b
-> Maybe QueryTagsConfig
-> SourceCustomization
-> SourceMetadata b
SourceMetadata
        @b
        SourceName
name
        BackendSourceKind b
backendSourceKind
        Tables b
forall a. Monoid a => a
mempty
        Functions b
forall a. Monoid a => a
mempty
        SourceConnConfiguration b
config
        Maybe QueryTagsConfig
forall a. Maybe a
Nothing
        SourceCustomization
customization

-- | Source configuration as stored in the Metadata DB for some existentialized backend.
newtype BackendSourceMetadata = BackendSourceMetadata {BackendSourceMetadata -> AnyBackend SourceMetadata
unBackendSourceMetadata :: AB.AnyBackend SourceMetadata}
  deriving newtype (BackendSourceMetadata -> BackendSourceMetadata -> Bool
(BackendSourceMetadata -> BackendSourceMetadata -> Bool)
-> (BackendSourceMetadata -> BackendSourceMetadata -> Bool)
-> Eq BackendSourceMetadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BackendSourceMetadata -> BackendSourceMetadata -> Bool
$c/= :: BackendSourceMetadata -> BackendSourceMetadata -> Bool
== :: BackendSourceMetadata -> BackendSourceMetadata -> Bool
$c== :: BackendSourceMetadata -> BackendSourceMetadata -> Bool
Eq, Int -> BackendSourceMetadata -> ShowS
[BackendSourceMetadata] -> ShowS
BackendSourceMetadata -> String
(Int -> BackendSourceMetadata -> ShowS)
-> (BackendSourceMetadata -> String)
-> ([BackendSourceMetadata] -> ShowS)
-> Show BackendSourceMetadata
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BackendSourceMetadata] -> ShowS
$cshowList :: [BackendSourceMetadata] -> ShowS
show :: BackendSourceMetadata -> String
$cshow :: BackendSourceMetadata -> String
showsPrec :: Int -> BackendSourceMetadata -> ShowS
$cshowsPrec :: Int -> BackendSourceMetadata -> ShowS
Show)

toSourceMetadata :: forall b. (Backend b) => Prism' BackendSourceMetadata (SourceMetadata b)
toSourceMetadata :: Prism' BackendSourceMetadata (SourceMetadata b)
toSourceMetadata = (SourceMetadata b -> BackendSourceMetadata)
-> (BackendSourceMetadata -> Maybe (SourceMetadata b))
-> Prism' BackendSourceMetadata (SourceMetadata b)
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' (AnyBackend SourceMetadata -> BackendSourceMetadata
BackendSourceMetadata (AnyBackend SourceMetadata -> BackendSourceMetadata)
-> (SourceMetadata b -> AnyBackend SourceMetadata)
-> SourceMetadata b
-> BackendSourceMetadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceMetadata b -> AnyBackend SourceMetadata
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
AB.mkAnyBackend) (AnyBackend SourceMetadata -> Maybe (SourceMetadata b)
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
AnyBackend i -> Maybe (i b)
AB.unpackAnyBackend (AnyBackend SourceMetadata -> Maybe (SourceMetadata b))
-> (BackendSourceMetadata -> AnyBackend SourceMetadata)
-> BackendSourceMetadata
-> Maybe (SourceMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BackendSourceMetadata -> AnyBackend SourceMetadata
unBackendSourceMetadata)

getSourceName :: BackendSourceMetadata -> SourceName
getSourceName :: BackendSourceMetadata -> SourceName
getSourceName BackendSourceMetadata
e = AnyBackend SourceMetadata
-> (forall (b :: BackendType).
    Backend b =>
    SourceMetadata b -> SourceName)
-> SourceName
forall (c :: BackendType -> Constraint) (i :: BackendType -> *) r.
AllBackendsSatisfy c =>
AnyBackend i -> (forall (b :: BackendType). c b => i b -> r) -> r
AB.dispatchAnyBackend @Backend (BackendSourceMetadata -> AnyBackend SourceMetadata
unBackendSourceMetadata BackendSourceMetadata
e) forall (b :: BackendType).
Backend b =>
SourceMetadata b -> SourceName
forall (b :: BackendType). SourceMetadata b -> SourceName
_smName

type Sources = InsOrdHashMap SourceName BackendSourceMetadata

sourcesCodec :: AC.JSONCodec Sources
sourcesCodec :: JSONCodec Sources
sourcesCodec = JSONCodec BackendSourceMetadata
-> (BackendSourceMetadata -> SourceName) -> JSONCodec Sources
forall k a.
(Hashable k, Ord k) =>
JSONCodec a -> (a -> k) -> JSONCodec (InsOrdHashMap k a)
sortedElemsCodecWith JSONCodec BackendSourceMetadata
backendSourceMetadataCodec BackendSourceMetadata -> SourceName
getSourceName

parseNonSourcesMetadata ::
  Object ->
  Parser
    ( RemoteSchemas,
      QueryCollections,
      MetadataAllowlist,
      CustomTypes,
      Actions,
      CronTriggers,
      ApiLimit,
      MetricsConfig,
      InheritedRoles,
      SetGraphqlIntrospectionOptions
    )
parseNonSourcesMetadata :: Object
-> Parser
     (RemoteSchemas, QueryCollections, MetadataAllowlist, CustomTypes,
      Actions, CronTriggers, ApiLimit, MetricsConfig, InheritedRoles,
      SetGraphqlIntrospectionOptions)
parseNonSourcesMetadata Object
o = do
  RemoteSchemas
remoteSchemas <-
    Text
-> (RemoteSchemaMetadata -> RemoteSchemaName)
-> Parser [RemoteSchemaMetadata]
-> Parser RemoteSchemas
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"remote schemas" RemoteSchemaMetadata -> RemoteSchemaName
_rsmName (Parser [RemoteSchemaMetadata] -> Parser RemoteSchemas)
-> Parser [RemoteSchemaMetadata] -> Parser RemoteSchemas
forall a b. (a -> b) -> a -> b
$
      Object
o Object -> Key -> Parser (Maybe [RemoteSchemaMetadata])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"remote_schemas" Parser (Maybe [RemoteSchemaMetadata])
-> [RemoteSchemaMetadata] -> Parser [RemoteSchemaMetadata]
forall a. Parser (Maybe a) -> a -> Parser a
.!= []
  QueryCollections
queryCollections <-
    Text
-> (CreateCollection -> CollectionName)
-> Parser [CreateCollection]
-> Parser QueryCollections
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"query collections" CreateCollection -> CollectionName
_ccName (Parser [CreateCollection] -> Parser QueryCollections)
-> Parser [CreateCollection] -> Parser QueryCollections
forall a b. (a -> b) -> a -> b
$
      Object
o Object -> Key -> Parser (Maybe [CreateCollection])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"query_collections" Parser (Maybe [CreateCollection])
-> [CreateCollection] -> Parser [CreateCollection]
forall a. Parser (Maybe a) -> a -> Parser a
.!= []
  MetadataAllowlist
allowlist <- Text
-> (AllowlistEntry -> CollectionName)
-> Parser [AllowlistEntry]
-> Parser MetadataAllowlist
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"allowlist entries" AllowlistEntry -> CollectionName
aeCollection (Parser [AllowlistEntry] -> Parser MetadataAllowlist)
-> Parser [AllowlistEntry] -> Parser MetadataAllowlist
forall a b. (a -> b) -> a -> b
$ Object
o Object -> Key -> Parser (Maybe [AllowlistEntry])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"allowlist" Parser (Maybe [AllowlistEntry])
-> [AllowlistEntry] -> Parser [AllowlistEntry]
forall a. Parser (Maybe a) -> a -> Parser a
.!= []
  CustomTypes
customTypes <- Object
o Object -> Key -> Parser (Maybe CustomTypes)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"custom_types" Parser (Maybe CustomTypes) -> CustomTypes -> Parser CustomTypes
forall a. Parser (Maybe a) -> a -> Parser a
.!= CustomTypes
emptyCustomTypes
  Actions
actions <- Text
-> (ActionMetadata -> ActionName)
-> Parser [ActionMetadata]
-> Parser Actions
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"actions" ActionMetadata -> ActionName
_amName (Parser [ActionMetadata] -> Parser Actions)
-> Parser [ActionMetadata] -> Parser Actions
forall a b. (a -> b) -> a -> b
$ Object
o Object -> Key -> Parser (Maybe [ActionMetadata])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"actions" Parser (Maybe [ActionMetadata])
-> [ActionMetadata] -> Parser [ActionMetadata]
forall a. Parser (Maybe a) -> a -> Parser a
.!= []
  CronTriggers
cronTriggers <-
    Text
-> (CronTriggerMetadata -> TriggerName)
-> Parser [CronTriggerMetadata]
-> Parser CronTriggers
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"cron triggers" CronTriggerMetadata -> TriggerName
ctName (Parser [CronTriggerMetadata] -> Parser CronTriggers)
-> Parser [CronTriggerMetadata] -> Parser CronTriggers
forall a b. (a -> b) -> a -> b
$
      Object
o Object -> Key -> Parser (Maybe [CronTriggerMetadata])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"cron_triggers" Parser (Maybe [CronTriggerMetadata])
-> [CronTriggerMetadata] -> Parser [CronTriggerMetadata]
forall a. Parser (Maybe a) -> a -> Parser a
.!= []

  ApiLimit
apiLimits <- Object
o Object -> Key -> Parser (Maybe ApiLimit)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"api_limits" Parser (Maybe ApiLimit) -> ApiLimit -> Parser ApiLimit
forall a. Parser (Maybe a) -> a -> Parser a
.!= ApiLimit
emptyApiLimit
  MetricsConfig
metricsConfig <- Object
o Object -> Key -> Parser (Maybe MetricsConfig)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"metrics_config" Parser (Maybe MetricsConfig)
-> MetricsConfig -> Parser MetricsConfig
forall a. Parser (Maybe a) -> a -> Parser a
.!= MetricsConfig
emptyMetricsConfig
  InheritedRoles
inheritedRoles <-
    Text
-> (Role -> RoleName) -> Parser [Role] -> Parser InheritedRoles
forall k a.
(Hashable k, Eq k, ToTxt k) =>
Text -> (a -> k) -> Parser [a] -> Parser (InsOrdHashMap k a)
parseListAsMap Text
"inherited roles" Role -> RoleName
_rRoleName (Parser [Role] -> Parser InheritedRoles)
-> Parser [Role] -> Parser InheritedRoles
forall a b. (a -> b) -> a -> b
$
      Object
o Object -> Key -> Parser (Maybe [Role])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"inherited_roles" Parser (Maybe [Role]) -> [Role] -> Parser [Role]
forall a. Parser (Maybe a) -> a -> Parser a
.!= []
  SetGraphqlIntrospectionOptions
introspectionDisabledForRoles <- Object
o Object -> Key -> Parser (Maybe SetGraphqlIntrospectionOptions)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"graphql_schema_introspection" Parser (Maybe SetGraphqlIntrospectionOptions)
-> SetGraphqlIntrospectionOptions
-> Parser SetGraphqlIntrospectionOptions
forall a. Parser (Maybe a) -> a -> Parser a
.!= SetGraphqlIntrospectionOptions
forall a. Monoid a => a
mempty
  (RemoteSchemas, QueryCollections, MetadataAllowlist, CustomTypes,
 Actions, CronTriggers, ApiLimit, MetricsConfig, InheritedRoles,
 SetGraphqlIntrospectionOptions)
-> Parser
     (RemoteSchemas, QueryCollections, MetadataAllowlist, CustomTypes,
      Actions, CronTriggers, ApiLimit, MetricsConfig, InheritedRoles,
      SetGraphqlIntrospectionOptions)
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    ( RemoteSchemas
remoteSchemas,
      QueryCollections
queryCollections,
      MetadataAllowlist
allowlist,
      CustomTypes
customTypes,
      Actions
actions,
      CronTriggers
cronTriggers,
      ApiLimit
apiLimits,
      MetricsConfig
metricsConfig,
      InheritedRoles
inheritedRoles,
      SetGraphqlIntrospectionOptions
introspectionDisabledForRoles
    )

-- | This newtype simply wraps the BackendConfig type family so that it can be used
-- with BackendMap in the Metadata type. GHC will not allow the type family to be
-- used directly. :(
newtype BackendConfigWrapper b = BackendConfigWrapper {BackendConfigWrapper b -> BackendConfig b
unBackendConfigWrapper :: BackendConfig b}

deriving newtype instance (Backend b) => Show (BackendConfigWrapper b)

deriving newtype instance (Backend b) => Eq (BackendConfigWrapper b)

deriving newtype instance (Backend b) => ToJSON (BackendConfigWrapper b)

deriving newtype instance (Backend b) => FromJSON (BackendConfigWrapper b)

deriving newtype instance (Semigroup (BackendConfig b)) => Semigroup (BackendConfigWrapper b)

deriving newtype instance (Monoid (BackendConfig b)) => Monoid (BackendConfigWrapper b)

data CatalogStateType
  = CSTCli
  | CSTConsole
  deriving (Int -> CatalogStateType -> ShowS
[CatalogStateType] -> ShowS
CatalogStateType -> String
(Int -> CatalogStateType -> ShowS)
-> (CatalogStateType -> String)
-> ([CatalogStateType] -> ShowS)
-> Show CatalogStateType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CatalogStateType] -> ShowS
$cshowList :: [CatalogStateType] -> ShowS
show :: CatalogStateType -> String
$cshow :: CatalogStateType -> String
showsPrec :: Int -> CatalogStateType -> ShowS
$cshowsPrec :: Int -> CatalogStateType -> ShowS
Show, CatalogStateType -> CatalogStateType -> Bool
(CatalogStateType -> CatalogStateType -> Bool)
-> (CatalogStateType -> CatalogStateType -> Bool)
-> Eq CatalogStateType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CatalogStateType -> CatalogStateType -> Bool
$c/= :: CatalogStateType -> CatalogStateType -> Bool
== :: CatalogStateType -> CatalogStateType -> Bool
$c== :: CatalogStateType -> CatalogStateType -> Bool
Eq)

$(deriveJSON defaultOptions {constructorTagModifier = snakeCase . drop 3} ''CatalogStateType)

data SetCatalogState = SetCatalogState
  { SetCatalogState -> CatalogStateType
_scsType :: CatalogStateType,
    SetCatalogState -> Value
_scsState :: Value
  }
  deriving (Int -> SetCatalogState -> ShowS
[SetCatalogState] -> ShowS
SetCatalogState -> String
(Int -> SetCatalogState -> ShowS)
-> (SetCatalogState -> String)
-> ([SetCatalogState] -> ShowS)
-> Show SetCatalogState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SetCatalogState] -> ShowS
$cshowList :: [SetCatalogState] -> ShowS
show :: SetCatalogState -> String
$cshow :: SetCatalogState -> String
showsPrec :: Int -> SetCatalogState -> ShowS
$cshowsPrec :: Int -> SetCatalogState -> ShowS
Show, SetCatalogState -> SetCatalogState -> Bool
(SetCatalogState -> SetCatalogState -> Bool)
-> (SetCatalogState -> SetCatalogState -> Bool)
-> Eq SetCatalogState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SetCatalogState -> SetCatalogState -> Bool
$c/= :: SetCatalogState -> SetCatalogState -> Bool
== :: SetCatalogState -> SetCatalogState -> Bool
$c== :: SetCatalogState -> SetCatalogState -> Bool
Eq)

$(deriveJSON hasuraJSON ''SetCatalogState)

data CatalogState = CatalogState
  { CatalogState -> Text
_csId :: Text,
    CatalogState -> Value
_csCliState :: Value,
    CatalogState -> Value
_csConsoleState :: Value
  }
  deriving (Int -> CatalogState -> ShowS
[CatalogState] -> ShowS
CatalogState -> String
(Int -> CatalogState -> ShowS)
-> (CatalogState -> String)
-> ([CatalogState] -> ShowS)
-> Show CatalogState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CatalogState] -> ShowS
$cshowList :: [CatalogState] -> ShowS
show :: CatalogState -> String
$cshow :: CatalogState -> String
showsPrec :: Int -> CatalogState -> ShowS
$cshowsPrec :: Int -> CatalogState -> ShowS
Show, CatalogState -> CatalogState -> Bool
(CatalogState -> CatalogState -> Bool)
-> (CatalogState -> CatalogState -> Bool) -> Eq CatalogState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CatalogState -> CatalogState -> Bool
$c/= :: CatalogState -> CatalogState -> Bool
== :: CatalogState -> CatalogState -> Bool
$c== :: CatalogState -> CatalogState -> Bool
Eq)

$(deriveToJSON hasuraJSON ''CatalogState)

data GetCatalogState
  = GetCatalogState
  deriving (Int -> GetCatalogState -> ShowS
[GetCatalogState] -> ShowS
GetCatalogState -> String
(Int -> GetCatalogState -> ShowS)
-> (GetCatalogState -> String)
-> ([GetCatalogState] -> ShowS)
-> Show GetCatalogState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetCatalogState] -> ShowS
$cshowList :: [GetCatalogState] -> ShowS
show :: GetCatalogState -> String
$cshow :: GetCatalogState -> String
showsPrec :: Int -> GetCatalogState -> ShowS
$cshowsPrec :: Int -> GetCatalogState -> ShowS
Show, GetCatalogState -> GetCatalogState -> Bool
(GetCatalogState -> GetCatalogState -> Bool)
-> (GetCatalogState -> GetCatalogState -> Bool)
-> Eq GetCatalogState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GetCatalogState -> GetCatalogState -> Bool
$c/= :: GetCatalogState -> GetCatalogState -> Bool
== :: GetCatalogState -> GetCatalogState -> Bool
$c== :: GetCatalogState -> GetCatalogState -> Bool
Eq)

$(deriveToJSON defaultOptions ''GetCatalogState)

instance FromJSON GetCatalogState where
  parseJSON :: Value -> Parser GetCatalogState
parseJSON Value
_ = GetCatalogState -> Parser GetCatalogState
forall (f :: * -> *) a. Applicative f => a -> f a
pure GetCatalogState
GetCatalogState