Closed hmajid2301 closed 3 years ago
Hello.
This is not related to this library, but to loopfz/tonic, which define the tonic.Handler
type. See https://github.com/loopfz/gadgeto/blob/master/tonic/README for reference.
What you should do is relying on a single type for your input parameter in the handler. You may use embedded structs in this case, to split common field in their own struct type.
edit: btw, tonic will handle the binding of the query params, headers AND body in the same input type.
I'm pretty new to golang would you mind showing me an example ?
Sure.
So, according yo what you said, your route /game/:name/question
should be represented with an input type like the following, to use in the handler's definition. This type defines the parameters of the body, but it also "embed" the game parameters through the type GameParams
.
type NewQuestionIn struct {
GameParams
Field1 string `json:"field1"`
Field2 string `json:"field2"`
}
type GameParams struct {
Name string `path:"name"`
APIKey string `header:"X-Api-Key"` // for example, if you need to bind headers
}
The handler should then have the following prototype:
func AddQuestion(c *gin.Context, in *NewQuestionIn) ("return type", error)
I imagine that you may have aQuestion
model type, like so:
type Question struct {
...
}
If so, you may reuse it. Embed it in the input type used in the handler:
type NewQuestionIn struct {
GameParams
Question
}
And the handler becomes:
func AddQuestion(c *gin.Context, in *NewQuestionIn) (*Question, error)
Here i assume that the handler should return a Question
object since that's the behavior of CREATE methods in traditional CRUD APIs.
I suggest that you take a look at the example. It doesn't show types composition, but it's a good start.
Closing, since there's no issue with the library itself, but feel free to ask if you need other clarifications.
Is it possible to have parameters and the body passed to a function?
For example let's say my endpoint is POST
/game/{name}/question
.Where the defintion looks like:
and the function definition looks something like:
However I get the following error message:
Is there another way to get the body and the parameters passed to a function ? Thanks