-
Notifications
You must be signed in to change notification settings - Fork 17
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
base: master
Are you sure you want to change the base?
Conversation
| Foo | ||
| Bar String Int String { a :: String, b :: Boolean } | ||
| Baz { id :: String, search :: String } | ||
| Qux String (Array String) |
There was a problem hiding this comment.
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.
@@ -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))) |
There was a problem hiding this comment.
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/
} | ||
where | ||
fooRoute = | ||
segment / int segment / segment ? { a: string, b: flag } | ||
path "qux" noArgs # end' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be “foo”
I don’t think this actually tests that I could write some manual test cases (e.g. |
Something to also consider is if you want to canonicalize to no trailing slash, you could use existing combinators with a pre-processing step which trims empty segments (via parsePath and runRouteParser). purescript-routing-duplex/src/Routing/Duplex/Parser.purs Lines 142 to 185 in a03a30e
|
It’s not clear to me how to do that, unless you’re suggesting changing the implementation of those functions. |
Parsing is just the composition of these two functions purescript-routing-duplex/src/Routing/Duplex/Parser.purs Lines 187 to 190 in a03a30e
I'm saying that it's possible to define your own version of this that just inserts a normalization step between the two, removing trailing |
After thinking about this some more, I think that a preprocessing/normalization stage is the better approach. That is, I think that the combinators provided by the library should be bidirectional, and I don't really like the idea of exposing a combinator with a clear bias. Enough guts are exposed that it's very easy to implement your own bias if that's what you want. I would think that whether or not to accept a trailing slash is something you'd want to do across the board though, which benefits from a preprocessing step, and is something we could make easily configurable. Some options could be whether to remove duplicate slashes, or whether to add/remove trailing/leading slashes. Is this something you'd be interested in doing? Do you have a strong use case for ad-hoc slash handling? |
I don't have a use case for ad-hoc slash handling, and would be interested in working on preprocessing/normalization although I'm not sure when I'd get around to it. |
As discussed on Slack. I figured it was a pretty common use case.