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

module Hasura.RemoteSchema.Metadata.Core
  ( RemoteSchemaDef (..),
    RemoteSchemaName (..),
    UrlFromEnv,
    getUrlFromEnv,
    RemoteSchemaMetadataG (..),
    rsmComment,
    rsmDefinition,
    rsmName,
    rsmPermissions,
    rsmRemoteRelationships,
  )
where

import Autodocodec (object, optionalField', optionalFieldWithDefault', optionalFieldWithDefaultWith', requiredField', (.=))
import Autodocodec.Class (HasCodec (codec))
import Autodocodec.Extended (typeableName)
import Control.Lens (makeLenses)
import Data.Aeson qualified as J
import Data.Environment qualified as Env
import Data.HashMap.Strict.InsOrd.Autodocodec (insertionOrderedElemsCodec)
import Data.HashMap.Strict.InsOrd.Extended qualified as InsOrdHashMap
import Data.Text qualified as T
import Data.Typeable (Typeable)
import Hasura.Base.Error
import Hasura.Prelude
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.Headers (HeaderConf (..))
import Hasura.RemoteSchema.Metadata.Base
import Hasura.RemoteSchema.Metadata.Customization
import Hasura.RemoteSchema.Metadata.Permission
import Hasura.RemoteSchema.Metadata.RemoteRelationship
import Network.URI.Extended qualified as N

type UrlFromEnv = Text

-- | Unvalidated remote schema config, from the user's API request
data RemoteSchemaDef = RemoteSchemaDef
  { RemoteSchemaDef -> Maybe InputWebhook
_rsdUrl :: Maybe InputWebhook,
    RemoteSchemaDef -> Maybe UrlFromEnv
_rsdUrlFromEnv :: Maybe UrlFromEnv,
    RemoteSchemaDef -> Maybe [HeaderConf]
_rsdHeaders :: Maybe [HeaderConf],
    RemoteSchemaDef -> Bool
_rsdForwardClientHeaders :: Bool,
    RemoteSchemaDef -> Maybe Int
_rsdTimeoutSeconds :: Maybe Int,
    RemoteSchemaDef -> Maybe RemoteSchemaCustomization
_rsdCustomization :: Maybe RemoteSchemaCustomization
    -- NOTE: In the future we might extend this API to support a small DSL of
    -- name transformations; this might live at a different layer, and be part of
    -- the schema customization story.
    --
    -- See: https://github.com/hasura/graphql-engine-mono/issues/144
    -- TODO we probably want to move this into a sub-field "transformations"?
  }
  deriving (Int -> RemoteSchemaDef -> ShowS
[RemoteSchemaDef] -> ShowS
RemoteSchemaDef -> String
(Int -> RemoteSchemaDef -> ShowS)
-> (RemoteSchemaDef -> String)
-> ([RemoteSchemaDef] -> ShowS)
-> Show RemoteSchemaDef
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RemoteSchemaDef -> ShowS
showsPrec :: Int -> RemoteSchemaDef -> ShowS
$cshow :: RemoteSchemaDef -> String
show :: RemoteSchemaDef -> String
$cshowList :: [RemoteSchemaDef] -> ShowS
showList :: [RemoteSchemaDef] -> ShowS
Show, RemoteSchemaDef -> RemoteSchemaDef -> Bool
(RemoteSchemaDef -> RemoteSchemaDef -> Bool)
-> (RemoteSchemaDef -> RemoteSchemaDef -> Bool)
-> Eq RemoteSchemaDef
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RemoteSchemaDef -> RemoteSchemaDef -> Bool
== :: RemoteSchemaDef -> RemoteSchemaDef -> Bool
$c/= :: RemoteSchemaDef -> RemoteSchemaDef -> Bool
/= :: RemoteSchemaDef -> RemoteSchemaDef -> Bool
Eq, (forall x. RemoteSchemaDef -> Rep RemoteSchemaDef x)
-> (forall x. Rep RemoteSchemaDef x -> RemoteSchemaDef)
-> Generic RemoteSchemaDef
forall x. Rep RemoteSchemaDef x -> RemoteSchemaDef
forall x. RemoteSchemaDef -> Rep RemoteSchemaDef x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. RemoteSchemaDef -> Rep RemoteSchemaDef x
from :: forall x. RemoteSchemaDef -> Rep RemoteSchemaDef x
$cto :: forall x. Rep RemoteSchemaDef x -> RemoteSchemaDef
to :: forall x. Rep RemoteSchemaDef x -> RemoteSchemaDef
Generic)

instance NFData RemoteSchemaDef

instance HasCodec RemoteSchemaDef where
  codec :: JSONCodec RemoteSchemaDef
codec =
    UrlFromEnv
-> ObjectCodec RemoteSchemaDef RemoteSchemaDef
-> JSONCodec RemoteSchemaDef
forall input output.
UrlFromEnv -> ObjectCodec input output -> ValueCodec input output
object UrlFromEnv
"RemoteSchemaDef"
      (ObjectCodec RemoteSchemaDef RemoteSchemaDef
 -> JSONCodec RemoteSchemaDef)
-> ObjectCodec RemoteSchemaDef RemoteSchemaDef
-> JSONCodec RemoteSchemaDef
forall a b. (a -> b) -> a -> b
$ Maybe InputWebhook
-> Maybe UrlFromEnv
-> Maybe [HeaderConf]
-> Bool
-> Maybe Int
-> Maybe RemoteSchemaCustomization
-> RemoteSchemaDef
RemoteSchemaDef
      (Maybe InputWebhook
 -> Maybe UrlFromEnv
 -> Maybe [HeaderConf]
 -> Bool
 -> Maybe Int
 -> Maybe RemoteSchemaCustomization
 -> RemoteSchemaDef)
-> Codec Object RemoteSchemaDef (Maybe InputWebhook)
-> Codec
     Object
     RemoteSchemaDef
     (Maybe UrlFromEnv
      -> Maybe [HeaderConf]
      -> Bool
      -> Maybe Int
      -> Maybe RemoteSchemaCustomization
      -> RemoteSchemaDef)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UrlFromEnv -> ObjectCodec (Maybe InputWebhook) (Maybe InputWebhook)
forall output.
HasCodec output =>
UrlFromEnv -> ObjectCodec (Maybe output) (Maybe output)
optionalField' UrlFromEnv
"url"
      ObjectCodec (Maybe InputWebhook) (Maybe InputWebhook)
-> (RemoteSchemaDef -> Maybe InputWebhook)
-> Codec Object RemoteSchemaDef (Maybe InputWebhook)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaDef -> Maybe InputWebhook
_rsdUrl
        Codec
  Object
  RemoteSchemaDef
  (Maybe UrlFromEnv
   -> Maybe [HeaderConf]
   -> Bool
   -> Maybe Int
   -> Maybe RemoteSchemaCustomization
   -> RemoteSchemaDef)
-> Codec Object RemoteSchemaDef (Maybe UrlFromEnv)
-> Codec
     Object
     RemoteSchemaDef
     (Maybe [HeaderConf]
      -> Bool
      -> Maybe Int
      -> Maybe RemoteSchemaCustomization
      -> RemoteSchemaDef)
forall a b.
Codec Object RemoteSchemaDef (a -> b)
-> Codec Object RemoteSchemaDef a -> Codec Object RemoteSchemaDef b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> UrlFromEnv -> ObjectCodec (Maybe UrlFromEnv) (Maybe UrlFromEnv)
forall output.
HasCodec output =>
UrlFromEnv -> ObjectCodec (Maybe output) (Maybe output)
optionalField' UrlFromEnv
"url_from_env"
      ObjectCodec (Maybe UrlFromEnv) (Maybe UrlFromEnv)
-> (RemoteSchemaDef -> Maybe UrlFromEnv)
-> Codec Object RemoteSchemaDef (Maybe UrlFromEnv)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaDef -> Maybe UrlFromEnv
_rsdUrlFromEnv
        Codec
  Object
  RemoteSchemaDef
  (Maybe [HeaderConf]
   -> Bool
   -> Maybe Int
   -> Maybe RemoteSchemaCustomization
   -> RemoteSchemaDef)
-> Codec Object RemoteSchemaDef (Maybe [HeaderConf])
-> Codec
     Object
     RemoteSchemaDef
     (Bool
      -> Maybe Int -> Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
forall a b.
Codec Object RemoteSchemaDef (a -> b)
-> Codec Object RemoteSchemaDef a -> Codec Object RemoteSchemaDef b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> UrlFromEnv -> ObjectCodec (Maybe [HeaderConf]) (Maybe [HeaderConf])
forall output.
HasCodec output =>
UrlFromEnv -> ObjectCodec (Maybe output) (Maybe output)
optionalField' UrlFromEnv
"headers"
      ObjectCodec (Maybe [HeaderConf]) (Maybe [HeaderConf])
-> (RemoteSchemaDef -> Maybe [HeaderConf])
-> Codec Object RemoteSchemaDef (Maybe [HeaderConf])
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaDef -> Maybe [HeaderConf]
_rsdHeaders
        Codec
  Object
  RemoteSchemaDef
  (Bool
   -> Maybe Int -> Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
-> Codec Object RemoteSchemaDef Bool
-> Codec
     Object
     RemoteSchemaDef
     (Maybe Int -> Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
forall a b.
Codec Object RemoteSchemaDef (a -> b)
-> Codec Object RemoteSchemaDef a -> Codec Object RemoteSchemaDef b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> UrlFromEnv -> Bool -> ObjectCodec Bool Bool
forall output.
HasCodec output =>
UrlFromEnv -> output -> ObjectCodec output output
optionalFieldWithDefault' UrlFromEnv
"forward_client_headers" Bool
False
      ObjectCodec Bool Bool
-> (RemoteSchemaDef -> Bool) -> Codec Object RemoteSchemaDef Bool
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaDef -> Bool
_rsdForwardClientHeaders
        Codec
  Object
  RemoteSchemaDef
  (Maybe Int -> Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
-> Codec Object RemoteSchemaDef (Maybe Int)
-> Codec
     Object
     RemoteSchemaDef
     (Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
forall a b.
Codec Object RemoteSchemaDef (a -> b)
-> Codec Object RemoteSchemaDef a -> Codec Object RemoteSchemaDef b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> UrlFromEnv -> ObjectCodec (Maybe Int) (Maybe Int)
forall output.
HasCodec output =>
UrlFromEnv -> ObjectCodec (Maybe output) (Maybe output)
optionalField' UrlFromEnv
"timeout_seconds"
      ObjectCodec (Maybe Int) (Maybe Int)
-> (RemoteSchemaDef -> Maybe Int)
-> Codec Object RemoteSchemaDef (Maybe Int)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaDef -> Maybe Int
_rsdTimeoutSeconds
        Codec
  Object
  RemoteSchemaDef
  (Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
-> Codec Object RemoteSchemaDef (Maybe RemoteSchemaCustomization)
-> ObjectCodec RemoteSchemaDef RemoteSchemaDef
forall a b.
Codec Object RemoteSchemaDef (a -> b)
-> Codec Object RemoteSchemaDef a -> Codec Object RemoteSchemaDef b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> UrlFromEnv
-> ObjectCodec
     (Maybe RemoteSchemaCustomization) (Maybe RemoteSchemaCustomization)
forall output.
HasCodec output =>
UrlFromEnv -> ObjectCodec (Maybe output) (Maybe output)
optionalField' UrlFromEnv
"customization"
      ObjectCodec
  (Maybe RemoteSchemaCustomization) (Maybe RemoteSchemaCustomization)
-> (RemoteSchemaDef -> Maybe RemoteSchemaCustomization)
-> Codec Object RemoteSchemaDef (Maybe RemoteSchemaCustomization)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaDef -> Maybe RemoteSchemaCustomization
_rsdCustomization

instance J.ToJSON RemoteSchemaDef where
  toJSON :: RemoteSchemaDef -> Value
toJSON = Options -> RemoteSchemaDef -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
J.genericToJSON Options
hasuraJSON {omitNothingFields :: Bool
J.omitNothingFields = Bool
True}
  toEncoding :: RemoteSchemaDef -> Encoding
toEncoding = Options -> RemoteSchemaDef -> Encoding
forall a.
(Generic a, GToJSON' Encoding Zero (Rep a)) =>
Options -> a -> Encoding
J.genericToEncoding Options
hasuraJSON {omitNothingFields :: Bool
J.omitNothingFields = Bool
True}

instance J.FromJSON RemoteSchemaDef where
  parseJSON :: Value -> Parser RemoteSchemaDef
parseJSON = String
-> (Object -> Parser RemoteSchemaDef)
-> Value
-> Parser RemoteSchemaDef
forall a. String -> (Object -> Parser a) -> Value -> Parser a
J.withObject String
"Object" ((Object -> Parser RemoteSchemaDef)
 -> Value -> Parser RemoteSchemaDef)
-> (Object -> Parser RemoteSchemaDef)
-> Value
-> Parser RemoteSchemaDef
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    Maybe InputWebhook
-> Maybe UrlFromEnv
-> Maybe [HeaderConf]
-> Bool
-> Maybe Int
-> Maybe RemoteSchemaCustomization
-> RemoteSchemaDef
RemoteSchemaDef
      (Maybe InputWebhook
 -> Maybe UrlFromEnv
 -> Maybe [HeaderConf]
 -> Bool
 -> Maybe Int
 -> Maybe RemoteSchemaCustomization
 -> RemoteSchemaDef)
-> Parser (Maybe InputWebhook)
-> Parser
     (Maybe UrlFromEnv
      -> Maybe [HeaderConf]
      -> Bool
      -> Maybe Int
      -> Maybe RemoteSchemaCustomization
      -> RemoteSchemaDef)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o
      Object -> Key -> Parser (Maybe InputWebhook)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
J..:? Key
"url"
      Parser
  (Maybe UrlFromEnv
   -> Maybe [HeaderConf]
   -> Bool
   -> Maybe Int
   -> Maybe RemoteSchemaCustomization
   -> RemoteSchemaDef)
-> Parser (Maybe UrlFromEnv)
-> Parser
     (Maybe [HeaderConf]
      -> Bool
      -> Maybe Int
      -> Maybe RemoteSchemaCustomization
      -> RemoteSchemaDef)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser (Maybe UrlFromEnv)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
J..:? Key
"url_from_env"
      Parser
  (Maybe [HeaderConf]
   -> Bool
   -> Maybe Int
   -> Maybe RemoteSchemaCustomization
   -> RemoteSchemaDef)
-> Parser (Maybe [HeaderConf])
-> Parser
     (Bool
      -> Maybe Int -> Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser (Maybe [HeaderConf])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
J..:? Key
"headers"
      Parser
  (Bool
   -> Maybe Int -> Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
-> Parser Bool
-> Parser
     (Maybe Int -> Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
forall a b. Parser (a -> b) -> Parser a -> Parser 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)
J..:? Key
"forward_client_headers"
      Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
J..!= Bool
False
      Parser
  (Maybe Int -> Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
-> Parser (Maybe Int)
-> Parser (Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
J..:? Key
"timeout_seconds"
      Parser (Maybe RemoteSchemaCustomization -> RemoteSchemaDef)
-> Parser (Maybe RemoteSchemaCustomization)
-> Parser RemoteSchemaDef
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser (Maybe RemoteSchemaCustomization)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
J..:? Key
"customization"

getUrlFromEnv :: (MonadError QErr m) => Env.Environment -> Text -> m (EnvRecord N.URI)
getUrlFromEnv :: forall (m :: * -> *).
MonadError QErr m =>
Environment -> UrlFromEnv -> m (EnvRecord URI)
getUrlFromEnv Environment
env UrlFromEnv
urlFromEnv = do
  let mEnv :: Maybe String
mEnv = Environment -> String -> Maybe String
Env.lookupEnv Environment
env (String -> Maybe String) -> String -> Maybe String
forall a b. (a -> b) -> a -> b
$ UrlFromEnv -> String
T.unpack UrlFromEnv
urlFromEnv
  String
uri <- Maybe String -> m String -> m String
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
onNothing Maybe String
mEnv (Code -> UrlFromEnv -> m String
forall (m :: * -> *) a. QErrM m => Code -> UrlFromEnv -> m a
throw400 Code
InvalidParams (UrlFromEnv -> m String) -> UrlFromEnv -> m String
forall a b. (a -> b) -> a -> b
$ UrlFromEnv -> UrlFromEnv
forall {a}. (Semigroup a, IsString a) => a -> a
envNotFoundMsg UrlFromEnv
urlFromEnv)
  case (String -> Maybe URI
N.parseURI String
uri) of
    Just URI
uri' -> EnvRecord URI -> m (EnvRecord URI)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EnvRecord URI -> m (EnvRecord URI))
-> EnvRecord URI -> m (EnvRecord URI)
forall a b. (a -> b) -> a -> b
$ UrlFromEnv -> URI -> EnvRecord URI
forall a. UrlFromEnv -> a -> EnvRecord a
EnvRecord UrlFromEnv
urlFromEnv URI
uri'
    Maybe URI
Nothing -> Code -> UrlFromEnv -> m (EnvRecord URI)
forall (m :: * -> *) a. QErrM m => Code -> UrlFromEnv -> m a
throw400 Code
InvalidParams (UrlFromEnv -> m (EnvRecord URI))
-> UrlFromEnv -> m (EnvRecord URI)
forall a b. (a -> b) -> a -> b
$ UrlFromEnv -> UrlFromEnv
forall {a}. (Semigroup a, IsString a) => a -> a
invalidUri UrlFromEnv
urlFromEnv
  where
    invalidUri :: a -> a
invalidUri a
x = a
"not a valid URI in environment variable: " a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
x
    envNotFoundMsg :: a -> a
envNotFoundMsg a
e = a
"environment variable '" a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
e a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
"' not set"

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

instance (HasCodec (RemoteRelationshipG r), Typeable r) => HasCodec (RemoteSchemaMetadataG r) where
  codec :: JSONCodec (RemoteSchemaMetadataG r)
codec =
    UrlFromEnv
-> ObjectCodec (RemoteSchemaMetadataG r) (RemoteSchemaMetadataG r)
-> JSONCodec (RemoteSchemaMetadataG r)
forall input output.
UrlFromEnv -> ObjectCodec input output -> ValueCodec input output
object (UrlFromEnv
"RemoteSchemaMetadata_" UrlFromEnv -> UrlFromEnv -> UrlFromEnv
forall a. Semigroup a => a -> a -> a
<> forall a. Typeable a => UrlFromEnv
forall {k} (a :: k). Typeable a => UrlFromEnv
typeableName @r)
      (ObjectCodec (RemoteSchemaMetadataG r) (RemoteSchemaMetadataG r)
 -> JSONCodec (RemoteSchemaMetadataG r))
-> ObjectCodec (RemoteSchemaMetadataG r) (RemoteSchemaMetadataG r)
-> JSONCodec (RemoteSchemaMetadataG r)
forall a b. (a -> b) -> a -> b
$ RemoteSchemaName
-> RemoteSchemaDef
-> Maybe UrlFromEnv
-> [RemoteSchemaPermissionMetadata]
-> SchemaRemoteRelationships r
-> RemoteSchemaMetadataG r
forall r.
RemoteSchemaName
-> RemoteSchemaDef
-> Maybe UrlFromEnv
-> [RemoteSchemaPermissionMetadata]
-> SchemaRemoteRelationships r
-> RemoteSchemaMetadataG r
RemoteSchemaMetadata
      (RemoteSchemaName
 -> RemoteSchemaDef
 -> Maybe UrlFromEnv
 -> [RemoteSchemaPermissionMetadata]
 -> SchemaRemoteRelationships r
 -> RemoteSchemaMetadataG r)
-> Codec Object (RemoteSchemaMetadataG r) RemoteSchemaName
-> Codec
     Object
     (RemoteSchemaMetadataG r)
     (RemoteSchemaDef
      -> Maybe UrlFromEnv
      -> [RemoteSchemaPermissionMetadata]
      -> SchemaRemoteRelationships r
      -> RemoteSchemaMetadataG r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UrlFromEnv -> ObjectCodec RemoteSchemaName RemoteSchemaName
forall output.
HasCodec output =>
UrlFromEnv -> ObjectCodec output output
requiredField' UrlFromEnv
"name"
      ObjectCodec RemoteSchemaName RemoteSchemaName
-> (RemoteSchemaMetadataG r -> RemoteSchemaName)
-> Codec Object (RemoteSchemaMetadataG r) RemoteSchemaName
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaMetadataG r -> RemoteSchemaName
forall r. RemoteSchemaMetadataG r -> RemoteSchemaName
_rsmName
        Codec
  Object
  (RemoteSchemaMetadataG r)
  (RemoteSchemaDef
   -> Maybe UrlFromEnv
   -> [RemoteSchemaPermissionMetadata]
   -> SchemaRemoteRelationships r
   -> RemoteSchemaMetadataG r)
-> Codec Object (RemoteSchemaMetadataG r) RemoteSchemaDef
-> Codec
     Object
     (RemoteSchemaMetadataG r)
     (Maybe UrlFromEnv
      -> [RemoteSchemaPermissionMetadata]
      -> SchemaRemoteRelationships r
      -> RemoteSchemaMetadataG r)
forall a b.
Codec Object (RemoteSchemaMetadataG r) (a -> b)
-> Codec Object (RemoteSchemaMetadataG r) a
-> Codec Object (RemoteSchemaMetadataG r) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> UrlFromEnv -> ObjectCodec RemoteSchemaDef RemoteSchemaDef
forall output.
HasCodec output =>
UrlFromEnv -> ObjectCodec output output
requiredField' UrlFromEnv
"definition"
      ObjectCodec RemoteSchemaDef RemoteSchemaDef
-> (RemoteSchemaMetadataG r -> RemoteSchemaDef)
-> Codec Object (RemoteSchemaMetadataG r) RemoteSchemaDef
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaMetadataG r -> RemoteSchemaDef
forall r. RemoteSchemaMetadataG r -> RemoteSchemaDef
_rsmDefinition
        Codec
  Object
  (RemoteSchemaMetadataG r)
  (Maybe UrlFromEnv
   -> [RemoteSchemaPermissionMetadata]
   -> SchemaRemoteRelationships r
   -> RemoteSchemaMetadataG r)
-> Codec Object (RemoteSchemaMetadataG r) (Maybe UrlFromEnv)
-> Codec
     Object
     (RemoteSchemaMetadataG r)
     ([RemoteSchemaPermissionMetadata]
      -> SchemaRemoteRelationships r -> RemoteSchemaMetadataG r)
forall a b.
Codec Object (RemoteSchemaMetadataG r) (a -> b)
-> Codec Object (RemoteSchemaMetadataG r) a
-> Codec Object (RemoteSchemaMetadataG r) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> UrlFromEnv -> ObjectCodec (Maybe UrlFromEnv) (Maybe UrlFromEnv)
forall output.
HasCodec output =>
UrlFromEnv -> ObjectCodec (Maybe output) (Maybe output)
optionalField' UrlFromEnv
"comment"
      ObjectCodec (Maybe UrlFromEnv) (Maybe UrlFromEnv)
-> (RemoteSchemaMetadataG r -> Maybe UrlFromEnv)
-> Codec Object (RemoteSchemaMetadataG r) (Maybe UrlFromEnv)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaMetadataG r -> Maybe UrlFromEnv
forall r. RemoteSchemaMetadataG r -> Maybe UrlFromEnv
_rsmComment
        Codec
  Object
  (RemoteSchemaMetadataG r)
  ([RemoteSchemaPermissionMetadata]
   -> SchemaRemoteRelationships r -> RemoteSchemaMetadataG r)
-> Codec
     Object (RemoteSchemaMetadataG r) [RemoteSchemaPermissionMetadata]
-> Codec
     Object
     (RemoteSchemaMetadataG r)
     (SchemaRemoteRelationships r -> RemoteSchemaMetadataG r)
forall a b.
Codec Object (RemoteSchemaMetadataG r) (a -> b)
-> Codec Object (RemoteSchemaMetadataG r) a
-> Codec Object (RemoteSchemaMetadataG r) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> UrlFromEnv
-> [RemoteSchemaPermissionMetadata]
-> ObjectCodec
     [RemoteSchemaPermissionMetadata] [RemoteSchemaPermissionMetadata]
forall output.
HasCodec output =>
UrlFromEnv -> output -> ObjectCodec output output
optionalFieldWithDefault' UrlFromEnv
"permissions" [RemoteSchemaPermissionMetadata]
forall a. Monoid a => a
mempty
      ObjectCodec
  [RemoteSchemaPermissionMetadata] [RemoteSchemaPermissionMetadata]
-> (RemoteSchemaMetadataG r -> [RemoteSchemaPermissionMetadata])
-> Codec
     Object (RemoteSchemaMetadataG r) [RemoteSchemaPermissionMetadata]
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaMetadataG r -> [RemoteSchemaPermissionMetadata]
forall r.
RemoteSchemaMetadataG r -> [RemoteSchemaPermissionMetadata]
_rsmPermissions
        Codec
  Object
  (RemoteSchemaMetadataG r)
  (SchemaRemoteRelationships r -> RemoteSchemaMetadataG r)
-> Codec
     Object (RemoteSchemaMetadataG r) (SchemaRemoteRelationships r)
-> ObjectCodec (RemoteSchemaMetadataG r) (RemoteSchemaMetadataG r)
forall a b.
Codec Object (RemoteSchemaMetadataG r) (a -> b)
-> Codec Object (RemoteSchemaMetadataG r) a
-> Codec Object (RemoteSchemaMetadataG r) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> UrlFromEnv
-> JSONCodec (SchemaRemoteRelationships r)
-> SchemaRemoteRelationships r
-> ObjectCodec
     (SchemaRemoteRelationships r) (SchemaRemoteRelationships r)
forall output.
UrlFromEnv
-> JSONCodec output -> output -> ObjectCodec output output
optionalFieldWithDefaultWith'
          UrlFromEnv
"remote_relationships"
          ((RemoteSchemaTypeRelationships r -> Name)
-> JSONCodec (SchemaRemoteRelationships r)
forall k a.
(Hashable k, HasCodec a, ToTxt k) =>
(a -> k) -> JSONCodec (InsOrdHashMap k a)
insertionOrderedElemsCodec RemoteSchemaTypeRelationships r -> Name
forall r. RemoteSchemaTypeRelationships r -> Name
_rstrsName)
          SchemaRemoteRelationships r
forall a. Monoid a => a
mempty
      ObjectCodec
  (SchemaRemoteRelationships r) (SchemaRemoteRelationships r)
-> (RemoteSchemaMetadataG r -> SchemaRemoteRelationships r)
-> Codec
     Object (RemoteSchemaMetadataG r) (SchemaRemoteRelationships r)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaMetadataG r -> SchemaRemoteRelationships r
forall r. RemoteSchemaMetadataG r -> SchemaRemoteRelationships r
_rsmRemoteRelationships

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

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

$(makeLenses ''RemoteSchemaMetadataG)