{-# LANGUAGE MultiParamTypeClasses, TypeFamilies, FlexibleContexts, AllowAmbiguousTypes, FlexibleInstances, IncoherentInstances, UndecidableInstances, PolyKinds, BlockArguments, DataKinds #-}
module IHP.Controller.Param
(
param
, paramOrNothing
, paramOrDefault
, paramOrError
, paramList
, paramListOrNothing
, hasParam
, queryOrBodyParam
, allParams
, paramText
, paramInt
, paramBool
, paramUUID
, ParamReader (..)
, ParamException (..)
, enumParamReader
, enumParamReaderJSON
, FillParams (..)
, ifValid
, ifNew
, emptyValueToNothing
) where
import IHP.Prelude
import Network.Wai (Request)
import qualified IHP.ModelSupport as ModelSupport
import IHP.ValidationSupport
import qualified Data.Attoparsec.ByteString.Char8 as Attoparsec
import qualified Data.Aeson as Aeson
import IHP.RequestVault ()
import qualified Control.DeepSeq as DeepSeq
import Text.Read (readMaybe)
import qualified Wai.Request.Params as Params
import Wai.Request.Params (ParamReader(..), ParamException(..), enumParamReaderJSON)
param :: (?request :: Request) => (ParamReader valueType) => ByteString -> valueType
param :: forall valueType.
(?request::Request, ParamReader valueType) =>
ByteString -> valueType
param !ByteString
name = RequestBody -> Request -> ByteString -> valueType
forall valueType.
ParamReader valueType =>
RequestBody -> Request -> ByteString -> valueType
Params.param ?request::Request
Request
?request.parsedBody ?request::Request
Request
?request ByteString
name
{-# INLINABLE param #-}
paramList :: forall valueType. (?request :: Request, DeepSeq.NFData valueType, ParamReader valueType) => ByteString -> [valueType]
paramList :: forall valueType.
(?request::Request, NFData valueType, ParamReader valueType) =>
ByteString -> [valueType]
paramList ByteString
name = RequestBody -> Request -> ByteString -> [valueType]
forall valueType.
(NFData valueType, ParamReader valueType) =>
RequestBody -> Request -> ByteString -> [valueType]
Params.paramList ?request::Request
Request
?request.parsedBody ?request::Request
Request
?request ByteString
name
{-# INLINABLE paramList #-}
paramListOrNothing :: forall valueType. (?request :: Request, DeepSeq.NFData valueType, ParamReader valueType) => ByteString -> [Maybe valueType]
paramListOrNothing :: forall valueType.
(?request::Request, NFData valueType, ParamReader valueType) =>
ByteString -> [Maybe valueType]
paramListOrNothing ByteString
name = RequestBody -> Request -> ByteString -> [Maybe valueType]
forall valueType.
(NFData valueType, ParamReader valueType) =>
RequestBody -> Request -> ByteString -> [Maybe valueType]
Params.paramListOrNothing ?request::Request
Request
?request.parsedBody ?request::Request
Request
?request ByteString
name
{-# INLINABLE paramListOrNothing #-}
paramText :: (?request :: Request) => ByteString -> Text
paramText :: (?request::Request) => ByteString -> Text
paramText = forall valueType.
(?request::Request, ParamReader valueType) =>
ByteString -> valueType
param @Text
paramInt :: (?request :: Request) => ByteString -> Int
paramInt :: (?request::Request) => ByteString -> Int
paramInt = forall valueType.
(?request::Request, ParamReader valueType) =>
ByteString -> valueType
param @Int
paramBool :: (?request :: Request) => ByteString -> Bool
paramBool :: (?request::Request) => ByteString -> Bool
paramBool = forall valueType.
(?request::Request, ParamReader valueType) =>
ByteString -> valueType
param @Bool
paramUUID :: (?request :: Request) => ByteString -> UUID
paramUUID :: (?request::Request) => ByteString -> UUID
paramUUID = forall valueType.
(?request::Request, ParamReader valueType) =>
ByteString -> valueType
param @UUID
hasParam :: (?request :: Request) => ByteString -> Bool
hasParam :: (?request::Request) => ByteString -> Bool
hasParam = RequestBody -> Request -> ByteString -> Bool
Params.hasParam ?request::Request
Request
?request.parsedBody ?request::Request
Request
?request
{-# INLINABLE hasParam #-}
paramOrDefault :: (?request :: Request) => ParamReader a => a -> ByteString -> a
paramOrDefault :: forall a.
(?request::Request, ParamReader a) =>
a -> ByteString -> a
paramOrDefault !a
defaultValue ByteString
name = RequestBody -> Request -> a -> ByteString -> a
forall a.
ParamReader a =>
RequestBody -> Request -> a -> ByteString -> a
Params.paramOrDefault ?request::Request
Request
?request.parsedBody ?request::Request
Request
?request a
defaultValue ByteString
name
{-# INLINABLE paramOrDefault #-}
paramOrNothing :: forall paramType. (?request :: Request) => ParamReader (Maybe paramType) => ByteString -> Maybe paramType
paramOrNothing :: forall paramType.
(?request::Request, ParamReader (Maybe paramType)) =>
ByteString -> Maybe paramType
paramOrNothing !ByteString
name = RequestBody -> Request -> ByteString -> Maybe paramType
forall paramType.
ParamReader (Maybe paramType) =>
RequestBody -> Request -> ByteString -> Maybe paramType
Params.paramOrNothing ?request::Request
Request
?request.parsedBody ?request::Request
Request
?request ByteString
name
{-# INLINABLE paramOrNothing #-}
paramOrError :: forall paramType. (?request :: Request) => ParamReader paramType => ByteString -> Either ParamException paramType
paramOrError :: forall paramType.
(?request::Request, ParamReader paramType) =>
ByteString -> Either ParamException paramType
paramOrError !ByteString
name = RequestBody
-> Request -> ByteString -> Either ParamException paramType
forall paramType.
ParamReader paramType =>
RequestBody
-> Request -> ByteString -> Either ParamException paramType
Params.paramOrError ?request::Request
Request
?request.parsedBody ?request::Request
Request
?request ByteString
name
{-# INLINABLE paramOrError #-}
queryOrBodyParam :: (?request :: Request) => ByteString -> Maybe ByteString
queryOrBodyParam :: (?request::Request) => ByteString -> Maybe ByteString
queryOrBodyParam !ByteString
name = RequestBody -> Request -> ByteString -> Maybe ByteString
Params.queryOrBodyParam ?request::Request
Request
?request.parsedBody ?request::Request
Request
?request ByteString
name
{-# INLINABLE queryOrBodyParam #-}
allParams :: (?request :: Request) => [(ByteString, Maybe ByteString)]
allParams :: (?request::Request) => [(ByteString, Maybe ByteString)]
allParams = RequestBody -> Request -> [(ByteString, Maybe ByteString)]
Params.allParams ?request::Request
Request
?request.parsedBody ?request::Request
Request
?request
instance ParamReader ModelSupport.Point where
{-# INLINABLE readParameter #-}
readParameter :: ByteString -> Either ByteString Point
readParameter ByteString
byteString =
case Parser Point -> ByteString -> Either String Point
forall a. Parser a -> ByteString -> Either String a
Attoparsec.parseOnly (do x <- Parser Double
Attoparsec.double; Attoparsec.char ','; y <- Attoparsec.double; Attoparsec.endOfInput; pure (ModelSupport.fromCoordinates x y)) ByteString
byteString of
Right Point
value -> Point -> Either ByteString Point
forall a b. b -> Either a b
Right Point
value
Left String
error -> ByteString -> Either ByteString Point
forall a b. a -> Either a b
Left ByteString
"has to be two numbers with a comma, e.g. '1,2'"
readParameterJSON :: Value -> Either ByteString Point
readParameterJSON (Aeson.String Text
string) = let ByteString
byteString :: ByteString = Text -> ByteString
forall a b. ConvertibleStrings a b => a -> b
cs Text
string in ByteString -> Either ByteString Point
forall a. ParamReader a => ByteString -> Either ByteString a
readParameter ByteString
byteString
readParameterJSON Value
_ = ByteString -> Either ByteString Point
forall a b. a -> Either a b
Left ByteString
"Expected Point"
instance ParamReader ModelSupport.Interval where
{-# INLINABLE readParameter #-}
readParameter :: ByteString -> Either ByteString Interval
readParameter ByteString
byteString = case String -> Maybe Interval
forall a. Read a => String -> Maybe a
readMaybe (ByteString -> String
forall a b. ConvertibleStrings a b => a -> b
cs ByteString
byteString) of
Just Interval
interval -> Interval -> Either ByteString Interval
forall a b. b -> Either a b
Right Interval
interval
Maybe Interval
Nothing -> ByteString -> Either ByteString Interval
forall a b. a -> Either a b
Left ByteString
"Invalid interval"
readParameterJSON :: Value -> Either ByteString Interval
readParameterJSON (Aeson.String Text
string) = case String -> Maybe Interval
forall a. Read a => String -> Maybe a
readMaybe (Text -> String
forall a b. ConvertibleStrings a b => a -> b
cs Text
string) of
Just Interval
interval -> Interval -> Either ByteString Interval
forall a b. b -> Either a b
Right Interval
interval
Maybe Interval
Nothing -> ByteString -> Either ByteString Interval
forall a b. a -> Either a b
Left ByteString
"Invalid interval"
readParameterJSON Value
_ = ByteString -> Either ByteString Interval
forall a b. a -> Either a b
Left ByteString
"Expected String"
instance ParamReader ModelSupport.Inet where
{-# INLINABLE readParameter #-}
readParameter :: ByteString -> Either ByteString Inet
readParameter ByteString
byteString = case String -> Maybe Inet
forall a. Read a => String -> Maybe a
readMaybe (ByteString -> String
forall a b. ConvertibleStrings a b => a -> b
cs ByteString
byteString) of
Just Inet
inet -> Inet -> Either ByteString Inet
forall a b. b -> Either a b
Right Inet
inet
Maybe Inet
Nothing -> ByteString -> Either ByteString Inet
forall a b. a -> Either a b
Left ByteString
"Invalid IP address"
readParameterJSON :: Value -> Either ByteString Inet
readParameterJSON (Aeson.String Text
string) = case String -> Maybe Inet
forall a. Read a => String -> Maybe a
readMaybe (Text -> String
forall a b. ConvertibleStrings a b => a -> b
cs Text
string) of
Just Inet
inet -> Inet -> Either ByteString Inet
forall a b. b -> Either a b
Right Inet
inet
Maybe Inet
Nothing -> ByteString -> Either ByteString Inet
forall a b. a -> Either a b
Left ByteString
"Invalid IP address"
readParameterJSON Value
_ = ByteString -> Either ByteString Inet
forall a b. a -> Either a b
Left ByteString
"Expected String"
instance ParamReader ModelSupport.Polygon where
{-# INLINABLE readParameter #-}
readParameter :: ByteString -> Either ByteString Polygon
readParameter ByteString
byteString =
let
pointParser :: Parser ByteString (Double, Double)
pointParser = do
Char -> Parser Char
Attoparsec.char Char
'('
x <- Parser Double
Attoparsec.double
Attoparsec.char ','
y <- Attoparsec.double
Attoparsec.char ')'
pure (x, y)
parser :: Parser ByteString Polygon
parser = do
points <- Parser ByteString (Double, Double)
pointParser Parser ByteString (Double, Double)
-> Parser Char -> Parser ByteString [(Double, Double)]
forall (f :: * -> *) a s. Alternative f => f a -> f s -> f [a]
`Attoparsec.sepBy` (Char -> Parser Char
Attoparsec.char Char
',')
Attoparsec.endOfInput
case ModelSupport.refineFromPointList points of
Just Polygon
polygon -> Polygon -> Parser ByteString Polygon
forall a. a -> Parser ByteString a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Polygon
polygon
Maybe Polygon
Nothing -> String -> Parser ByteString Polygon
forall a. String -> Parser ByteString a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Polygon must have at least 3 points"
in
case Parser ByteString Polygon -> ByteString -> Either String Polygon
forall a. Parser a -> ByteString -> Either String a
Attoparsec.parseOnly Parser ByteString Polygon
parser ByteString
byteString of
Right Polygon
value -> Polygon -> Either ByteString Polygon
forall a b. b -> Either a b
Right Polygon
value
Left String
error -> ByteString -> Either ByteString Polygon
forall a b. a -> Either a b
Left (String -> ByteString
forall a b. ConvertibleStrings a b => a -> b
cs String
error)
readParameterJSON :: Value -> Either ByteString Polygon
readParameterJSON (Aeson.String Text
string) = let ByteString
byteString :: ByteString = Text -> ByteString
forall a b. ConvertibleStrings a b => a -> b
cs Text
string in ByteString -> Either ByteString Polygon
forall a. ParamReader a => ByteString -> Either ByteString a
readParameter ByteString
byteString
readParameterJSON Value
_ = ByteString -> Either ByteString Polygon
forall a b. a -> Either a b
Left ByteString
"Expected Polygon"
instance {-# OVERLAPS #-} (ParamReader (ModelSupport.PrimaryKey model')) => ParamReader (ModelSupport.Id' model') where
{-# INLINABLE readParameter #-}
readParameter :: ByteString -> Either ByteString (Id' model')
readParameter ByteString
uuid = PrimaryKey model' -> Id' model'
forall (table :: Symbol). PrimaryKey table -> Id' table
ModelSupport.Id (PrimaryKey model' -> Id' model')
-> Either ByteString (PrimaryKey model')
-> Either ByteString (Id' model')
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either ByteString (PrimaryKey model')
forall a. ParamReader a => ByteString -> Either ByteString a
readParameter ByteString
uuid
readParameterJSON :: Value -> Either ByteString (Id' model')
readParameterJSON Value
value = PrimaryKey model' -> Id' model'
forall (table :: Symbol). PrimaryKey table -> Id' table
ModelSupport.Id (PrimaryKey model' -> Id' model')
-> Either ByteString (PrimaryKey model')
-> Either ByteString (Id' model')
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Either ByteString (PrimaryKey model')
forall a. ParamReader a => Value -> Either ByteString a
readParameterJSON Value
value
enumParamReader :: forall parameter. (Enum parameter, ModelSupport.InputValue parameter) => ByteString -> Either ByteString parameter
enumParamReader :: forall parameter.
(Enum parameter, InputValue parameter) =>
ByteString -> Either ByteString parameter
enumParamReader ByteString
string =
case (parameter -> Bool) -> [parameter] -> Maybe parameter
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\parameter
value -> parameter -> Text
forall a. InputValue a => a -> Text
ModelSupport.inputValue parameter
value Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
string') [parameter]
forall enumType. Enum enumType => [enumType]
allEnumValues of
Just parameter
value -> parameter -> Either ByteString parameter
forall a b. b -> Either a b
Right parameter
value
Maybe parameter
Nothing -> ByteString -> Either ByteString parameter
forall a b. a -> Either a b
Left ByteString
"Invalid value"
where
string' :: Text
string' = ByteString -> Text
forall a b. ConvertibleStrings a b => a -> b
cs ByteString
string
class FillParams (params :: [Symbol]) record where
fill :: (
?request :: Request
, HasField "meta" record ModelSupport.MetaBag
, SetField "meta" record ModelSupport.MetaBag
) => record -> record
instance FillParams ('[]) record where
fill :: (?request::Request, HasField "meta" record MetaBag,
SetField "meta" record MetaBag) =>
record -> record
fill !record
record = record
record
{-# INLINE fill #-}
instance (FillParams rest record
, KnownSymbol fieldName
, SetField fieldName record fieldType
, ParamReader fieldType
, HasField "meta" record ModelSupport.MetaBag
, SetField "meta" record ModelSupport.MetaBag
) => FillParams (fieldName:rest) record where
fill :: (?request::Request, HasField "meta" record MetaBag,
SetField "meta" record MetaBag) =>
record -> record
fill !record
record =
let
ByteString
name :: ByteString = String -> ByteString
forall a b. ConvertibleStrings a b => a -> b
cs (String -> ByteString) -> String -> ByteString
forall a b. (a -> b) -> a -> b
$! (Proxy fieldName -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @fieldName))
record' :: record
record' = case ByteString -> Either ParamException fieldType
forall paramType.
(?request::Request, ParamReader paramType) =>
ByteString -> Either ParamException paramType
paramOrError ByteString
name of
Right !(fieldType
value :: fieldType) -> forall (field :: Symbol) model value.
SetField field model value =>
value -> model -> model
setField @fieldName fieldType
value record
record
Left ParamCouldNotBeParsedException { ByteString
parserError :: ByteString
parserError :: ParamException -> ByteString
parserError } -> Proxy fieldName -> Text -> record -> record
forall (field :: Symbol) model.
(KnownSymbol field, HasField "meta" model MetaBag,
SetField "meta" model MetaBag) =>
Proxy field -> Text -> model -> model
attachFailure (forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @fieldName) (ByteString -> Text
forall a b. ConvertibleStrings a b => a -> b
cs ByteString
parserError) record
record
Left ParamNotFoundException {} -> record
record
in
forall (params :: [Symbol]) record.
(FillParams params record, ?request::Request,
HasField "meta" record MetaBag, SetField "meta" record MetaBag) =>
record -> record
fill @rest record
record'
{-# INLINE fill #-}
ifValid :: (HasField "meta" model ModelSupport.MetaBag) => (Either model model -> IO r) -> model -> IO r
ifValid :: forall model r.
HasField "meta" model MetaBag =>
(Either model model -> IO r) -> model -> IO r
ifValid Either model model -> IO r
branch model
model = Either model model -> IO r
branch (Either model model -> IO r) -> Either model model -> IO r
forall a b. (a -> b) -> a -> b
$! if model -> Bool
forall record. HasField "meta" record MetaBag => record -> Bool
ModelSupport.isValid model
model
then model -> Either model model
forall a b. b -> Either a b
Right model
model
else model -> Either model model
forall a b. a -> Either a b
Left model
model
{-# INLINE ifValid #-}
ifNew :: forall record. (?modelContext :: ModelSupport.ModelContext, HasField "meta" record MetaBag) => (record -> record) -> record -> record
ifNew :: forall record.
(?modelContext::ModelContext, HasField "meta" record MetaBag) =>
(record -> record) -> record -> record
ifNew record -> record
thenBlock record
record = if record -> Bool
forall record. HasField "meta" record MetaBag => record -> Bool
ModelSupport.isNew record
record then record -> record
thenBlock record
record else record
record
emptyValueToNothing :: Proxy name -> model -> model
emptyValueToNothing Proxy name
field = Proxy name -> (Maybe mono -> Maybe mono) -> model -> model
forall model (name :: Symbol) value.
(KnownSymbol name, HasField name model value,
SetField name model value) =>
Proxy name -> (value -> value) -> model -> model
modify Proxy name
field (Maybe mono -> (mono -> Maybe mono) -> Maybe mono -> Maybe mono
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Maybe mono
forall a. Maybe a
Nothing (\mono
value -> if mono -> Bool
forall mono. MonoFoldable mono => mono -> Bool
null mono
value then Maybe mono
forall a. Maybe a
Nothing else mono -> Maybe mono
forall a. a -> Maybe a
Just mono
value))