Skip to content

Commit 5fbd3f7

Browse files
authored
Fix Date param binding (#628)
When `BindStyledParameterWithLocation` is used with a Date param, `UnmarshalText` of the `Date`'s underlying `time.Time` was called. This would result in an error, because the `DateFormat` `2016-01-02` is not used by `time.Time`'s `UnmarshalText`. This commit adds an override to `Date` to make the binding work.
1 parent 256e3b6 commit 5fbd3f7

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

date.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,12 @@ func (d *Date) UnmarshalJSON(data []byte) error {
3232
func (d Date) String() string {
3333
return d.Time.Format(DateFormat)
3434
}
35+
36+
func (d *Date) UnmarshalText(data []byte) error {
37+
parsed, err := time.Parse(DateFormat, string(data))
38+
if err != nil {
39+
return err
40+
}
41+
d.Time = parsed
42+
return nil
43+
}

date_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,14 @@ func TestDate_Stringer(t *testing.T) {
5252
assert.Equal(t, "2019-04-01", fmt.Sprintf("%v", d))
5353
})
5454
}
55+
56+
func TestDate_UnmarshalText(t *testing.T) {
57+
testDate := time.Date(2022, 6, 14, 0, 0, 0, 0, time.UTC)
58+
value := []byte("2022-06-14")
59+
60+
date := Date{}
61+
err := date.UnmarshalText(value)
62+
63+
assert.NoError(t, err)
64+
assert.Equal(t, testDate, date.Time)
65+
}

0 commit comments

Comments
 (0)