I had an Open API generated schema, and I would like to create the response struct in my handler function. But when I want to create the anonymous struct, I need to write again the field tags, too. Is there any solution for not needing to repeat that? Here is the minimal example
package main
// Response is generated by a tool in a separated file
type Response struct {
Result *struct {
Id int `json:"id"`
} `json:"result,omitempty"`
}
func main() {
var response Response
response.Result = &struct {
Id int `json:"id"`
}{5}
// This results in an error
// cannot use &struct { Id int } literal (type *struct { Id int }) as type *struct { Id int "json:\"id\"" } in assignment
response.Result = &struct {Id int}{5}
}
This anonymous struct is a result of oapi-codegen, when I use a complex type in the components part of the api definition as a response type. I think if I use a CreateBookResponseResult type, the code generator won't generate an anonymous struct, but I would postpone this option until I can.
components:
CreateBookResponse:
type: object
properties:
result:
type: object
properties:
id:
type: integer