Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

For nulls.Time, decode empty value as NULL #2359

Merged
merged 1 commit into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions binding/decoders/null_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ func NullTimeDecoderFn() func([]string) (interface{}, error) {
return func(vals []string) (interface{}, 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
Expand Down
6 changes: 6 additions & 0 deletions binding/decoders/null_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,28 @@ func Test_NullTimeCustomDecoder_Decode(t *testing.T) {
testCases := []struct {
input string
expected time.Time
expValid bool
expectErr bool
}{
{
input: "2017-01-01",
expected: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.UTC),
expValid: true,
},
{
input: "2018-07-13T15:34",
expected: time.Date(2018, time.July, 13, 15, 34, 0, 0, time.UTC),
expValid: true,
},
{
input: "2018-20-10T30:15",
expected: time.Time{},
expValid: false,
},
{
input: "",
expected: time.Time{},
expValid: false,
},
}

Expand All @@ -47,5 +52,6 @@ func Test_NullTimeCustomDecoder_Decode(t *testing.T) {
}

r.Equal(testCase.expected, nt.Time)
r.Equal(testCase.expValid, nt.Valid)
}
}