Open germainc opened 1 month ago
I use this approach, for nil values.
func TestNilToVal(t *testing.T) {
var boolean *bool
resBool := ogencl.NewOptBool(NilToVal(boolean))
fmt.Println(resBool)
var str *string
resString := ogencl.NewOptString(NilToVal(str))
fmt.Println(resString)
}
func NilToVal[T any](v *T) T {
if v == nil {
v = new(T)
}
return *v
}
@wildwind123 That approach makes the assumption that nil
and the zero value of a type carry the same meaning in your system.
If you do that with a *time.Time
it will render out as:
"generated_at": "0001-01-01T00:00:00Z"
which does not have the same meaning as:
"generated_at": null
Description
Would it make more sense to instantiate
OptNilDateTime
(orOptDateTime
or any other optional type) by passing in a pointer?You would still have the flexibility to write more complex logic when you need it but it would simplify most uses cases.