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

Route parameter converter #93

Closed
System-Glitch opened this issue May 2, 2020 · 3 comments
Closed

Route parameter converter #93

System-Glitch opened this issue May 2, 2020 · 3 comments
Labels
feature request Request for new feature implementation good first issue Good for newcomers

Comments

@System-Glitch
Copy link
Member

Proposal

Route parameter converters are functions converting route parameters into another type of data, typically models. The main use-case would be to create a built-in url converter for models removing parameter int conversion and SQL query from the controller.

For example, for the following route definition:

GET /user/{User}

The route parameter converter would take the "User" route parameter and search for the "User" registered model based on its primary key (defined by the gorm tag) in database. If no record is found, then the route will not match and a 404 error will be returned.

This feature could be implemented in a middleware or in the core of the router. However, for performance reasons, a middleware called after the route matched with the current simple matching process would be preferred. That would avoid a lot of unnecessary SQL queries.

Developers using the framework should be able to implement route parameter converters. How to define which converter to use when defining a route is a question that remains open.

Possible drawbacks

It may be difficult to handle multi-parameter routes and especially relations between models. For example, the following route is retrieving a forum post from the author "User":

GET /user/{User}/post/{Post}

How could we check and guarantee that the Post is related to User? Should we use the gorm field tags? What if there are multiple foreign keys to the same table?

An expensive work of reflection may be needed for this kind of use-case.

Additional information

This issue is a feature proposal and is meant to be discussed.
It is also a good candidate if you want to contribute to the project.

@System-Glitch System-Glitch added good first issue Good for newcomers feature request Request for new feature implementation labels May 2, 2020
@MShoaei
Copy link

MShoaei commented May 2, 2020

How exactly do you intend to send a struct in URI? Would you please send and example input?

@System-Glitch
Copy link
Member Author

System-Glitch commented May 2, 2020

@MShoaei
Let's say we have a Product struct model. The idea behind this would be to declare a route like this:

router.Get("/product/{Product}", product.Show, nil)

A request to show the Product with the ID (primary key) "4" would be:

GET /product/4

The framework would do something like this:

product := model.Product{}
database.GetConnection().First(&product, request.Params["Product"])

// Override the raw parameter with the struct
request.Params["Product"] = product

This is just a draft, and that would require the request.Params map to be changed to a map[string]interface{}, which is not ideal, but hopefully you get the idea.

The motivation behind this is that retrieving data from the database is not part of the business logic so it shouldn't be done inside the controller handler. Also, this would reduce redundancy because you currently have to write this kind of code in almost every controller handler.

@System-Glitch
Copy link
Member Author

I prefer to ditch this idea. It is already easy enough to retrieve the model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request for new feature implementation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants