-
-
Notifications
You must be signed in to change notification settings - Fork 8
initial work on router compiler #6
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
15e7804
initial work on router compiler
rmorshea 6d7254f
fix tests and upgrade to compat latest idom ver
rmorshea 54d6776
rework route compiler interface
rmorshea 540d0ce
rework based on feedback
rmorshea 2f8f853
remove starlette as dep
rmorshea a169d6f
remove print
rmorshea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
from typing import Any | ||
|
||
from starlette.convertors import Convertor | ||
from starlette.routing import compile_path as _compile_starlette_path | ||
|
||
from idom_router.types import Route | ||
|
||
|
||
def compile_starlette_route(route: Route) -> StarletteRoutePattern: | ||
pattern, _, converters = _compile_starlette_path(route.path) | ||
return StarletteRoutePattern(pattern, converters) | ||
|
||
|
||
class StarletteRoutePattern: | ||
rmorshea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def __init__( | ||
self, | ||
pattern: re.Pattern[str], | ||
converters: dict[str, Convertor], | ||
) -> None: | ||
self.pattern = pattern | ||
self.key = pattern.pattern | ||
self.converters = converters | ||
|
||
def match(self, path: str) -> dict[str, Any] | None: | ||
match = self.pattern.match(path) | ||
if match: | ||
return { | ||
k: self.converters[k].convert(v) if k in self.converters else v | ||
for k, v in match.groupdict().items() | ||
} | ||
rmorshea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import Any, Sequence, TypeVar | ||
|
||
from idom.types import Key | ||
from typing_extensions import Protocol, Self | ||
|
||
|
||
@dataclass | ||
class Route: | ||
path: str | ||
element: Any | ||
routes: Sequence[Self] | ||
|
||
def __init__( | ||
self, | ||
path: str, | ||
element: Any | None, | ||
*routes_: Self, | ||
# we need kwarg in order to play nice with the expected dataclass interface | ||
routes: Sequence[Self] = (), | ||
) -> None: | ||
self.path = path | ||
self.element = element | ||
self.routes = (*routes_, *routes) | ||
|
||
|
||
R = TypeVar("R", bound=Route, contravariant=True) | ||
|
||
|
||
class RouteCompiler(Protocol[R]): | ||
def __call__(self, route: R) -> RoutePattern: | ||
"""Compile a route into a pattern that can be matched against a path""" | ||
|
||
|
||
class RoutePattern(Protocol): | ||
@property | ||
def key(self) -> Key: | ||
"""Uniquely identified this pattern""" | ||
|
||
def match(self, path: str) -> dict[str, Any] | None: | ||
"""Returns otherwise a dict of path parameters if the path matches, else None""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
idom >=0.40.2,<0.41 | ||
idom >=1 | ||
typing_extensions | ||
starlette |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.