From 52d53103ad40ce3f9be27153efd6e5ed2bd74023 Mon Sep 17 00:00:00 2001 From: Alexis Brodeur Date: Tue, 1 May 2018 15:12:50 -0400 Subject: [PATCH 1/5] Add example for `newtype` in routes Also add parens to previous examples that failed compilation. --- GUIDE.md | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/GUIDE.md b/GUIDE.md index 625f139..f3135ff 100644 --- a/GUIDE.md +++ b/GUIDE.md @@ -83,7 +83,7 @@ no interesting values we want to consume, we can use `<$` from `Prelude`. ```purescript postIndex :: Match MyRoute postIndex = - PostIndex <$ lit "posts + PostIndex <$ (lit "posts") ``` Our next routes require extracting an integer `PostId`. @@ -91,11 +91,11 @@ Our next routes require extracting an integer `PostId`. ```purescript post :: Match MyRoute post = - Post <$> lit "posts" *> int + Post <$> (lit "posts" *> int) postEdit :: Match MyRoute postEdit = - PostEdit <$> lit "posts" *> int <* lit "edit" + PostEdit <$> (lit "posts" *> int <* lit "edit") ``` Note the use of the `*>` and `<*` operators. These let us direct the focus of @@ -108,7 +108,7 @@ And now finally, we need to extract multiple segments for `PostBrowse`. ```purescript postBrowse :: Match MyRoute postBrowse = - PostBrowse <$> lit "posts" *> str <*> str + PostBrowse <$> (lit "posts" *> str) <*> str ``` The `<*>` combinator has arrows on both sides because we want both values. @@ -145,9 +145,9 @@ We can also go ahead and inline our parsers. myRoute :: Match MyRoute myRoute = oneOf [ PostIndex <$ lit "posts" - , Post <$> lit "posts" *> int - , PostEdit <$> lit "posts" *> int <* lit "edit" - , PostBrowse <$> lit "posts" *> str <*> str + , Post <$> (lit "posts" *> int) + , PostEdit <$> (lit "posts" *> int <* lit "edit") + , PostBrowse <$> (lit "posts" *> str) <*> str ] ``` @@ -336,3 +336,36 @@ main = do Right { foo, bar } -> ... Left errors -> ... ``` + +## Using `newtype` in routes + +Using the following routes with a `newtype`: +```purescript +newtype PostId = PostID Int +derive instance newtypePostID :: Newtype PostId _ + +data MyRoute + = PostIndex + | Post PostId + | PostEdit PostId + | PostBrowse String String +``` + +It is possible to warp an `int` route parameter into a `PostId` using the +following function: +```purescript +postId :: forall f. MatchClass f => f PostId +postId = pure wrap <*> int +``` + +The created `postId` function can then be used like the `str` function. +```purescript +myRoute :: Match MyRoute +myRoute = + root *> lit "posts" *> oneOf + [ PostEdit <$> postId <* lit "edit" + , Post <$> postId + , PostBrowse <$> str <*> str + , pure PostIndex + ] <* end +``` From eceb17f07c5b2a254c8484050cc699c70b293ac9 Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Wed, 22 Sep 2021 13:19:09 -0700 Subject: [PATCH 2/5] Fix merge conflict mitsake --- GUIDE.md | 1 - 1 file changed, 1 deletion(-) diff --git a/GUIDE.md b/GUIDE.md index 1752534..4d56486 100644 --- a/GUIDE.md +++ b/GUIDE.md @@ -111,7 +111,6 @@ And now finally, we need to extract multiple segments for `PostBrowse`. ```purescript postBrowse :: Match MyRoute postBrowse = - PostBrowse <$> (lit "posts" *> str) <*> str PostBrowse <$> (lit "posts" *> lit "browse" *> int) <*> str ``` From f6ec6731ff3fbdb75de6bc3200182350c4e39079 Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Wed, 22 Sep 2021 13:22:22 -0700 Subject: [PATCH 3/5] Address all feedback --- GUIDE.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/GUIDE.md b/GUIDE.md index 4d56486..f38a0ae 100644 --- a/GUIDE.md +++ b/GUIDE.md @@ -350,8 +350,8 @@ main = do Using the following routes with a `newtype`: ```purescript -newtype PostId = PostID Int -derive instance newtypePostID :: Newtype PostId _ +newtype PostId = PostId Int +derive instance newtypePostId :: Newtype PostId _ data MyRoute = PostIndex @@ -360,21 +360,21 @@ data MyRoute | PostBrowse String String ``` -It is possible to warp an `int` route parameter into a `PostId` using the +It is possible to wrap an `int` route parameter into a `PostId` using the following function: ```purescript postId :: forall f. MatchClass f => f PostId -postId = pure wrap <*> int +postId = PostId <*> int ``` -The created `postId` function can then be used like the `str` function. +The created `postId` function can then be used like the `parser` function. ```purescript myRoute :: Match MyRoute myRoute = root *> lit "posts" *> oneOf [ PostEdit <$> postId <* lit "edit" , Post <$> postId - , PostBrowse <$> str <*> str + , PostBrowse <$> (lit "browse" *> int) <*> str , pure PostIndex ] <* end ``` From 7d2cbe9d0572f90ff65e19f9521afee4062ef374 Mon Sep 17 00:00:00 2001 From: JordanMartinez Date: Wed, 22 Sep 2021 13:23:13 -0700 Subject: [PATCH 4/5] Drop unneeded parens --- GUIDE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GUIDE.md b/GUIDE.md index f38a0ae..fd93668 100644 --- a/GUIDE.md +++ b/GUIDE.md @@ -82,7 +82,7 @@ no interesting values we want to consume, we can use `<$` from `Prelude`. ```purescript postIndex :: Match MyRoute postIndex = - PostIndex <$ (lit "posts") + PostIndex <$ lit "posts" ``` Our next routes require extracting an integer `PostId`. From 9f4dcc9bf707e41cb6890b0ed56119a6f902f765 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Thu, 23 Sep 2021 07:31:43 -0700 Subject: [PATCH 5/5] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 880c5a6..506c17e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ New features: Bugfixes: Other improvements: +- Update readme to show how to use newtypes (#57 by @brodeuralexis and @JordanMartinez) ## [v10.0.1](https://github.com/purescript-contrib/purescript-routing/releases/tag/v10.0.1) - 2021-05-06