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

Add custom URL convertor register #1437

Merged
merged 7 commits into from Jan 28, 2022
Merged

Conversation

Kludex
Copy link
Sponsor Member

@Kludex Kludex commented Jan 27, 2022

Custom URL Convertor

Currently, we don't have a way to create URL convertors, meaning that our users can only match the default types. Those are:

CONVERTOR_TYPES = {
"str": StringConvertor(),
"path": PathConvertor(),
"int": IntegerConvertor(),
"float": FloatConvertor(),
"uuid": UUIDConvertor(),
}

To be fair, there's a way: adding a key, value pair on the CONVERTOR_TYPES dictionary. That's not available on Starlette's public API, or documented.

The idea of this PR is to document and introduce a public API that can be used to create custom URL convertors.

How does Django and Flask handle this?

Both of them have a way to register custom URL convertors. You can find the way Django makes it possible here, and for Flask here.

Other

Recently, there was a PR to introduce time related convertors on #1398, and I think we should follow the path this PR proposes here, and not creating more convertors on Starlette itself.

Missing

  • docs (Do we want a single page with this, or it should be a section on the Routing page?)

@Kludex Kludex changed the title Add register that allows custom URL convertors Add custom URL convertor register Jan 27, 2022
@@ -0,0 +1,99 @@
import datetime as dt
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

I've created multiple convertors here with the purpose of reproducing #1398. I can also simplify the test suite if wanted. Just let me know.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with simplifying the tests, as this can be extended for other things like UUID and there's not really much difference. This is really good.

@Kludex Kludex requested a review from a team January 27, 2022 09:46
@tomchristie
Copy link
Member

Well this is nice and simple isn't it? Seems worthwhile.
Would need some brief docs.

@aminalaee
Copy link
Member

@Kludex I guess this PR can close this #1058 with a reference/example, what do you think?

@Kludex
Copy link
Sponsor Member Author

Kludex commented Jan 27, 2022

@Kludex I guess this PR can close this #1058 with a reference/example, what do you think?

Yeah, this PR closes that issue as well. I've used another convertor as an example on the docs, but we can answer that issue with "how to register/change the path convertor now".

docs/routing.md Outdated Show resolved Hide resolved
@Kludex Kludex merged commit 199fc70 into encode:master Jan 28, 2022
@Kludex Kludex deleted the feat/custom-convertor branch February 17, 2023 18: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.

Add user-defined converters to routing
3 participants