Skip to content

Commit

Permalink
Added textToId function
Browse files Browse the repository at this point in the history
Fixes #432
  • Loading branch information
mpscholten committed Oct 1, 2020
1 parent 8193105 commit 97d99f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Guide/recipes.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 19 additions & 1 deletion IHP/ModelSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 97d99f2

Please sign in to comment.