module Hasura.RemoteSchema.Metadata.Permission
  ( RemoteSchemaPermissionDefinition (..),
    RemoteSchemaPermissionMetadata (..),
  )
where

import Autodocodec (HasCodec (codec), object, optionalField', requiredField', requiredFieldWith, (.=))
import Autodocodec.Extended (graphQLSchemaDocumentCodec)
import Data.Aeson qualified as J
import Hasura.Prelude
import Hasura.RQL.Types.Roles (RoleName)
import Language.GraphQL.Draft.Printer qualified as G
import Language.GraphQL.Draft.Syntax qualified as G
import Text.Builder qualified as TB

newtype RemoteSchemaPermissionDefinition = RemoteSchemaPermissionDefinition
  { RemoteSchemaPermissionDefinition -> SchemaDocument
_rspdSchema :: G.SchemaDocument
  }
  deriving (Int -> RemoteSchemaPermissionDefinition -> ShowS
[RemoteSchemaPermissionDefinition] -> ShowS
RemoteSchemaPermissionDefinition -> String
(Int -> RemoteSchemaPermissionDefinition -> ShowS)
-> (RemoteSchemaPermissionDefinition -> String)
-> ([RemoteSchemaPermissionDefinition] -> ShowS)
-> Show RemoteSchemaPermissionDefinition
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RemoteSchemaPermissionDefinition -> ShowS
showsPrec :: Int -> RemoteSchemaPermissionDefinition -> ShowS
$cshow :: RemoteSchemaPermissionDefinition -> String
show :: RemoteSchemaPermissionDefinition -> String
$cshowList :: [RemoteSchemaPermissionDefinition] -> ShowS
showList :: [RemoteSchemaPermissionDefinition] -> ShowS
Show, RemoteSchemaPermissionDefinition
-> RemoteSchemaPermissionDefinition -> Bool
(RemoteSchemaPermissionDefinition
 -> RemoteSchemaPermissionDefinition -> Bool)
-> (RemoteSchemaPermissionDefinition
    -> RemoteSchemaPermissionDefinition -> Bool)
-> Eq RemoteSchemaPermissionDefinition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RemoteSchemaPermissionDefinition
-> RemoteSchemaPermissionDefinition -> Bool
== :: RemoteSchemaPermissionDefinition
-> RemoteSchemaPermissionDefinition -> Bool
$c/= :: RemoteSchemaPermissionDefinition
-> RemoteSchemaPermissionDefinition -> Bool
/= :: RemoteSchemaPermissionDefinition
-> RemoteSchemaPermissionDefinition -> Bool
Eq, (forall x.
 RemoteSchemaPermissionDefinition
 -> Rep RemoteSchemaPermissionDefinition x)
-> (forall x.
    Rep RemoteSchemaPermissionDefinition x
    -> RemoteSchemaPermissionDefinition)
-> Generic RemoteSchemaPermissionDefinition
forall x.
Rep RemoteSchemaPermissionDefinition x
-> RemoteSchemaPermissionDefinition
forall x.
RemoteSchemaPermissionDefinition
-> Rep RemoteSchemaPermissionDefinition x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x.
RemoteSchemaPermissionDefinition
-> Rep RemoteSchemaPermissionDefinition x
from :: forall x.
RemoteSchemaPermissionDefinition
-> Rep RemoteSchemaPermissionDefinition x
$cto :: forall x.
Rep RemoteSchemaPermissionDefinition x
-> RemoteSchemaPermissionDefinition
to :: forall x.
Rep RemoteSchemaPermissionDefinition x
-> RemoteSchemaPermissionDefinition
Generic)

instance NFData RemoteSchemaPermissionDefinition

instance Hashable RemoteSchemaPermissionDefinition

instance HasCodec RemoteSchemaPermissionDefinition where
  codec :: JSONCodec RemoteSchemaPermissionDefinition
codec =
    Text
-> ObjectCodec
     RemoteSchemaPermissionDefinition RemoteSchemaPermissionDefinition
-> JSONCodec RemoteSchemaPermissionDefinition
forall input output.
Text -> ObjectCodec input output -> ValueCodec input output
object Text
"RemoteSchemaPermissionDefinition"
      (ObjectCodec
   RemoteSchemaPermissionDefinition RemoteSchemaPermissionDefinition
 -> JSONCodec RemoteSchemaPermissionDefinition)
-> ObjectCodec
     RemoteSchemaPermissionDefinition RemoteSchemaPermissionDefinition
-> JSONCodec RemoteSchemaPermissionDefinition
forall a b. (a -> b) -> a -> b
$ SchemaDocument -> RemoteSchemaPermissionDefinition
RemoteSchemaPermissionDefinition
      (SchemaDocument -> RemoteSchemaPermissionDefinition)
-> Codec Object RemoteSchemaPermissionDefinition SchemaDocument
-> ObjectCodec
     RemoteSchemaPermissionDefinition RemoteSchemaPermissionDefinition
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> ValueCodec SchemaDocument SchemaDocument
-> Text
-> ObjectCodec SchemaDocument SchemaDocument
forall input output.
Text -> ValueCodec input output -> Text -> ObjectCodec input output
requiredFieldWith
        Text
"schema"
        ValueCodec SchemaDocument SchemaDocument
graphQLSchemaDocumentCodec
        Text
"GraphQL schema document, e.g. the content of schema.gql"
      ObjectCodec SchemaDocument SchemaDocument
-> (RemoteSchemaPermissionDefinition -> SchemaDocument)
-> Codec Object RemoteSchemaPermissionDefinition SchemaDocument
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaPermissionDefinition -> SchemaDocument
_rspdSchema

instance J.FromJSON RemoteSchemaPermissionDefinition where
  parseJSON :: Value -> Parser RemoteSchemaPermissionDefinition
parseJSON = String
-> (Object -> Parser RemoteSchemaPermissionDefinition)
-> Value
-> Parser RemoteSchemaPermissionDefinition
forall a. String -> (Object -> Parser a) -> Value -> Parser a
J.withObject String
"RemoteSchemaPermissionDefinition" ((Object -> Parser RemoteSchemaPermissionDefinition)
 -> Value -> Parser RemoteSchemaPermissionDefinition)
-> (Object -> Parser RemoteSchemaPermissionDefinition)
-> Value
-> Parser RemoteSchemaPermissionDefinition
forall a b. (a -> b) -> a -> b
$ \Object
obj -> do
    (SchemaDocument -> RemoteSchemaPermissionDefinition)
-> Parser SchemaDocument -> Parser RemoteSchemaPermissionDefinition
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SchemaDocument -> RemoteSchemaPermissionDefinition
RemoteSchemaPermissionDefinition (Parser SchemaDocument -> Parser RemoteSchemaPermissionDefinition)
-> Parser SchemaDocument -> Parser RemoteSchemaPermissionDefinition
forall a b. (a -> b) -> a -> b
$ Object
obj Object -> Key -> Parser SchemaDocument
forall a. FromJSON a => Object -> Key -> Parser a
J..: Key
"schema"

instance J.ToJSON RemoteSchemaPermissionDefinition where
  toJSON :: RemoteSchemaPermissionDefinition -> Value
toJSON (RemoteSchemaPermissionDefinition SchemaDocument
schema) =
    [Pair] -> Value
J.object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ [Key
"schema" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
J..= Text -> Value
J.String (Builder -> Text
TB.run (Builder -> Text)
-> (SchemaDocument -> Builder) -> SchemaDocument -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SchemaDocument -> Builder
forall a. Printer a => SchemaDocument -> a
G.schemaDocument (SchemaDocument -> Text) -> SchemaDocument -> Text
forall a b. (a -> b) -> a -> b
$ SchemaDocument
schema)]

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
$cshowsPrec :: Int -> RemoteSchemaPermissionMetadata -> ShowS
showsPrec :: Int -> RemoteSchemaPermissionMetadata -> ShowS
$cshow :: RemoteSchemaPermissionMetadata -> String
show :: RemoteSchemaPermissionMetadata -> String
$cshowList :: [RemoteSchemaPermissionMetadata] -> ShowS
showList :: [RemoteSchemaPermissionMetadata] -> ShowS
Show, RemoteSchemaPermissionMetadata
-> RemoteSchemaPermissionMetadata -> Bool
(RemoteSchemaPermissionMetadata
 -> RemoteSchemaPermissionMetadata -> Bool)
-> (RemoteSchemaPermissionMetadata
    -> RemoteSchemaPermissionMetadata -> Bool)
-> Eq RemoteSchemaPermissionMetadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RemoteSchemaPermissionMetadata
-> RemoteSchemaPermissionMetadata -> Bool
== :: RemoteSchemaPermissionMetadata
-> RemoteSchemaPermissionMetadata -> Bool
$c/= :: RemoteSchemaPermissionMetadata
-> RemoteSchemaPermissionMetadata -> Bool
/= :: 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
$cfrom :: forall x.
RemoteSchemaPermissionMetadata
-> Rep RemoteSchemaPermissionMetadata x
from :: forall x.
RemoteSchemaPermissionMetadata
-> Rep RemoteSchemaPermissionMetadata x
$cto :: forall x.
Rep RemoteSchemaPermissionMetadata x
-> RemoteSchemaPermissionMetadata
to :: forall x.
Rep RemoteSchemaPermissionMetadata x
-> RemoteSchemaPermissionMetadata
Generic)

instance HasCodec RemoteSchemaPermissionMetadata where
  codec :: JSONCodec RemoteSchemaPermissionMetadata
codec =
    Text
-> ObjectCodec
     RemoteSchemaPermissionMetadata RemoteSchemaPermissionMetadata
-> JSONCodec RemoteSchemaPermissionMetadata
forall input output.
Text -> ObjectCodec input output -> ValueCodec input output
object Text
"RemoteSchemaPermissionMetadata"
      (ObjectCodec
   RemoteSchemaPermissionMetadata RemoteSchemaPermissionMetadata
 -> JSONCodec RemoteSchemaPermissionMetadata)
-> ObjectCodec
     RemoteSchemaPermissionMetadata RemoteSchemaPermissionMetadata
-> JSONCodec RemoteSchemaPermissionMetadata
forall a b. (a -> b) -> a -> b
$ RoleName
-> RemoteSchemaPermissionDefinition
-> Maybe Text
-> RemoteSchemaPermissionMetadata
RemoteSchemaPermissionMetadata
      (RoleName
 -> RemoteSchemaPermissionDefinition
 -> Maybe Text
 -> RemoteSchemaPermissionMetadata)
-> Codec Object RemoteSchemaPermissionMetadata RoleName
-> Codec
     Object
     RemoteSchemaPermissionMetadata
     (RemoteSchemaPermissionDefinition
      -> Maybe Text -> RemoteSchemaPermissionMetadata)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> ObjectCodec RoleName RoleName
forall output. HasCodec output => Text -> ObjectCodec output output
requiredField' Text
"role"
      ObjectCodec RoleName RoleName
-> (RemoteSchemaPermissionMetadata -> RoleName)
-> Codec Object RemoteSchemaPermissionMetadata RoleName
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaPermissionMetadata -> RoleName
_rspmRole
        Codec
  Object
  RemoteSchemaPermissionMetadata
  (RemoteSchemaPermissionDefinition
   -> Maybe Text -> RemoteSchemaPermissionMetadata)
-> Codec
     Object
     RemoteSchemaPermissionMetadata
     RemoteSchemaPermissionDefinition
-> Codec
     Object
     RemoteSchemaPermissionMetadata
     (Maybe Text -> RemoteSchemaPermissionMetadata)
forall a b.
Codec Object RemoteSchemaPermissionMetadata (a -> b)
-> Codec Object RemoteSchemaPermissionMetadata a
-> Codec Object RemoteSchemaPermissionMetadata b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> ObjectCodec
     RemoteSchemaPermissionDefinition RemoteSchemaPermissionDefinition
forall output. HasCodec output => Text -> ObjectCodec output output
requiredField' Text
"definition"
      ObjectCodec
  RemoteSchemaPermissionDefinition RemoteSchemaPermissionDefinition
-> (RemoteSchemaPermissionMetadata
    -> RemoteSchemaPermissionDefinition)
-> Codec
     Object
     RemoteSchemaPermissionMetadata
     RemoteSchemaPermissionDefinition
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaPermissionMetadata -> RemoteSchemaPermissionDefinition
_rspmDefinition
        Codec
  Object
  RemoteSchemaPermissionMetadata
  (Maybe Text -> RemoteSchemaPermissionMetadata)
-> Codec Object RemoteSchemaPermissionMetadata (Maybe Text)
-> ObjectCodec
     RemoteSchemaPermissionMetadata RemoteSchemaPermissionMetadata
forall a b.
Codec Object RemoteSchemaPermissionMetadata (a -> b)
-> Codec Object RemoteSchemaPermissionMetadata a
-> Codec Object RemoteSchemaPermissionMetadata b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> ObjectCodec (Maybe Text) (Maybe Text)
forall output.
HasCodec output =>
Text -> ObjectCodec (Maybe output) (Maybe output)
optionalField' Text
"comment"
      ObjectCodec (Maybe Text) (Maybe Text)
-> (RemoteSchemaPermissionMetadata -> Maybe Text)
-> Codec Object RemoteSchemaPermissionMetadata (Maybe Text)
forall oldInput output newInput.
ObjectCodec oldInput output
-> (newInput -> oldInput) -> ObjectCodec newInput output
.= RemoteSchemaPermissionMetadata -> Maybe Text
_rspmComment

instance J.FromJSON RemoteSchemaPermissionMetadata where
  parseJSON :: Value -> Parser RemoteSchemaPermissionMetadata
parseJSON = Options -> Value -> Parser RemoteSchemaPermissionMetadata
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
J.genericParseJSON Options
hasuraJSON {omitNothingFields :: Bool
J.omitNothingFields = Bool
True}

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