I am implementing a rest api where I send a json request body.
type Service struct {
id int64 `db:"id" json:"id"`
Name string `form:"name" db:"name" json:"name" binding:"required"`
Servicetype string `form:"type" db:"type" json:"type" binding:"required"`
}
func myHandler(c *gin.Context) {
if c.BindJSON(&json) == nil {
fmt.Println(json.Servicetype)
} else {
fmt.Println("json binding error")
}
}
Servicetype is of type enum in my database. How can I have binding for that in my Service struct? I am able to bind the Name
field as it is of type VARCHAR in database. But it fails to bind when I add Servicetype
in the struct. I am using postgres as my database.