You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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 structrequest.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.
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:
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":
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.
The text was updated successfully, but these errors were encountered: