I'm failing to understand how I can work around the problem I'm currently experiencing in my own application.
Imagine this example of a struct that models an incoming request and a function that puts the fields from that request into a database.
type NewBooleanRequest struct {
RequiredString string `json:"requiredString"`
OptionalBoolean bool `json:"maybeBoolean"`
}
func LogBooleanRequest(req NewBooleanRequest, db *sql.DB) {
db.Exec("INSERT INTO log (booleanValue, stringValue) VALUES ($1, $2)", req.OptionalBoolean, req.RequiredString)
}
Now this obviously works fine if I know I will be given a value for all fields of my request model, but that's not a common requirement in reality. How do people generally model "optional" semantics for bool
values given that bool
has a zero value that is valid in essentially all contexts?