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

initial work on router compiler #6

Merged
merged 6 commits into from
May 11, 2023
Merged

initial work on router compiler #6

merged 6 commits into from
May 11, 2023

Conversation

rmorshea
Copy link
Contributor

@rmorshea rmorshea commented Oct 20, 2022

closes: #5
closes: #13

Depends on: reactive-python/reactpy#823


The interface for the compiler is a simple callable of the form:

def my_compiler(route: Route) -> RoutePattern: ...

Where RoutePattern is any object of the form:

class MyRoutePattern:
    key: Key
    def match(self, path: str) -> dict[str, Any] | None: ...

The value returned by match is None to indicate a non-match or a dictionary of parameters that were discovered in the path string.

@Archmonger
Copy link
Contributor

Archmonger commented Oct 20, 2022

It might be nice to be able to pass in parameters to the URL matching engine via the Route(...) callable. For example, in Django's case it would allow users to configure between regex matching and Django matching.

@rmorshea
Copy link
Contributor Author

An easy way to determine whether it's a regex path could be to check if it starts with ^.

@Archmonger
Copy link
Contributor

Archmonger commented Apr 15, 2023

I think it's better to be explicit.

In general, we probably need some kind of idom_router.types.UrlResolver prototype in order to use Django's matching.

Route( ... , url_resolver: Callable | None = DjangoResolver )
from dataclasses import dataclass
from typing import Callable, Iterable


@dataclass
class UrlResolver:
    is_match: Callable[[str, Iterable[str]], bool]

@rmorshea rmorshea force-pushed the route-compiler branch 4 times, most recently from 5d7170c to 21543f0 Compare April 29, 2023 22:05
@rmorshea
Copy link
Contributor Author

@Archmonger, I've generalized the interface and edited the description. In short, you are now allowed to create compilers that operate on Route subclasses. So in the case of django, you could create a wrapper around the existing tooling and do something like:

from reactpy_router import router as _router, Route as _Route

@dataclass
class Route(_Route):

    regex: bool

    # need a custom constructor due to the `*args` allowed for routes in the base class
    def __init__(self, *args, regex: None | bool = None, **kwargs):
        self.regex = regex
        super().__init__(*args, **kwargs)

def router(*routes: Route):
    return _route(*routes, compiler=compile_django_route)

def compile_django_route(route: Route) -> DjangoRoutePattern:
    ...

class DjangoRoutePattern:
    ...

See above for expected interface for RoutePattern.

This allows compilers to work in a wider variety of ways.
@rmorshea rmorshea marked this pull request as ready for review April 29, 2023 22:29
@rmorshea rmorshea requested a review from Archmonger April 29, 2023 22:29
@Archmonger
Copy link
Contributor

I'll take a peek at it soon. Luckily today is the first time in a while I'm actually going to have some free time for dev.

Copy link
Contributor

@Archmonger Archmonger left a comment

Choose a reason for hiding this comment

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

  • Needs docs on how to use this in the readme.
    • I'm also now noticing there's still literally no info on how to use idom router in the readme.
  • Needs more comments across the new code.
  • There's a bit of over-privatization of variables going on. I think it's best to only underscore things that, if the user was to touch it, would fundamentally break things.

tests/test_router.py Outdated Show resolved Hide resolved
idom_router/__init__.py Outdated Show resolved Hide resolved
idom_router/compilers.py Outdated Show resolved Hide resolved
idom_router/compilers.py Outdated Show resolved Hide resolved
idom_router/router.py Outdated Show resolved Hide resolved
@Archmonger
Copy link
Contributor

I'm practically done with everything on my Conreq PR besides integrating URL routing. So this PR has officially become a hard block for me.

@rmorshea
Copy link
Contributor Author

rmorshea commented May 6, 2023

Notes from review:

idom_router/routers/starlette.py Outdated Show resolved Hide resolved
idom_router/core.py Show resolved Hide resolved
@rmorshea rmorshea force-pushed the route-compiler branch 3 times, most recently from 5a92ffb to f23c06a Compare May 10, 2023 06:52
@rmorshea
Copy link
Contributor Author

rmorshea commented May 10, 2023

  • I think that caching still makes sense - there's a fair bit of logic required to parse paths and their parameters (see idom_router.simple.parse_path) and the hashing logic will likely be substantially faster.
  • react-router supports nested routes, but not nested "routers"
  • We can add docs in a follow-up PR - it seems prudent to get this in, rename the repo, and release to unblock things

@rmorshea rmorshea requested a review from Archmonger May 10, 2023 07:01
@Archmonger
Copy link
Contributor

Archmonger commented May 10, 2023

EDIT: The suggestion below wouldn't match react-router's paradigm of creating a new router for different matching styles. Can be ignored.


What if rather than us compiling the paths within the route handler, we provide a function that compiles the provided path when provided?

class Path:
    def __init__(self, path: str):
        self.path = path
        self.something_to_be_cached = ...
        ...

    def __repr__(self):
        return self.path

    def resolve(self, match: str) -> bool:
        ...

Path("^this_is_my_path/")

This would require all router paths to be Path type, rather than str. For other frameworks, Path can be subclassed. Such as DjangoPath("this_is_my_path/<int>/")

This would allow regex caching to occur each Path instance during the initial declaration. Would alleviate issues with stale caching.

@Archmonger
Copy link
Contributor

Will review tomorrow

Copy link
Contributor

@Archmonger Archmonger left a comment

Choose a reason for hiding this comment

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

Other than a missed print statement, looks good to me.

I believe this PR also closes #13

idom_router/simple.py Outdated Show resolved Hide resolved
@rmorshea rmorshea merged commit 8077994 into main May 11, 2023
@Archmonger Archmonger deleted the route-compiler branch December 18, 2023 10:41
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.

Rename Route to route Ability to configure the URL routing engine
2 participants