2021-12-22 03:10:28 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
module Hasura.Backends.DataConnector.IR.Scalar.Value
|
2021-12-22 03:10:28 +03:00
|
|
|
( Value (..),
|
2022-06-02 05:06:45 +03:00
|
|
|
Literal (..),
|
|
|
|
parseValue,
|
2021-12-22 03:10:28 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
import Data.Aeson (FromJSON, ToJSON)
|
2022-06-02 05:06:45 +03:00
|
|
|
import Data.Aeson.Types qualified as J
|
2022-03-31 07:45:03 +03:00
|
|
|
import Data.Scientific
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API qualified as API
|
2022-06-02 05:06:45 +03:00
|
|
|
import Hasura.Backends.DataConnector.IR.Scalar.Type qualified as IR.S.T
|
2021-12-22 03:10:28 +03:00
|
|
|
import Hasura.Incremental (Cacheable)
|
|
|
|
import Hasura.Prelude
|
2022-04-28 04:51:58 +03:00
|
|
|
import Witch qualified
|
2021-12-22 03:10:28 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Literal scalar values that can appear as leaf nodes in expressions
|
|
|
|
--
|
2022-02-25 19:08:18 +03:00
|
|
|
-- NOTE: This type shouldn't _need_ ser/de instances, but they're imposed by
|
|
|
|
-- the 'Backend' class.
|
2021-12-22 03:10:28 +03:00
|
|
|
data Value
|
|
|
|
= String Text
|
2022-03-31 07:45:03 +03:00
|
|
|
| Number Scientific
|
2021-12-22 03:10:28 +03:00
|
|
|
| Boolean Bool
|
|
|
|
| Null
|
|
|
|
deriving stock (Data, Eq, Generic, Ord, Show)
|
|
|
|
deriving anyclass (Cacheable, FromJSON, Hashable, NFData, ToJSON)
|
2022-03-16 07:12:15 +03:00
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
instance Witch.From API.Value Value where
|
2022-03-16 07:12:15 +03:00
|
|
|
from = \case
|
|
|
|
API.String txt -> String txt
|
|
|
|
API.Number x -> Number x
|
|
|
|
API.Boolean p -> Boolean p
|
|
|
|
API.Null -> Null
|
2022-04-08 09:48:37 +03:00
|
|
|
|
2022-04-28 04:51:58 +03:00
|
|
|
instance Witch.From Value API.Value where
|
2022-04-08 09:48:37 +03:00
|
|
|
from = \case
|
|
|
|
String txt -> API.String txt
|
|
|
|
Number x -> API.Number x
|
|
|
|
Boolean p -> API.Boolean p
|
|
|
|
Null -> API.Null
|
2022-06-02 05:06:45 +03:00
|
|
|
|
|
|
|
data Literal
|
|
|
|
= ValueLiteral Value
|
|
|
|
| ArrayLiteral [Value]
|
|
|
|
deriving stock (Eq, Show, Generic, Ord)
|
|
|
|
deriving anyclass (Cacheable, Hashable, NFData, ToJSON)
|
|
|
|
|
|
|
|
parseValue :: IR.S.T.Type -> J.Value -> J.Parser Value
|
|
|
|
parseValue type' val =
|
|
|
|
case (type', val) of
|
|
|
|
(_, J.Null) -> pure Null
|
|
|
|
(IR.S.T.String, value) -> String <$> J.parseJSON value
|
|
|
|
(IR.S.T.Bool, value) -> Boolean <$> J.parseJSON value
|
|
|
|
(IR.S.T.Number, value) -> Number <$> J.parseJSON value
|