add date format and orDefault helpers

This commit is contained in:
Hayden 2022-12-03 15:02:07 -09:00
parent de419dc37d
commit 29ede65033
No known key found for this signature in database
GPG key ID: 17CF79474E257545

View file

@ -0,0 +1,18 @@
package repo
import "time"
func sqliteDateFormat(t time.Time) string {
return t.Format("2006-01-02 15:04:05")
}
// orDefault returns the value of the pointer if it is not nil, otherwise it returns the default value
//
// This is used for nullable or potentially nullable fields (or aggregates) in the database when running
// queries. If the field is null, the pointer will be nil, so we return the default value instead.
func orDefault[T any](v *T, def T) T {
if v == nil {
return def
}
return *v
}