Skip to content

Commit 62bd6d8

Browse files
authored
Fix Videos API failing to parse Duration (#19)
1 parent cf6d4f4 commit 62bd6d8

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

api/twitch_videos.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"net/http"
78
"time"
@@ -21,11 +22,13 @@ type Video struct {
2122
ViewCount int `json:"view_count"`
2223
Language string `json:"language"`
2324
Type string `json:"type"`
24-
Duration time.Duration `json:"duration"`
25+
Duration VideoDuration `json:"duration"`
2526
PublishedAt time.Time `json:"published_at"`
2627
CreatedAt time.Time `json:"created_at"`
2728
}
2829

30+
type VideoDuration time.Duration
31+
2932
type VideosResource struct {
3033
client *Client
3134
}
@@ -174,3 +177,21 @@ func (c *VideosDeleteCall) Do(ctx context.Context, opts ...RequestOption) (*Vide
174177
Data: data.Data,
175178
}, nil
176179
}
180+
181+
func (d *VideoDuration) UnmarshalJSON(data []byte) error {
182+
var str string
183+
if err := json.Unmarshal(data, &str); err != nil {
184+
return err
185+
}
186+
187+
parsed, err := time.ParseDuration(str)
188+
if err != nil {
189+
return err
190+
}
191+
*d = VideoDuration(parsed)
192+
return nil
193+
}
194+
195+
func (d VideoDuration) AsDuration() time.Duration {
196+
return time.Duration(d)
197+
}

0 commit comments

Comments
 (0)