IHP Api Reference
Copyright(c) digitally induced GmbH 2020
Safe HaskellSafe-Inferred

IHP.HaskellSupport

Description

 
Synopsis

Documentation

(|>) :: t -> (t -> t) -> t infixl 8 Source #

(|>>) :: Functor f => f a -> (a -> b) -> f b infixl 8 Source #

whenEmpty :: (Applicative f, IsEmpty value) => value -> f () -> f () Source #

whenNonEmpty :: (IsEmpty a, Applicative f) => a -> f () -> f () Source #

get :: forall model name value. (KnownSymbol name, HasField name model value) => Proxy name -> model -> value Source #

Returns the field value for a field name

Example:

data Project = Project { name :: Text, isPublic :: Bool }

let project = Project { name = "Hello World", isPublic = False }
>>> project.name
"Hello World"
>>> project.isPublic
False

set :: forall model name value. (KnownSymbol name, SetField name model value) => Proxy name -> value -> model -> model Source #

Sets a field of a record and returns the new record.

Example:

data Project = Project { name :: Text, isPublic :: Bool }

let project = Project { name = "Hello World", isPublic = False }
>>> set #name "New Name" project
Project { name = "New Name", isPublic = False }
>>> set #isPublic True project
Project { name = "Hello World", isPublic = True }

setJust :: forall model name value. (KnownSymbol name, SetField name model (Maybe value)) => Proxy name -> value -> model -> model Source #

Like set but wraps the value with a Just. Useful when you want to set a Maybe field

Example:

data Project = Project { name :: Maybe Text }

let project = Project { name = Nothing }
>>> setJust #name "New Name" project
Project { name = Just "New Name" }

setMaybe :: forall model name value. (KnownSymbol name, SetField name model (Maybe value)) => Proxy name -> Maybe value -> model -> model Source #

Like set but doesn't set the value if it's Nothing. Useful when you update NULL values | e.g. via a cron job and don't want to lose that work on subsequent updates.

Example:

data Project = Project { name :: Maybe Text }

let project = Project { name = Nothing }
>>> setMaybe #name (Just "New Name") project
Project { name = Just "New Name" }
>>> setMaybe #name Nothing project
Project { name = Just "New Name" } -- previous value is kept

ifOrEmpty :: Monoid a => Bool -> a -> a Source #

modify :: forall model name value. (KnownSymbol name, HasField name model value, SetField name model value) => Proxy name -> (value -> value) -> model -> model Source #

modifyJust :: forall model name value. (KnownSymbol name, HasField name model (Maybe value), SetField name model (Maybe value)) => Proxy name -> (value -> value) -> model -> model Source #

class SetField (field :: Symbol) model value | field model -> value where Source #

Methods

setField :: value -> model -> model Source #

Instances

Instances details
SetField "maxItems" Options Int Source # 
Instance details

Defined in IHP.Pagination.Types

SetField "selectFrom" SQLQuery ByteString Source # 
Instance details

Defined in IHP.QueryBuilder

SetField "windowSize" Options Int Source # 
Instance details

Defined in IHP.Pagination.Types

SetField "annotations" MetaBag [(Text, Violation)] Source # 
Instance details

Defined in IHP.ModelSupport

SetField "asyncs" DataSyncController [Async ()] Source # 
Instance details

Defined in IHP.DataSync.ControllerImpl

SetField "distinctClause" SQLQuery (Maybe ByteString) Source # 
Instance details

Defined in IHP.QueryBuilder

SetField "distinctOnClause" SQLQuery (Maybe ByteString) Source # 
Instance details

Defined in IHP.QueryBuilder

SetField "limitClause" SQLQuery (Maybe ByteString) Source # 
Instance details

Defined in IHP.QueryBuilder

SetField "offsetClause" SQLQuery (Maybe ByteString) Source # 
Instance details

Defined in IHP.QueryBuilder

SetField "orderByClause" SQLQuery [OrderByClause] Source # 
Instance details

Defined in IHP.QueryBuilder

SetField "queryIndex" SQLQuery (Maybe ByteString) Source # 
Instance details

Defined in IHP.QueryBuilder

SetField "touchedFields" MetaBag [Text] Source # 
Instance details

Defined in IHP.ModelSupport

Methods

setField :: [Text] -> MetaBag -> MetaBag Source #

SetField "whereCondition" SQLQuery (Maybe Condition) Source # 
Instance details

Defined in IHP.QueryBuilder

SetField "subscriptions" DataSyncController (HashMap UUID (MVar ())) Source # 
Instance details

Defined in IHP.DataSync.ControllerImpl

SetField "transactions" DataSyncController (HashMap UUID DataSyncTransaction) Source # 
Instance details

Defined in IHP.DataSync.ControllerImpl

SetField "cssFramework" (FormContext record) CSSFramework Source # 
Instance details

Defined in IHP.View.Types

Methods

setField :: CSSFramework -> FormContext record -> FormContext record Source #

SetField "disableJavascriptSubmission" (FormContext record) Bool Source # 
Instance details

Defined in IHP.View.Types

Methods

setField :: Bool -> FormContext record -> FormContext record Source #

SetField "formAction" (FormContext record) Text Source # 
Instance details

Defined in IHP.View.Types

Methods

setField :: Text -> FormContext record -> FormContext record Source #

SetField "formClass" (FormContext record) Text Source # 
Instance details

Defined in IHP.View.Types

Methods

setField :: Text -> FormContext record -> FormContext record Source #

SetField "formId" (FormContext record) Text Source # 
Instance details

Defined in IHP.View.Types

Methods

setField :: Text -> FormContext record -> FormContext record Source #

SetField "formMethod" (FormContext record) Text Source # 
Instance details

Defined in IHP.View.Types

Methods

setField :: Text -> FormContext record -> FormContext record Source #

SetField "model" (FormContext record) record Source # 
Instance details

Defined in IHP.View.Types

Methods

setField :: record -> FormContext record -> FormContext record Source #

SetField "state" (ComponentInstance state) state Source # 
Instance details

Defined in IHP.ServerSideComponent.Types

Methods

setField :: state -> ComponentInstance state -> ComponentInstance state Source #

SetField "customFormAttributes" (FormContext record) [(Text, Text)] Source # 
Instance details

Defined in IHP.View.Types

Methods

setField :: [(Text, Text)] -> FormContext record -> FormContext record Source #

class HasField field model value => UpdateField (field :: Symbol) model model' value value' | model model' value' -> value where Source #

Methods

updateField :: value' -> model -> model' Source #

incrementField :: forall model name value. (KnownSymbol name, HasField name model value, SetField name model value, Num value) => Proxy name -> model -> model Source #

Plus 1 on record field.

Example:

data Project = Project { name :: Text, followersCount :: Int }

let project = Project { name = "Hello World", followersCount = 0 }
>>> project |> incrementField #followersCount
Project { name = "Hello World", followersCount = 1 }

decrementField :: forall model name value. (KnownSymbol name, HasField name model value, SetField name model value, Num value) => Proxy name -> model -> model Source #

Minus 1 on a record field.

Example:

data Project = Project { name :: Text, followersCount :: Int }

let project = Project { name = "Hello World", followersCount = 1337 }
>>> project |> decrementField #followersCount
Project { name = "Hello World", followersCount = 1336 }

forEach :: (MonoFoldable mono, Applicative m) => mono -> (Element mono -> m ()) -> m () Source #

Example:

forEach users \user -> putStrLn (tshow user)

Example: Within HSX

renderUser :: User -> Html
renderUser user = [hsx|<div>User: {user.name}</div>|]

render = [hsx|{forEach users renderUser}|]

forEachWithIndex :: Applicative m => [a] -> ((Int, a) -> m ()) -> m () Source #

Like forEach but with an index, starting at 0

Example: With a Callback

forEachWithIndex users \(index, user) -> putStrLn (tshow index <> ": " <> tshow user)

Example: With a Function

printUser :: (Int, User) -> IO ()
printUser (index, user) = putStrLn (tshow index <> ": " <> tshow user)

forEachWithIndex users printUser

Example: Within HSX

renderUser :: (Int, User) -> Html
renderUser (index, user) = [hsx|<div>User {index}: {user.name}</div>|]

render = [hsx|{forEachWithIndex users renderUser}|]

textToInt :: Text -> Maybe Int Source #

Parses a text to an int. Returns Nothing on failure.

Example:

>>> textToInt "1337"
Just 1337
>>> textToInt "bad input"
Nothing

isWeekend :: Day -> Bool Source #

Returns True when day is Saturday or Sunday.

Example:

>>> isWeekend $ fromGregorian 2019 10 7
False
>>> isWeekend $ fromGregorian 2020 6 13
True

todayIsWeekend :: IO Bool Source #

Returns True when today is Saturday or Sunday.

Example:

do
    todayIsWeekend <- isWeekend
    when todayIsWeekend (putStrLn "It's weekend!")

debug :: Show value => value -> value Source #

Debug-print a value during evaluation

Alias for traceShowId

includes :: (MonoFoldable container, Eq (Element container)) => Element container -> container -> Bool Source #

stripTags :: Text -> Text Source #

Removes all html tags from a given html text

>>> stripTags "This is <b>Bold</b>"
"This is Bold"

symbolToText :: forall symbol. KnownSymbol symbol => Text Source #

Returns the value of a type level symbol as a text

>>> symbolToText @"hello"
"hello"
>>> symbolToText @(GetTableName User)
"users"

symbolToByteString :: forall symbol. KnownSymbol symbol => ByteString Source #

Returns the value of a type level symbol as a bytestring

>>> symbolToByteString @"hello"
"hello"
>>> symbolToByteString @(GetTableName User)
"users"

class IsEmpty value where Source #

Used by nonEmpty and isEmptyValue to check for emptyness

Methods

isEmpty :: value -> Bool Source #

Returns True when the value is an empty string, empty list, zero UUID, etc.

Instances

Instances details
IsEmpty Text Source # 
Instance details

Defined in IHP.HaskellSupport

Methods

isEmpty :: Text -> Bool Source #

IsEmpty UUID Source # 
Instance details

Defined in IHP.HaskellSupport

Methods

isEmpty :: UUID -> Bool Source #

IsEmpty (PrimaryKey table) => IsEmpty (Id' table) Source # 
Instance details

Defined in IHP.ModelSupport

Methods

isEmpty :: Id' table -> Bool Source #

IsEmpty (Maybe value) Source # 
Instance details

Defined in IHP.HaskellSupport

Methods

isEmpty :: Maybe value -> Bool Source #

IsEmpty [a] Source # 
Instance details

Defined in IHP.HaskellSupport

Methods

isEmpty :: [a] -> Bool Source #

copyFields :: CopyFields fields destinationRecord sourceRecord => sourceRecord -> destinationRecord -> destinationRecord Source #

Provides the copyFields function

Useful to rewrite getter-setter code like this:

let newProject = newRecord @Project
    |> set #name (otherProject.name)
    |> set #isPublic (otherProject.isPublic)
    |> set #userId (otherProject.userId)

With copyFields this can be written like this:

let newProject = newRecord @Project
    |> copyFields @["name", "isPublic", "userId"] otherProject

allEnumValues :: forall enumType. Enum enumType => [enumType] Source #

Returns a list of all values of an enum type

Given a data structure like this:

data Color = Yellow | Red | Blue deriving (Enum)

You can call allEnumValues to get a list of all colors:

>>> allEnumValues @Color
[Yellow, Red, Blue]

This also works if the enum is defined in the Schema.sql:

CREATE TYPE brokerage_subscription_type AS ENUM ('basic_subscription', 'bronze_subscription', 'silver_subscription', 'gold_subscription');
>>> allEnumValues @BrokerageSubscriptionType
[BasicSubscription, BronzeSubscription, SilverSubscription]

Orphan instances

IsString UUID Source # 
Instance details

Methods

fromString :: String -> UUID #

Default UUID Source # 
Instance details

Methods

def :: UUID #

ConvertibleStrings ByteString Key Source # 
Instance details

ConvertibleStrings Text Key Source # 
Instance details

Methods

convertString :: Text -> Key #

(KnownSymbol name, name' ~ name) => IsLabel name (Proxy name') Source # 
Instance details

Methods

fromLabel :: Proxy name' #

IsString string => IsString (Maybe string) Source #

Allows `Just "someThing"` to be written as `"someThing"`

Instance details

Methods

fromString :: String -> Maybe string #