Skip to content

Commit e3d3a01

Browse files
authored
Merge pull request #2359 from travisturner/tlt/empty-null-time
For nulls.Time, decode empty value as NULL
2 parents 578e4d0 + 0ed3bfe commit e3d3a01

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

binding/decoders/null_time.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ func NullTimeDecoderFn() func([]string) (interface{}, error) {
77
return func(vals []string) (interface{}, error) {
88
var ti nulls.Time
99

10+
// If vals is empty, return a nulls.Time with Valid = false (i.e. NULL).
11+
// The parseTime() function called below does this check as well, but
12+
// because it doesn't return an error in the case where vals is empty,
13+
// we have no way to determine from its response that the nulls.Time
14+
// should actually be NULL.
15+
if len(vals) == 0 || vals[0] == "" {
16+
return ti, nil
17+
}
18+
1019
t, err := parseTime(vals)
1120
if err != nil {
1221
return ti, err

binding/decoders/null_time_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,28 @@ func Test_NullTimeCustomDecoder_Decode(t *testing.T) {
1414
testCases := []struct {
1515
input string
1616
expected time.Time
17+
expValid bool
1718
expectErr bool
1819
}{
1920
{
2021
input: "2017-01-01",
2122
expected: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.UTC),
23+
expValid: true,
2224
},
2325
{
2426
input: "2018-07-13T15:34",
2527
expected: time.Date(2018, time.July, 13, 15, 34, 0, 0, time.UTC),
28+
expValid: true,
2629
},
2730
{
2831
input: "2018-20-10T30:15",
2932
expected: time.Time{},
33+
expValid: false,
3034
},
3135
{
3236
input: "",
3337
expected: time.Time{},
38+
expValid: false,
3439
},
3540
}
3641

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

4954
r.Equal(testCase.expected, nt.Time)
55+
r.Equal(testCase.expValid, nt.Valid)
5056
}
5157
}

0 commit comments

Comments
 (0)