How do I tell sqlc to omit a field in the json if it's empty #1087
Replies: 3 comments 4 replies
-
There is currently not a way a to do this. What you can do is use a custom struct and override the output of the query to return that struct. |
Beta Was this translation helpful? Give feedback.
-
In the same vein, could sql add support for golang json struct flag type User struct {
Id int64 `json:"id,string"`
} |
Beta Was this translation helpful? Give feedback.
-
I have opened a PR for adding support for |
Beta Was this translation helpful? Give feedback.
-
Let's say I have a table
After I run
sqlc generate
the following model will be generated:I would like to omit the description if it's empty (as it's a nullable column).
Now, I can use column override and use
string
type instead ofsql.NullString
. So now the generated type will beThe problem with this is that if Description is
nil
, it won't be omitted from thejson
response. So the response would be something like this:Is there a way I can tell
sqlc
to addomitempty
tag to a particular field? (Description field in this case)Any other way to solve this issue?
Beta Was this translation helpful? Give feedback.
All reactions