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

Custom Route Param Types 🔥 #125

Closed
Ethosa opened this issue Sep 2, 2023 · 0 comments
Closed

Custom Route Param Types 🔥 #125

Ethosa opened this issue Sep 2, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@Ethosa
Copy link
Contributor

Ethosa commented Sep 2, 2023

In Nim it maybe ..

type MyType* = object
  first, second, third: string

proc parseMyType*(data: string): MyType =
  MyType(
    first: data[0], second: data[1], third: data[2]
  )

registerRouteParamType(
  "my_type",  # unique type identifier
  "\d\w\d",  # type pattern
  parseMyType  # proc/func that takes one string argument and returns any data
)

serve "127.0.0.1", 5000:
  get "/{i:my_type}":
    echo i.first
    echo i.second
    echo i.third

Python:

class MyUniqueIdentifier:
    def __init__(self, data: str):
        self.identifier = int(data)


register_route_param_type("my_unique_id", r"\d+", MyUniqueIdentifier)


@app.get("/registered/{data:my_unique_id}")
def handle(data: MyUniqueIdentifier):
    print(data.identifier)
    return {'response': data.identifier}
@Ethosa Ethosa added the enhancement New feature or request label Sep 2, 2023
@Ethosa Ethosa self-assigned this Sep 2, 2023
Ethosa added a commit that referenced this issue Sep 2, 2023
@Ethosa Ethosa closed this as completed Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant