I use GORM as ORM library for my project (REST API).
A single book has an author. When no author is defined (no first name or last name), I want to omit the complete author field.
Now, the JSON output of the "author" field is "author": {},
instead of omitting the field.
What could be a clean solution to achieve this?
Code snippet
type Author struct {
FirstName string `json:"first_name,omitempty"`
LastName int32 `json:"last_name,omitempty"`
}
type Book struct {
ID uint
Title string `gorm:"index;not null" json:"title"`
Author Author `gorm:"embedded" json:"author,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}