Skip to content

Commit

Permalink
Merge pull request #1106 from rusq/patch-1
Browse files Browse the repository at this point in the history
Handle possible null in JSONTime Unmarshal.
  • Loading branch information
kanata2 committed Sep 11, 2022
2 parents ba4dcc3 + e47ef98 commit d197559
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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)
}
})
}
}

0 comments on commit d197559

Please sign in to comment.