-
-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathnull_time.go
More file actions
29 lines (23 loc) · 770 Bytes
/
null_time.go
File metadata and controls
29 lines (23 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package decoders
import "github.com/gobuffalo/buffalo/internal/nulls"
// NullTimeDecoderFn is a custom type decoder func for null.Time fields
func NullTimeDecoderFn() func([]string) (any, error) {
return func(vals []string) (any, error) {
var ti nulls.Time
// If vals is empty, return a nulls.Time with Valid = false (i.e. NULL).
// The parseTime() function called below does this check as well, but
// because it doesn't return an error in the case where vals is empty,
// we have no way to determine from its response that the nulls.Time
// should actually be NULL.
if len(vals) == 0 || vals[0] == "" {
return ti, nil
}
t, err := parseTime(vals)
if err != nil {
return ti, err
}
ti.Time = t
ti.Valid = true
return ti, nil
}
}