evancz / url-parser

Parse URLs into nicely structured data
http://package.elm-lang.org/packages/evancz/url-parser/latest/
BSD 3-Clause "New" or "Revised" License
114 stars 29 forks source link

Question: How to parse recursive paths? #12

Closed arcmode closed 8 years ago

arcmode commented 8 years ago

Hi, thanks for making Elm, it's great!.

Well, I have this type for pages:

  type Page
      = CollectionPage String (Maybe Page)
      | ItemPage  Int (Maybe Page)

And I'm trying to parse paths like this:

  /users/5/friends/9 

Into this:

  page =
      CollectionPage "users"
          (Just
              (ItemPage 5
                  (Just
                      (CollectionPage "friends"
                          (Just (ItemPage 9 Nothing))
                      )
                  )
              )
          )

So far I have managed to parse only explicit paths using format, but I want a general parser for arbitrary recursive paths. Is there an idiomatic way to achieve this recursive parsing using format or do you think I need to make something with custom or anything else?

process-bot commented 8 years ago

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

evancz commented 8 years ago

Thanks :) Before answering the general question, I'd like to know more about your scenario. Are you saying that the following path is valid for you?

/users/5/users/5/users/5/users/5/users/5/

Can you be more explicit about the paths that are valid? What exactly are you trying to do?

(Also, these questions are typically better for the Elm slack channel or for elm-discuss.)

arcmode commented 8 years ago

Hello, thanks for answering.

Any path formed by strings and numbers would be valid initially. I just want to represent paths with a recursive type because I want to play with a recursive viewPage : Page -> Html msg where each node decides how to treat its child node and so on.

I think I probably will have to change the type Page to be able to pattern-match everything safely but the core of my question is how can I parse recursively using this library in order to produce recursive values?. My best idea right now is to use custom directly instead of format.

Sorry for posting a non-issue by the way, I will bring this into slack and or elm-discuss. Do you mind if I share the link to this issue with more people or you prefer to close it?

evancz commented 8 years ago

You cannot do it with the current API. I would not want to support that in this library unless we had a good concrete scenario that will come up in practice often. That said, you can always parse things from the Location yourself, so nothing is stopping you from handling "recursive paths".

It's fine to link here, but I think it'd be better to continue discussion in another place. If that turns into a specific example that is representative of general use, open a new issue about that.