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
Question description
Hello I have this problem when trying to use the BodyParse in my structure with the special types of SQL to be able to receive null values, BodyParse apparently cannot handle them
Code snippet
package main
import (
"fmt""time""github.com/google/uuid""github.com/gofiber/fiber/v2"
)
typeNullString sql.NullStringtypeCatalogstruct {
ID uuid.UUID`json:"id"`ParentID uuid.UUID`json:"parent_id"`Namestring`json:"name"`AddonNullString`json:"addon"`Typestring`json:"type"`CreateAt time.Time`json:"create_at"`UpdateAt time.Time`json:"update_at"`
}
funcCatalogInsert(catalog*Catalog) Catalog {
db:=Connect()
deferdb.Close()
varnewCatalogCatalogid:=uuid.New()
fields:="id,name,addon,type"values:="UUID_TO_BIN(?),?,?,?"ifcatalog.ParentID!=uuid.Nil {
fields+=",parent_id"values+=",UUID_TO_BIN(?)"
}
query:=fmt.Sprintf(`INSERT INTO catalogs (%s) VALUES (%s)`, fields, values)
insert, err:=db.Prepare(query)
iferr!=nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
ifcatalog.ParentID!=uuid.Nil {
_, err=insert.Exec(
id,
catalog.Name,
catalog.Addon,
catalog.Type,
catalog.ParentID,
)
} else {
_, err=insert.Exec(
id,
catalog.Name,
catalog.Addon,
catalog.Type,
)
}
iferr!=nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
deferinsert.Close()
results, err:=db.Prepare(`SELECT BIN_TO_UUID(id) AS id, BIN_TO_UUID(parent_id) AS parent_id, name, addon, type FROM catalogs WHERE id = UUID_TO_BIN(?)`)
iferr!=nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
err=results.QueryRow(id).Scan(
newCatalog.ID,
newCatalog.ParentID,
newCatalog.Name,
newCatalog.Addon,
newCatalog.Type,
)
iferr!=nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
returnnewCatalog
}
funcCatalogAdd(c*fiber.Ctx) error {
catalog:=new(Catalog)
iferr:=c.BodyParser(catalog); err!=nil {
returnerr
}
newCatalog:=CatalogInsert(catalog)
returnc.JSON(fiber.Map{
"item": newCatalog,
})
}
When I do the post, it throws me the following error
json: cannot unmarshal ""USA","type":"country"}" into Go struct field Catalog.addon of type NullString
How could I solve it.
Thank you for your answers.
The text was updated successfully, but these errors were encountered:
Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord
I had similar issue, and ended up writing custom UnmarshalJSON. since BodyParser uses Unmarshaler interface (for JSON) you can handle it in your own way. I created new struct for the request body, and implemented the Unmarshaler + Marshaler interfaces to handle incoming and outgoing JSONs.
Question description
Hello I have this problem when trying to use the BodyParse in my structure with the special types of SQL to be able to receive null values, BodyParse apparently cannot handle them
Code snippet
When I do the post, it throws me the following error
json: cannot unmarshal ""USA","type":"country"}" into Go struct field Catalog.addon of type NullString
How could I solve it.
Thank you for your answers.
The text was updated successfully, but these errors were encountered: