Skip to content
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

Matching wildcard #20

Closed
shovelmn12 opened this issue Apr 28, 2021 · 5 comments
Closed

Matching wildcard #20

shovelmn12 opened this issue Apr 28, 2021 · 5 comments

Comments

@shovelmn12
Copy link

Hello

I have seen the issue before #18 and read your comment about it
Now we do have a use case for it and I am working on a library that needs it :)

I am working on a routing library that would let you use it with ease and now with web support the library really needs a wildcard matching for routing

WouterRouterDelegate(
    tag: "home",
    child: WouterSwitch(
      routes: {
        "/": (context, [Map<String, dynamic>? data]) => MaterialPage(
              child: HomeScreen(),
            ),
        "/people": (context, [Map<String, dynamic>? data]) => MaterialPage(
              child: PeopleScreen(),
            ),
      },
    ),
  );

under /people route:

WouterRouterDelegate.withParent(
          parent: context.wouter,
          tag: "people",
          base: "/people",
          child: WouterSwitch(
            routes: {
              r"/:id(\d+)": (context, [Map<String, dynamic>? data]) =>
                  MaterialPage(
                    child: PersonDetailsScreen(
                      person: people[data!["id"]]!,
                    ),
                  ),
              "/": (context, [Map<String, dynamic>? data]) =>
                  const MaterialPage(
                    child: AllPeopleScreen(),
                  ),
              r"(.*)": (context, [Map<String, dynamic>? data]) =>
                  const MaterialPage(
                    child: Redirect(
                      to: "/people",
                    ),
                  ),
            },
          ),
        ),

No i would like users to be able to redirect to other paths with ease and currently it is not possible
as you can see from the example above I would like to redirect users from anything that doesn't match "/" or "/:id(\d+)" back to "/people"
but not because i cant match a wildcard so I can't implement such a capability
I can't use prefix: true as a user might not want it in other routes..
that just an easy example and the sky is the limit of course

I propose to add wildcard: bool option that set to false by default so then users can choose if to add support for wildcard matching or not

  1. How do you convert * to a path? In the branch linked above, I decided to treat it as an empty path, but it's not clear that would always be desired.

if you are referring to toPath method you can throw an error or return null as its not something anyone should use... (maybe just generate a random path haha)
otherwise I dont see any other issues with that

  1. It doesn't introduce any new capability that I'm aware of. It's a slightly more convenient shorthand for writing the following path specification.

I just put on a good example :)

Ill be happy if you can consider the option to put it back in some form as I think it would be very beneficial for web libraries

@leonsenft
Copy link
Owner

You can achieve the behavior you've described using a parameter that matches anything as follows:

routes: {
  r'/:id(\d+)': ... // people details
  r'/': ... // people screen
  r':_(.*)': ... // redirect
}

Have I misunderstood; does this fail to meet your criteria in any way? Is this simply about the ergonomics of it?

@shovelmn12
Copy link
Author

shovelmn12 commented May 7, 2021

does this :_(.*) matches anything?
if so it doesn't matter as long as I can get that functionality ...

EDIT:

if :_(.*) matches /any/path/even/long so its great :)

@shovelmn12
Copy link
Author

I found whats not ok :)

I can't do: /:id* because /:id/:_(.*) wont match /someID....
How can I get that behaviour? with the current options its only with setting prefix: true

@leonsenft
Copy link
Owner

Thank you for the example! I think this is another possible use case for #19, which would allow you to define it as /:id/:_(.*)?

@shovelmn12
Copy link
Author

Thank you for the example! I think this is another possible use case for #19, which would allow you to define it as /:id/:_(.*)?

Ok ill try that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants