{-# OPTIONS_GHC -fno-warn-orphans #-}

module Hasura.Backends.DataConnector.Adapter.Backend
  ( CustomBooleanOperator (..),
    columnTypeToScalarType,
    parseValue,
    lookupGraphQLType,
  )
where

import Control.Lens (view)
import Data.Aeson qualified as J
import Data.Aeson.Extended (ToJSONKeyValue (..))
import Data.Aeson.Key (fromText)
import Data.Aeson.Types qualified as J
import Data.HashMap.Strict qualified as HashMap
import Data.List.NonEmpty qualified as NonEmpty
import Data.Map.Strict (Map)
import Data.Scientific (fromFloatDigits)
import Data.Text qualified as Text
import Data.Text.Casing qualified as C
import Data.Text.Extended ((<<>))
import Hasura.Backends.DataConnector.API qualified as API
import Hasura.Backends.DataConnector.Adapter.Types qualified as DC
import Hasura.Backends.DataConnector.Adapter.Types.Mutations qualified as DC
import Hasura.Base.Error (Code (ValidationFailed), QErr, runAesonParser, throw400)
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp
import Hasura.RQL.Types.Backend (Backend (..), ComputedFieldReturnType, HasSourceConfiguration (..), SupportedNamingCase (..), XDisable, XEnable)
import Hasura.RQL.Types.BackendType (BackendSourceKind (DataConnectorKind), BackendType (DataConnector))
import Hasura.RQL.Types.Column (ColumnType (..))
import Hasura.RQL.Types.ResizePool
import Language.GraphQL.Draft.Syntax qualified as G
import Witch qualified

-- | An alias for '()' indicating that a particular associated type has not yet
-- been implemented for the 'DataConnector' backend.
--
-- '()' is used (rather than a type with an empty data constructor) because it
-- comes with many of the instances that these associated types require.
--
-- This alias should /not/ be exported from this module, and it's only defined
-- for clarity.
type Unimplemented = ()

instance Backend 'DataConnector where
  type BackendConfig 'DataConnector = Map DC.DataConnectorName DC.DataConnectorOptions
  type BackendInfo 'DataConnector = HashMap DC.DataConnectorName DC.DataConnectorInfo

  type TableName 'DataConnector = DC.TableName
  type FunctionName 'DataConnector = DC.FunctionName
  type FunctionReturnType 'DataConnector = DC.FunctionReturnType
  type RawFunctionInfo 'DataConnector = API.FunctionInfo
  type FunctionArgument 'DataConnector = API.FunctionArg
  type ConstraintName 'DataConnector = DC.ConstraintName
  type BasicOrderType 'DataConnector = DC.OrderDirection
  type NullsOrderType 'DataConnector = Unimplemented
  type CountType 'DataConnector = DC.CountAggregate
  type Column 'DataConnector = DC.ColumnName
  type ScalarValue 'DataConnector = J.Value
  type ScalarType 'DataConnector = DC.ScalarType

  -- This does not actually have to be the full IR Expression, in fact it is only
  -- required to represent literals, so we use a special type for that.
  -- The 'SQLExpression' type family should be removed in a future refactor
  type SQLExpression 'DataConnector = DC.Literal
  type ScalarSelectionArguments 'DataConnector = Void
  type BooleanOperators 'DataConnector = CustomBooleanOperator
  type ExtraTableMetadata 'DataConnector = DC.ExtraTableMetadata
  type ComputedFieldDefinition 'DataConnector = Unimplemented
  type FunctionArgumentExp 'DataConnector = DC.ArgumentExp
  type ComputedFieldImplicitArguments 'DataConnector = Unimplemented
  type ComputedFieldReturn 'DataConnector = Unimplemented

  type UpdateVariant 'DataConnector = DC.DataConnectorUpdateVariant
  type BackendInsert 'DataConnector = DC.BackendInsert

  type XComputedField 'DataConnector = XDisable
  type XRelay 'DataConnector = XDisable
  type XNodesAgg 'DataConnector = XEnable
  type XEventTriggers 'DataConnector = XDisable
  type XNestedInserts 'DataConnector = XDisable
  type XStreamingSubscription 'DataConnector = XDisable
  type XNestedObjects 'DataConnector = XEnable

  type HealthCheckTest 'DataConnector = Void

  isComparableType :: ScalarType 'DataConnector -> Bool
  isComparableType :: ScalarType 'DataConnector -> Bool
isComparableType = Bool -> ScalarType -> Bool
forall a b. a -> b -> a
const Bool
False

  isNumType :: ScalarType 'DataConnector -> Bool
  isNumType :: ScalarType 'DataConnector -> Bool
isNumType = Bool -> ScalarType -> Bool
forall a b. a -> b -> a
const Bool
False

  getCustomAggregateOperators :: DC.SourceConfig -> HashMap G.Name (HashMap DC.ScalarType DC.ScalarType)
  getCustomAggregateOperators :: SourceConfig -> HashMap Name (HashMap ScalarType ScalarType)
getCustomAggregateOperators DC.SourceConfig {Maybe Int
Maybe Text
Config
Capabilities
Environment
Manager
BaseUrl
DataConnectorName
_scEndpoint :: BaseUrl
_scConfig :: Config
_scTemplate :: Maybe Text
_scCapabilities :: Capabilities
_scManager :: Manager
_scTimeoutMicroseconds :: Maybe Int
_scDataConnectorName :: DataConnectorName
_scEnvironment :: Environment
_scEndpoint :: SourceConfig -> BaseUrl
_scConfig :: SourceConfig -> Config
_scTemplate :: SourceConfig -> Maybe Text
_scCapabilities :: SourceConfig -> Capabilities
_scManager :: SourceConfig -> Manager
_scTimeoutMicroseconds :: SourceConfig -> Maybe Int
_scDataConnectorName :: SourceConfig -> DataConnectorName
_scEnvironment :: SourceConfig -> Environment
..} =
    (ScalarType
 -> ScalarTypeCapabilities
 -> HashMap Name (HashMap ScalarType ScalarType)
 -> HashMap Name (HashMap ScalarType ScalarType))
-> HashMap Name (HashMap ScalarType ScalarType)
-> HashMap ScalarType ScalarTypeCapabilities
-> HashMap Name (HashMap ScalarType ScalarType)
forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
HashMap.foldrWithKey ScalarType
-> ScalarTypeCapabilities
-> HashMap Name (HashMap ScalarType ScalarType)
-> HashMap Name (HashMap ScalarType ScalarType)
forall {k} {source} {v}.
(Hashable k, From source k, From ScalarType v) =>
source
-> ScalarTypeCapabilities
-> HashMap Name (HashMap k v)
-> HashMap Name (HashMap k v)
insertOps HashMap Name (HashMap ScalarType ScalarType)
forall a. Monoid a => a
mempty HashMap ScalarType ScalarTypeCapabilities
scalarTypesCapabilities
    where
      scalarTypesCapabilities :: HashMap ScalarType ScalarTypeCapabilities
scalarTypesCapabilities = ScalarTypesCapabilities
-> HashMap ScalarType ScalarTypeCapabilities
API.unScalarTypesCapabilities (ScalarTypesCapabilities
 -> HashMap ScalarType ScalarTypeCapabilities)
-> ScalarTypesCapabilities
-> HashMap ScalarType ScalarTypeCapabilities
forall a b. (a -> b) -> a -> b
$ Capabilities -> ScalarTypesCapabilities
API._cScalarTypes Capabilities
_scCapabilities
      insertOps :: source
-> ScalarTypeCapabilities
-> HashMap Name (HashMap k v)
-> HashMap Name (HashMap k v)
insertOps source
typeName API.ScalarTypeCapabilities {Maybe GraphQLType
UpdateColumnOperators
AggregateFunctions
ComparisonOperators
_stcComparisonOperators :: ComparisonOperators
_stcAggregateFunctions :: AggregateFunctions
_stcUpdateColumnOperators :: UpdateColumnOperators
_stcGraphQLType :: Maybe GraphQLType
_stcComparisonOperators :: ScalarTypeCapabilities -> ComparisonOperators
_stcAggregateFunctions :: ScalarTypeCapabilities -> AggregateFunctions
_stcUpdateColumnOperators :: ScalarTypeCapabilities -> UpdateColumnOperators
_stcGraphQLType :: ScalarTypeCapabilities -> Maybe GraphQLType
..} HashMap Name (HashMap k v)
m =
        (Name
 -> ScalarType
 -> HashMap Name (HashMap k v)
 -> HashMap Name (HashMap k v))
-> HashMap Name (HashMap k v)
-> HashMap Name ScalarType
-> HashMap Name (HashMap k v)
forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
HashMap.foldrWithKey Name
-> ScalarType
-> HashMap Name (HashMap k v)
-> HashMap Name (HashMap k v)
insertOp HashMap Name (HashMap k v)
m
          (HashMap Name ScalarType -> HashMap Name (HashMap k v))
-> HashMap Name ScalarType -> HashMap Name (HashMap k v)
forall a b. (a -> b) -> a -> b
$ AggregateFunctions -> HashMap Name ScalarType
API.unAggregateFunctions AggregateFunctions
_stcAggregateFunctions
        where
          insertOp :: Name
-> ScalarType
-> HashMap Name (HashMap k v)
-> HashMap Name (HashMap k v)
insertOp Name
funtionName ScalarType
resultTypeName =
            (HashMap k v -> HashMap k v -> HashMap k v)
-> Name
-> HashMap k v
-> HashMap Name (HashMap k v)
-> HashMap Name (HashMap k v)
forall k v.
(Eq k, Hashable k) =>
(v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v
HashMap.insertWith HashMap k v -> HashMap k v -> HashMap k v
forall k v.
(Eq k, Hashable k) =>
HashMap k v -> HashMap k v -> HashMap k v
HashMap.union Name
funtionName
              (HashMap k v
 -> HashMap Name (HashMap k v) -> HashMap Name (HashMap k v))
-> HashMap k v
-> HashMap Name (HashMap k v)
-> HashMap Name (HashMap k v)
forall a b. (a -> b) -> a -> b
$ k -> v -> HashMap k v
forall k v. Hashable k => k -> v -> HashMap k v
HashMap.singleton
                (source -> k
forall source target. From source target => source -> target
Witch.from source
typeName)
                (ScalarType -> v
forall source target. From source target => source -> target
Witch.from ScalarType
resultTypeName)

  textToScalarValue :: Maybe Text -> ScalarValue 'DataConnector
  textToScalarValue :: Maybe Text -> ScalarValue 'DataConnector
textToScalarValue = String -> Maybe Text -> Value
forall a. HasCallStack => String -> a
error String
"textToScalarValue: not implemented for the Data Connector backend."

  parseScalarValue :: API.ScalarTypesCapabilities -> ScalarType 'DataConnector -> J.Value -> Either QErr (ScalarValue 'DataConnector)
  parseScalarValue :: ScalarTypesCapabilities
-> ScalarType 'DataConnector
-> Value
-> Either QErr (ScalarValue 'DataConnector)
parseScalarValue ScalarTypesCapabilities
ctx ScalarType 'DataConnector
type' Value
value = (Value -> Parser Value) -> Value -> Either QErr Value
forall (m :: * -> *) v a. QErrM m => (v -> Parser a) -> v -> m a
runAesonParser (ScalarTypesCapabilities -> ScalarType -> Value -> Parser Value
parseValue ScalarTypesCapabilities
ctx ScalarType 'DataConnector
ScalarType
type') Value
value

  scalarValueToJSON :: ScalarValue 'DataConnector -> J.Value
  scalarValueToJSON :: ScalarValue 'DataConnector -> Value
scalarValueToJSON = Value -> Value
ScalarValue 'DataConnector -> Value
forall a. a -> a
id

  -- TODO: Fill in this definition for computed fields
  computedFieldFunction :: ComputedFieldDefinition 'DataConnector -> FunctionName 'DataConnector
  computedFieldFunction :: ComputedFieldDefinition 'DataConnector
-> FunctionName 'DataConnector
computedFieldFunction = String -> () -> FunctionName
forall a. HasCallStack => String -> a
error String
"computedFieldFunction: not implemented for the Data Connector backend"

  computedFieldReturnType :: ComputedFieldReturn 'DataConnector -> ComputedFieldReturnType 'DataConnector
  computedFieldReturnType :: ComputedFieldReturn 'DataConnector
-> ComputedFieldReturnType 'DataConnector
computedFieldReturnType = String -> () -> ComputedFieldReturnType 'DataConnector
forall a. HasCallStack => String -> a
error String
"computedFieldReturnType: not implemented for the Data Connector backend"

  fromComputedFieldImplicitArguments :: v -> ComputedFieldImplicitArguments 'DataConnector -> [FunctionArgumentExp 'DataConnector v]
  fromComputedFieldImplicitArguments :: forall v.
v
-> ComputedFieldImplicitArguments 'DataConnector
-> [FunctionArgumentExp 'DataConnector v]
fromComputedFieldImplicitArguments = String -> v -> () -> [ArgumentExp v]
forall a. HasCallStack => String -> a
error String
"fromComputedFieldImplicitArguments: not implemented for the Data Connector backend"

  -- phil said this was cursed
  tableToFunction :: TableName 'DataConnector -> FunctionName 'DataConnector
  tableToFunction :: TableName 'DataConnector -> FunctionName 'DataConnector
tableToFunction = TableName 'DataConnector -> FunctionName 'DataConnector
TableName -> FunctionName
forall a b. Coercible a b => a -> b
coerce

  functionToTable :: FunctionName 'DataConnector -> TableName 'DataConnector
  functionToTable :: FunctionName 'DataConnector -> TableName 'DataConnector
functionToTable = FunctionName 'DataConnector -> TableName 'DataConnector
FunctionName -> TableName
forall a b. Coercible a b => a -> b
coerce

  tableGraphQLName :: TableName 'DataConnector -> Either QErr G.Name
  tableGraphQLName :: TableName 'DataConnector -> Either QErr Name
tableGraphQLName TableName 'DataConnector
name = do
    let snakedName :: Text
snakedName = forall (b :: BackendType). Backend b => TableName b -> Text
snakeCaseTableName @'DataConnector TableName 'DataConnector
name
    Text -> Maybe Name
G.mkName Text
snakedName
      Maybe Name -> Either QErr Name -> Either QErr Name
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Code -> Text -> Either QErr Name
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
ValidationFailed (Text
"TableName " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
snakedName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" is not a valid GraphQL identifier")

  functionGraphQLName :: FunctionName 'DataConnector -> Either QErr G.Name
  functionGraphQLName :: FunctionName 'DataConnector -> Either QErr Name
functionGraphQLName FunctionName 'DataConnector
name = do
    let snakedName :: Text
snakedName = forall (b :: BackendType). Backend b => TableName b -> Text
snakeCaseTableName @'DataConnector (FunctionName -> TableName
forall a b. Coercible a b => a -> b
coerce FunctionName 'DataConnector
FunctionName
name)
    Text -> Maybe Name
G.mkName Text
snakedName
      Maybe Name -> Either QErr Name -> Either QErr Name
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Code -> Text -> Either QErr Name
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
ValidationFailed (Text
"FunctionName " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
snakedName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" is not a valid GraphQL name")

  snakeCaseTableName :: TableName 'DataConnector -> Text
  snakeCaseTableName :: TableName 'DataConnector -> Text
snakeCaseTableName = Text -> [Text] -> Text
Text.intercalate Text
"_" ([Text] -> Text) -> (TableName -> [Text]) -> TableName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty Text -> [Text]
forall a. NonEmpty a -> [a]
NonEmpty.toList (NonEmpty Text -> [Text])
-> (TableName -> NonEmpty Text) -> TableName -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableName -> NonEmpty Text
DC.unTableName

  getTableIdentifier :: TableName 'DataConnector -> Either QErr C.GQLNameIdentifier
  getTableIdentifier :: TableName 'DataConnector -> Either QErr GQLNameIdentifier
getTableIdentifier name :: TableName 'DataConnector
name@(DC.TableName (Text
prefix :| [Text]
suffixes)) =
    let identifier :: Maybe GQLNameIdentifier
identifier = do
          Name
namePrefix <- Text -> Maybe Name
G.mkName Text
prefix
          [NameSuffix]
nameSuffixes <- (Text -> Maybe NameSuffix) -> [Text] -> Maybe [NameSuffix]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse Text -> Maybe NameSuffix
G.mkNameSuffix [Text]
suffixes
          GQLNameIdentifier -> Maybe GQLNameIdentifier
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GQLNameIdentifier -> Maybe GQLNameIdentifier)
-> GQLNameIdentifier -> Maybe GQLNameIdentifier
forall a b. (a -> b) -> a -> b
$ (Name, [NameSuffix]) -> GQLNameIdentifier
C.fromAutogeneratedTuple (Name
namePrefix, [NameSuffix]
nameSuffixes)
     in Maybe GQLNameIdentifier
identifier
          Maybe GQLNameIdentifier
-> Either QErr GQLNameIdentifier -> Either QErr GQLNameIdentifier
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Code -> Text -> Either QErr GQLNameIdentifier
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
ValidationFailed (Text
"TableName " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TableName 'DataConnector
TableName
name TableName -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" is not a valid GraphQL identifier")

  namingConventionSupport :: SupportedNamingCase
  namingConventionSupport :: SupportedNamingCase
namingConventionSupport = SupportedNamingCase
OnlyHasuraCase

  resizeSourcePools :: SourceConfig 'DataConnector -> ServerReplicas -> IO SourceResizePoolSummary
  resizeSourcePools :: SourceConfig 'DataConnector
-> ServerReplicas -> IO SourceResizePoolSummary
resizeSourcePools SourceConfig 'DataConnector
_sourceConfig ServerReplicas
_serverReplicas =
    -- Data connectors do not have concept of connection pools
    SourceResizePoolSummary -> IO SourceResizePoolSummary
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure SourceResizePoolSummary
noPoolsResizedSummary

  defaultTriggerOnReplication :: Maybe (XEventTriggers 'DataConnector, TriggerOnReplication)
defaultTriggerOnReplication = Maybe (Void, TriggerOnReplication)
Maybe (XEventTriggers 'DataConnector, TriggerOnReplication)
forall a. Maybe a
Nothing

  backendSupportsNestedObjects :: Either QErr (XNestedObjects 'DataConnector)
backendSupportsNestedObjects = () -> Either QErr ()
forall a. a -> Either QErr a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

  sourceSupportsSchemalessTables :: SourceConfig 'DataConnector -> Bool
sourceSupportsSchemalessTables =
    Getting Bool SourceConfig Bool
-> SourceConfig 'DataConnector -> Bool
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (Getting Bool SourceConfig Bool
 -> SourceConfig 'DataConnector -> Bool)
-> Getting Bool SourceConfig Bool
-> SourceConfig 'DataConnector
-> Bool
forall a b. (a -> b) -> a -> b
$ (Capabilities -> Const Bool Capabilities)
-> SourceConfig -> Const Bool SourceConfig
Lens' SourceConfig Capabilities
DC.scCapabilities ((Capabilities -> Const Bool Capabilities)
 -> SourceConfig -> Const Bool SourceConfig)
-> ((Bool -> Const Bool Bool)
    -> Capabilities -> Const Bool Capabilities)
-> Getting Bool SourceConfig Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DataSchemaCapabilities -> Const Bool DataSchemaCapabilities)
-> Capabilities -> Const Bool Capabilities
Lens' Capabilities DataSchemaCapabilities
API.cDataSchema ((DataSchemaCapabilities -> Const Bool DataSchemaCapabilities)
 -> Capabilities -> Const Bool Capabilities)
-> ((Bool -> Const Bool Bool)
    -> DataSchemaCapabilities -> Const Bool DataSchemaCapabilities)
-> (Bool -> Const Bool Bool)
-> Capabilities
-> Const Bool Capabilities
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> Const Bool Bool)
-> DataSchemaCapabilities -> Const Bool DataSchemaCapabilities
Lens' DataSchemaCapabilities Bool
API.dscSupportsSchemalessTables

instance HasSourceConfiguration 'DataConnector where
  type SourceConfig 'DataConnector = DC.SourceConfig
  type SourceConnConfiguration 'DataConnector = DC.ConnSourceConfig
  type ScalarTypeParsingContext 'DataConnector = API.ScalarTypesCapabilities

  sourceConfigNumReadReplicas :: SourceConfig 'DataConnector -> Int
sourceConfigNumReadReplicas = Int -> SourceConfig -> Int
forall a b. a -> b -> a
const Int
0 -- not supported
  sourceConfigConnectonTemplateEnabled :: SourceConfig 'DataConnector -> Bool
sourceConfigConnectonTemplateEnabled = Bool -> SourceConfig -> Bool
forall a b. a -> b -> a
const Bool
False -- not supported
  sourceConfigBackendSourceKind :: SourceConfig 'DataConnector -> BackendSourceKind 'DataConnector
sourceConfigBackendSourceKind DC.SourceConfig {Maybe Int
Maybe Text
Config
Capabilities
Environment
Manager
BaseUrl
DataConnectorName
_scEndpoint :: SourceConfig -> BaseUrl
_scConfig :: SourceConfig -> Config
_scTemplate :: SourceConfig -> Maybe Text
_scCapabilities :: SourceConfig -> Capabilities
_scManager :: SourceConfig -> Manager
_scTimeoutMicroseconds :: SourceConfig -> Maybe Int
_scDataConnectorName :: SourceConfig -> DataConnectorName
_scEnvironment :: SourceConfig -> Environment
_scEndpoint :: BaseUrl
_scConfig :: Config
_scTemplate :: Maybe Text
_scCapabilities :: Capabilities
_scManager :: Manager
_scTimeoutMicroseconds :: Maybe Int
_scDataConnectorName :: DataConnectorName
_scEnvironment :: Environment
..} = DataConnectorName -> BackendSourceKind 'DataConnector
DataConnectorKind DataConnectorName
_scDataConnectorName

data CustomBooleanOperator a = CustomBooleanOperator
  { forall a. CustomBooleanOperator a -> Text
_cboName :: Text,
    forall a.
CustomBooleanOperator a
-> Maybe (Either (RootOrCurrentColumn 'DataConnector) a)
_cboRHS :: Maybe (Either (RootOrCurrentColumn 'DataConnector) a) -- TODO turn Either into a specific type
  }
  deriving stock (CustomBooleanOperator a -> CustomBooleanOperator a -> Bool
(CustomBooleanOperator a -> CustomBooleanOperator a -> Bool)
-> (CustomBooleanOperator a -> CustomBooleanOperator a -> Bool)
-> Eq (CustomBooleanOperator a)
forall a.
Eq a =>
CustomBooleanOperator a -> CustomBooleanOperator a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a.
Eq a =>
CustomBooleanOperator a -> CustomBooleanOperator a -> Bool
== :: CustomBooleanOperator a -> CustomBooleanOperator a -> Bool
$c/= :: forall a.
Eq a =>
CustomBooleanOperator a -> CustomBooleanOperator a -> Bool
/= :: CustomBooleanOperator a -> CustomBooleanOperator a -> Bool
Eq, (forall x.
 CustomBooleanOperator a -> Rep (CustomBooleanOperator a) x)
-> (forall x.
    Rep (CustomBooleanOperator a) x -> CustomBooleanOperator a)
-> Generic (CustomBooleanOperator a)
forall x.
Rep (CustomBooleanOperator a) x -> CustomBooleanOperator a
forall x.
CustomBooleanOperator a -> Rep (CustomBooleanOperator a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x.
Rep (CustomBooleanOperator a) x -> CustomBooleanOperator a
forall a x.
CustomBooleanOperator a -> Rep (CustomBooleanOperator a) x
$cfrom :: forall a x.
CustomBooleanOperator a -> Rep (CustomBooleanOperator a) x
from :: forall x.
CustomBooleanOperator a -> Rep (CustomBooleanOperator a) x
$cto :: forall a x.
Rep (CustomBooleanOperator a) x -> CustomBooleanOperator a
to :: forall x.
Rep (CustomBooleanOperator a) x -> CustomBooleanOperator a
Generic, (forall m. Monoid m => CustomBooleanOperator m -> m)
-> (forall m a.
    Monoid m =>
    (a -> m) -> CustomBooleanOperator a -> m)
-> (forall m a.
    Monoid m =>
    (a -> m) -> CustomBooleanOperator a -> m)
-> (forall a b. (a -> b -> b) -> b -> CustomBooleanOperator a -> b)
-> (forall a b. (a -> b -> b) -> b -> CustomBooleanOperator a -> b)
-> (forall b a. (b -> a -> b) -> b -> CustomBooleanOperator a -> b)
-> (forall b a. (b -> a -> b) -> b -> CustomBooleanOperator a -> b)
-> (forall a. (a -> a -> a) -> CustomBooleanOperator a -> a)
-> (forall a. (a -> a -> a) -> CustomBooleanOperator a -> a)
-> (forall a. CustomBooleanOperator a -> [a])
-> (forall a. CustomBooleanOperator a -> Bool)
-> (forall a. CustomBooleanOperator a -> Int)
-> (forall a. Eq a => a -> CustomBooleanOperator a -> Bool)
-> (forall a. Ord a => CustomBooleanOperator a -> a)
-> (forall a. Ord a => CustomBooleanOperator a -> a)
-> (forall a. Num a => CustomBooleanOperator a -> a)
-> (forall a. Num a => CustomBooleanOperator a -> a)
-> Foldable CustomBooleanOperator
forall a. Eq a => a -> CustomBooleanOperator a -> Bool
forall a. Num a => CustomBooleanOperator a -> a
forall a. Ord a => CustomBooleanOperator a -> a
forall m. Monoid m => CustomBooleanOperator m -> m
forall a. CustomBooleanOperator a -> Bool
forall a. CustomBooleanOperator a -> Int
forall a. CustomBooleanOperator a -> [a]
forall a. (a -> a -> a) -> CustomBooleanOperator a -> a
forall m a. Monoid m => (a -> m) -> CustomBooleanOperator a -> m
forall b a. (b -> a -> b) -> b -> CustomBooleanOperator a -> b
forall a b. (a -> b -> b) -> b -> CustomBooleanOperator a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => CustomBooleanOperator m -> m
fold :: forall m. Monoid m => CustomBooleanOperator m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> CustomBooleanOperator a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> CustomBooleanOperator a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> CustomBooleanOperator a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> CustomBooleanOperator a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> CustomBooleanOperator a -> b
foldr :: forall a b. (a -> b -> b) -> b -> CustomBooleanOperator a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> CustomBooleanOperator a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> CustomBooleanOperator a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> CustomBooleanOperator a -> b
foldl :: forall b a. (b -> a -> b) -> b -> CustomBooleanOperator a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> CustomBooleanOperator a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> CustomBooleanOperator a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> CustomBooleanOperator a -> a
foldr1 :: forall a. (a -> a -> a) -> CustomBooleanOperator a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> CustomBooleanOperator a -> a
foldl1 :: forall a. (a -> a -> a) -> CustomBooleanOperator a -> a
$ctoList :: forall a. CustomBooleanOperator a -> [a]
toList :: forall a. CustomBooleanOperator a -> [a]
$cnull :: forall a. CustomBooleanOperator a -> Bool
null :: forall a. CustomBooleanOperator a -> Bool
$clength :: forall a. CustomBooleanOperator a -> Int
length :: forall a. CustomBooleanOperator a -> Int
$celem :: forall a. Eq a => a -> CustomBooleanOperator a -> Bool
elem :: forall a. Eq a => a -> CustomBooleanOperator a -> Bool
$cmaximum :: forall a. Ord a => CustomBooleanOperator a -> a
maximum :: forall a. Ord a => CustomBooleanOperator a -> a
$cminimum :: forall a. Ord a => CustomBooleanOperator a -> a
minimum :: forall a. Ord a => CustomBooleanOperator a -> a
$csum :: forall a. Num a => CustomBooleanOperator a -> a
sum :: forall a. Num a => CustomBooleanOperator a -> a
$cproduct :: forall a. Num a => CustomBooleanOperator a -> a
product :: forall a. Num a => CustomBooleanOperator a -> a
Foldable, (forall a b.
 (a -> b) -> CustomBooleanOperator a -> CustomBooleanOperator b)
-> (forall a b.
    a -> CustomBooleanOperator b -> CustomBooleanOperator a)
-> Functor CustomBooleanOperator
forall a b. a -> CustomBooleanOperator b -> CustomBooleanOperator a
forall a b.
(a -> b) -> CustomBooleanOperator a -> CustomBooleanOperator b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b.
(a -> b) -> CustomBooleanOperator a -> CustomBooleanOperator b
fmap :: forall a b.
(a -> b) -> CustomBooleanOperator a -> CustomBooleanOperator b
$c<$ :: forall a b. a -> CustomBooleanOperator b -> CustomBooleanOperator a
<$ :: forall a b. a -> CustomBooleanOperator b -> CustomBooleanOperator a
Functor, Functor CustomBooleanOperator
Foldable CustomBooleanOperator
Functor CustomBooleanOperator
-> Foldable CustomBooleanOperator
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b)
    -> CustomBooleanOperator a -> f (CustomBooleanOperator b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    CustomBooleanOperator (f a) -> f (CustomBooleanOperator a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b)
    -> CustomBooleanOperator a -> m (CustomBooleanOperator b))
-> (forall (m :: * -> *) a.
    Monad m =>
    CustomBooleanOperator (m a) -> m (CustomBooleanOperator a))
-> Traversable CustomBooleanOperator
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
CustomBooleanOperator (m a) -> m (CustomBooleanOperator a)
forall (f :: * -> *) a.
Applicative f =>
CustomBooleanOperator (f a) -> f (CustomBooleanOperator a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b)
-> CustomBooleanOperator a -> m (CustomBooleanOperator b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b)
-> CustomBooleanOperator a -> f (CustomBooleanOperator b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b)
-> CustomBooleanOperator a -> f (CustomBooleanOperator b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b)
-> CustomBooleanOperator a -> f (CustomBooleanOperator b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
CustomBooleanOperator (f a) -> f (CustomBooleanOperator a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
CustomBooleanOperator (f a) -> f (CustomBooleanOperator a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b)
-> CustomBooleanOperator a -> m (CustomBooleanOperator b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b)
-> CustomBooleanOperator a -> m (CustomBooleanOperator b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
CustomBooleanOperator (m a) -> m (CustomBooleanOperator a)
sequence :: forall (m :: * -> *) a.
Monad m =>
CustomBooleanOperator (m a) -> m (CustomBooleanOperator a)
Traversable, Int -> CustomBooleanOperator a -> ShowS
[CustomBooleanOperator a] -> ShowS
CustomBooleanOperator a -> String
(Int -> CustomBooleanOperator a -> ShowS)
-> (CustomBooleanOperator a -> String)
-> ([CustomBooleanOperator a] -> ShowS)
-> Show (CustomBooleanOperator a)
forall a. Show a => Int -> CustomBooleanOperator a -> ShowS
forall a. Show a => [CustomBooleanOperator a] -> ShowS
forall a. Show a => CustomBooleanOperator a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> CustomBooleanOperator a -> ShowS
showsPrec :: Int -> CustomBooleanOperator a -> ShowS
$cshow :: forall a. Show a => CustomBooleanOperator a -> String
show :: CustomBooleanOperator a -> String
$cshowList :: forall a. Show a => [CustomBooleanOperator a] -> ShowS
showList :: [CustomBooleanOperator a] -> ShowS
Show)

instance (NFData a) => NFData (CustomBooleanOperator a)

instance (Hashable a) => Hashable (CustomBooleanOperator a)

instance (J.ToJSON a) => ToJSONKeyValue (CustomBooleanOperator a) where
  toJSONKeyValue :: CustomBooleanOperator a -> (Key, Value)
toJSONKeyValue CustomBooleanOperator {Maybe (Either (RootOrCurrentColumn 'DataConnector) a)
Text
_cboName :: forall a. CustomBooleanOperator a -> Text
_cboRHS :: forall a.
CustomBooleanOperator a
-> Maybe (Either (RootOrCurrentColumn 'DataConnector) a)
_cboName :: Text
_cboRHS :: Maybe (Either (RootOrCurrentColumn 'DataConnector) a)
..} = (Text -> Key
fromText Text
_cboName, Maybe (Either (RootOrCurrentColumn 'DataConnector) a) -> Value
forall a. ToJSON a => a -> Value
J.toJSON Maybe (Either (RootOrCurrentColumn 'DataConnector) a)
_cboRHS)

parseValue :: API.ScalarTypesCapabilities -> DC.ScalarType -> J.Value -> J.Parser J.Value
parseValue :: ScalarTypesCapabilities -> ScalarType -> Value -> Parser Value
parseValue ScalarTypesCapabilities
ctx ScalarType
type' Value
val =
  case Value
val of
    Value
J.Null -> Value -> Parser Value
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
J.Null
    Value
value -> case ScalarTypesCapabilities -> ScalarType -> Maybe GraphQLType
lookupGraphQLType ScalarTypesCapabilities
ctx ScalarType
type' of
      Maybe GraphQLType
Nothing -> Value -> Parser Value
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
value
      Just GraphQLType
DC.GraphQLInt -> (Scientific -> Value
J.Number (Scientific -> Value) -> (Int -> Scientific) -> Int -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral) (Int -> Value) -> Parser Int -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Value -> Parser a
J.parseJSON @Int Value
value
      Just GraphQLType
DC.GraphQLFloat -> (Scientific -> Value
J.Number (Scientific -> Value) -> (Double -> Scientific) -> Double -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Scientific
forall a. RealFloat a => a -> Scientific
fromFloatDigits) (Double -> Value) -> Parser Double -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Value -> Parser a
J.parseJSON @Double Value
value
      Just GraphQLType
DC.GraphQLString -> Text -> Value
J.String (Text -> Value) -> Parser Text -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Text
forall a. FromJSON a => Value -> Parser a
J.parseJSON Value
value
      Just GraphQLType
DC.GraphQLBoolean -> Bool -> Value
J.Bool (Bool -> Value) -> Parser Bool -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Bool
forall a. FromJSON a => Value -> Parser a
J.parseJSON Value
value
      Just GraphQLType
DC.GraphQLID -> Text -> Value
J.String (Text -> Value) -> Parser Text -> Parser Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Text
parseID Value
value
  where
    parseID :: Value -> Parser Text
parseID Value
value = forall a. FromJSON a => Value -> Parser a
J.parseJSON @Text Value
value Parser Text -> Parser Text -> Parser Text
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Int -> Text
forall a. Show a => a -> Text
tshow (Int -> Text) -> Parser Int -> Parser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Value -> Parser a
J.parseJSON @Int Value
value

lookupGraphQLType :: API.ScalarTypesCapabilities -> DC.ScalarType -> Maybe DC.GraphQLType
lookupGraphQLType :: ScalarTypesCapabilities -> ScalarType -> Maybe GraphQLType
lookupGraphQLType ScalarTypesCapabilities
capabilities ScalarType
type' =
  ScalarTypeCapabilities -> Maybe GraphQLType
API._stcGraphQLType (ScalarTypeCapabilities -> Maybe GraphQLType)
-> Maybe ScalarTypeCapabilities -> Maybe GraphQLType
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ScalarType
-> HashMap ScalarType ScalarTypeCapabilities
-> Maybe ScalarTypeCapabilities
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (ScalarType -> ScalarType
forall source target. From source target => source -> target
Witch.from ScalarType
type') (ScalarTypesCapabilities
-> HashMap ScalarType ScalarTypeCapabilities
API.unScalarTypesCapabilities ScalarTypesCapabilities
capabilities)

columnTypeToScalarType :: ColumnType 'DataConnector -> DC.ScalarType
columnTypeToScalarType :: ColumnType 'DataConnector -> ScalarType
columnTypeToScalarType = \case
  ColumnScalar ScalarType 'DataConnector
scalarType -> ScalarType 'DataConnector
ScalarType
scalarType
  -- Data connectors does not yet support enum tables.
  -- If/when we add this support, we probably want to
  -- embed the enum scalar type name within the `EnumReference` record type
  ColumnEnumReference EnumReference 'DataConnector
_ -> String -> ScalarType
forall a. HasCallStack => String -> a
error String
"columnTypeToScalarType got enum"