From 97d99f242eff183a065513c12e68194629e82c35 Mon Sep 17 00:00:00 2001 From: Marc Scholten Date: Thu, 1 Oct 2020 16:41:42 +0200 Subject: [PATCH] Added textToId function Fixes #432 --- Guide/recipes.markdown | 15 +++++++++++++++ IHP/ModelSupport.hs | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Guide/recipes.markdown b/Guide/recipes.markdown index 880b3f683..4ef6228e3 100644 --- a/Guide/recipes.markdown +++ b/Guide/recipes.markdown @@ -169,6 +169,21 @@ In case the id is hardcoded, you can just type UUID value with the right type si let projectId = "ca63aace-af4b-4e6c-bcfa-76ca061dbdc6" :: Id Project ``` +## Getting a `Id Something` from a `Text` / `ByteString` / `String` + +Sometimes you have a text, bytestring or string which represents some record id. You can transform it to an Id like this: + +```haskell +let myUUID :: Text = ... +let projectId = textToId myUUID +``` + +In case the id is hardcoded, you can just type UUID value with the right type signature like this: + +```haskell +let projectId = "ca63aace-af4b-4e6c-bcfa-76ca061dbdc6" :: Id Project +``` + ## Making a dynamic Login/Logout button Depending on the `user` object from the viewContext, we can tell that there is no user logged in when the `user` is `Nothing`, and confirm someone is logged in if the `user` is a `Just user`. Here is an example of a navbar, which has a dynamic Login/Logout button. You can define this in your View/Layout to reuse this in your Views. diff --git a/IHP/ModelSupport.hs b/IHP/ModelSupport.hs index 1bc3856b6..652c9ddea 100644 --- a/IHP/ModelSupport.hs +++ b/IHP/ModelSupport.hs @@ -14,7 +14,7 @@ import Database.PostgreSQL.Simple.FromField hiding (Field, name) import Database.PostgreSQL.Simple.ToField import Data.Default import Data.Time.Format.ISO8601 (iso8601Show) -import Data.String.Conversions (cs) +import Data.String.Conversions (cs ,ConvertibleStrings) import Data.Time.Clock import Data.Time.Calendar import Unsafe.Coerce @@ -205,9 +205,27 @@ instance Newtype.Newtype (Id' model) where pack = Id unpack (Id uuid) = uuid +-- | Sometimes you have a hardcoded UUID value which represents some record id. This instance allows you +-- to write the Id like a string: +-- +-- > let projectId = "ca63aace-af4b-4e6c-bcfa-76ca061dbdc6" :: Id Project instance Read (PrimaryKey model) => IsString (Id' model) where fromString uuid = Id (Prelude.read uuid) +-- | Transforms a text, bytestring or string into an Id. Throws an exception if the input is invalid. +-- +-- __Example:__ +-- +-- > let projectIdText = "7cbc76e2-1c4f-49b6-a7d9-5015e7575a9b" :: Text +-- > let projectId = (textToId projectIdText) :: Id Project +-- +-- In case your UUID value is hardcoded, there is also an 'IsString' instance, so you +-- can just write it like: +-- +-- > let projectId = "ca63aace-af4b-4e6c-bcfa-76ca061dbdc6" :: Id Project +textToId :: (Read (PrimaryKey model), ConvertibleStrings text String) => text -> Id' model +textToId text = Id (Prelude.read (cs text)) + instance Default (PrimaryKey model) => Default (Id' model) where {-# INLINE def #-} def = Newtype.pack def