Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle null value in JSONTime #1106

Merged
merged 2 commits into from Sep 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions info.go
Expand Up @@ -409,6 +409,11 @@ func (t JSONTime) Time() time.Time {
func (t *JSONTime) UnmarshalJSON(buf []byte) error {
s := bytes.Trim(buf, `"`)

if bytes.EqualFold(s, []byte("null")) {
*t = JSONTime(0)
return nil
}

v, err := strconv.Atoi(string(s))
if err != nil {
return err
Expand Down
44 changes: 44 additions & 0 deletions info_test.go
@@ -0,0 +1,44 @@
package slack

import (
"testing"
)

func TestJSONTime_UnmarshalJSON(t *testing.T) {
type args struct {
buf []byte
}
tests := []struct {
name string
args args
wantTr JSONTime
wantErr bool
}{
{
"acceptable int64 timestamp",
args{[]byte(`1643435556`)},
JSONTime(1643435556),
false,
},
{
"acceptable string timestamp",
args{[]byte(`"1643435556"`)},
JSONTime(1643435556),
false,
},
{
"null",
args{[]byte(`null`)},
JSONTime(0),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var tr JSONTime
if err := tr.UnmarshalJSON(tt.args.buf); (err != nil) != tt.wantErr {
t.Errorf("JSONTime.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}