Skip to content

Add end' combinator to support trailing slashes #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Routing/Duplex.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Routing.Duplex
, path
, root
, end
, end'
, segment
, param
, flag
Expand All @@ -30,7 +31,7 @@ module Routing.Duplex

import Prelude

import Control.Alt (class Alt)
import Control.Alt (class Alt, (<|>))
import Control.Alternative (class Alternative)
import Data.Either (Either)
import Data.Foldable (class Foldable, foldMap, foldr)
Expand Down Expand Up @@ -81,6 +82,10 @@ root = path ""
end :: forall a b. RouteDuplex a b -> RouteDuplex a b
end (RouteDuplex enc dec) = RouteDuplex enc (dec <* Parser.end)

-- Like `end`, but matches an optional trailing slash when parsing. The slash is omitted when printing.
end' :: forall a b. RouteDuplex a b -> RouteDuplex a b
end' (RouteDuplex enc dec) = RouteDuplex enc (dec <* (Parser.end <|> (Parser.prefix "" Parser.end)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm proposing end' instead of index since it's kind of like end/


segment :: RouteDuplex' String
segment = RouteDuplex Printer.put Parser.take

Expand Down
28 changes: 17 additions & 11 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Data.Generic.Rep.Show (genericShow)
import Data.String.Gen (genAlphaString)
import Data.Symbol (SProxy(..))
import Effect (Effect)
import Routing.Duplex (RouteDuplex', flag, int, param, parse, print, record, rest, root, segment, string, (:=))
import Routing.Duplex (RouteDuplex', end', flag, int, param, parse, path, print, record, rest, root, segment, string, (:=))
import Routing.Duplex.Generic (noArgs)
import Routing.Duplex.Generic as RDG
import Routing.Duplex.Generic.Syntax ((/), (?))
Expand All @@ -17,26 +17,28 @@ import Test.QuickCheck.Gen (Gen, arrayOf, chooseInt)

data TestRoute
= Root
| Foo String Int String { a :: String, b :: Boolean }
| Bar { id :: String, search :: String }
| Baz String (Array String)
| Foo
| Bar String Int String { a :: String, b :: Boolean }
| Baz { id :: String, search :: String }
| Qux String (Array String)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally put made Qux nullary and put it at the bottom but quickcheck was sometimes generating "qux" which would cause the tests to fail.

I know the order doesn't matter here but it does when using sum, and I thought it was best to keep them the same.


derive instance eqTestRoute :: Eq TestRoute
derive instance genericTestRoute :: Generic TestRoute _
instance showTestRoute :: Show TestRoute where show = genericShow

genTestRoute :: Gen TestRoute
genTestRoute = do
chooseInt 1 4 >>= case _ of
chooseInt 1 5 >>= case _ of
1 -> pure Root
2 ->
Foo
2 -> pure Foo
3 ->
Bar
<$> genAlphaString
<*> arbitrary
<*> genAlphaString
<*> ({ a: _, b: _ } <$> genAlphaString <*> arbitrary)
3 -> Bar <$> ({ id: _, search: _ } <$> genAlphaString <*> genAlphaString)
_ -> Baz <$> genAlphaString <*> (arrayOf genAlphaString)
3 -> Baz <$> ({ id: _, search: _ } <$> genAlphaString <*> genAlphaString)
_ -> Qux <$> genAlphaString <*> (arrayOf genAlphaString)

_id = SProxy :: SProxy "id"
_search = SProxy :: SProxy "search"
Expand All @@ -48,17 +50,21 @@ route =
, "Foo": fooRoute
, "Bar": barRoute
, "Baz": bazRoute
, "Qux": quxRoute
}
where
fooRoute =
segment / int segment / segment ? { a: string, b: flag }
path "qux" noArgs # end'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be “foo”


barRoute =
segment / int segment / segment ? { a: string, b: flag }

bazRoute =
record
# _id := segment
# _search := param "search"

bazRoute =
quxRoute =
segment / rest

main :: Effect Unit
Expand Down