Skip to content

chore: allow empty str #25

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

Merged
merged 1 commit into from
Sep 26, 2022
Merged

chore: allow empty str #25

merged 1 commit into from
Sep 26, 2022

Conversation

fundon
Copy link
Member

@fundon fundon commented Sep 24, 2022

#19

@adriangb try this branch

@adriangb
Copy link
Contributor

cc #26

@fundon
Copy link
Member Author

fundon commented Sep 24, 2022

@adriangb In this case:

Router(
  {
    "/users": Router(
      {
        "": users_endpoint,
        "/:name": user_endpoint,
       }
    )
)

Can the sub-router get the prefix path /users?

@adriangb
Copy link
Contributor

Not easily. That same instance could be used somewhere else as well.

@adriangb
Copy link
Contributor

I'm realizing I may have misunderstood. Just to clarify, I don't think the nested router should know/need to know that it is mounted under the prefix "/users".

With the example you gave I think the routing should be as follows:

/users -> Some(users_endpoint)
/users/ -> None
/users/abc123 -> Some(user_endpoint)

@fundon
Copy link
Member Author

fundon commented Sep 24, 2022

In writing, empty string can be used, but a full path can be assembled at the end.

Pseudo codes:

sub_router.path = ''
sub_router.routes = ['', '/:name']

parent_router.path = '/users'
parent_router.mount(sub_router)

fn mount(sub_router) {
   for route in sub_router.routes {
       let path = parent_router.path + route
       tree.insert(path)
   }
}

@adriangb
Copy link
Contributor

Yeah that’s one way to do it. It’s nice to be able to support the version where the parent router doesn’t need to introspect into itself children

@fundon fundon merged commit 8e8e39b into main Sep 26, 2022
@fundon fundon deleted the allow_empty_str branch September 26, 2022 01:34
@fundon fundon restored the allow_empty_str branch September 26, 2022 01:34
Comment on lines +1696 to +1698
tree.insert("", 14);
let r = tree.find("/").unwrap();
assert_eq!(r.value, &14);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit confusing. The path goes in as "/" but comes out as "/"?

Copy link
Member Author

Choose a reason for hiding this comment

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

tree.insert("", 14) allow change the start node value , but it does not modify the node prefix path.

  1. tree.insert("", 14) node path = ''
  2. tree.insert("/", 15) node path = '/', overwrite the empty string

Copy link
Member Author

Choose a reason for hiding this comment

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

Or do not allow empty string, users handle their own paths

Copy link
Member Author

@fundon fundon Sep 26, 2022

Choose a reason for hiding this comment

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

here is another way to allow '' to be the root node of the tree and '/' to be its child node. (@adriangb This should be what you expect).

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea I would expect “/“ to be a child of ””

Copy link
Member Author

Choose a reason for hiding this comment

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

Because, I always consider that the real request path does not appear '', when organizing the routing system, it has to pre-process path at the users

Copy link
Contributor

Choose a reason for hiding this comment

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

For what it's worth I was easily able to work around this one level of abstraction up: adriangb/routrie@70fc7c5

I think the fact that this can be solved one level of abstraction up indicates that the limitation is artificial/due to the implementation, not a conceptual one.

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

Successfully merging this pull request may close these issues.

2 participants