Closed syahnur197 closed 2 years ago
If we use the following
type Job struct { gorm.Model Title string Company string Salary string Location string }
It will return the following JSON in API
{ "ID" : 1, "CreatedAt" : "foo", "UpdatedAt" : "foo", "DeletedAt": "foo", "Title" : "foo", "Company" : "foo", "Salary" : "foo", "Location" : "foo", }
Annotating the struct with json"field" won't lowercase the fields in gorm.Model
json"field"
type Job struct { gorm.Model Title string `json:"title"` Company string `json:"company"` Salary string `json:"salary"` Location string `json:"location"` }
{ "ID" : 1, "CreatedAt" : "foo", "UpdatedAt" : "foo", "DeletedAt": "foo", "title" : "foo", "company" : "foo", "salary" : "foo", "location" : "foo", }
To lowercase all the fields we can't use gorm.Model
type Job struct { ID uint `gorm:"primarykey" json:"id"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"` Title string `json:"title"` Company string `json:"company"` Salary string `json:"salary"` Location string `json:"location"` }
{ "id" : 1, "created_at" : "foo", "updated_at" : "foo", "deleted_at": "foo", "title" : "foo", "company" : "foo", "salary" : "foo", "location" : "foo", }
I prefer the above JSON fields since it's the common convention in API response. What do you think?
Can we not create our own base model to use instead of gorm.model?
If we use the following
It will return the following JSON in API
Annotating the struct with
json"field"
won't lowercase the fields in gorm.ModelIt will return the following JSON in API
To lowercase all the fields we can't use gorm.Model
It will return the following JSON in API
I prefer the above JSON fields since it's the common convention in API response. What do you think?