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

Commit

Permalink
Merge pull request #2359 from travisturner/tlt/empty-null-time
Browse files Browse the repository at this point in the history
For nulls.Time, decode empty value as NULL
  • Loading branch information
sio4 authored Jan 22, 2023
2 parents 578e4d0 + 0ed3bfe commit e3d3a01
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
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)
}
}

0 comments on commit e3d3a01

Please sign in to comment.