Skip to content

Commit

Permalink
chore(kemono): Change Time to a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jul 10, 2024
1 parent 2d6cf86 commit 7265437
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
7 changes: 3 additions & 4 deletions internal/kemono/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"path"
"strconv"
"strings"
"time"

"github.com/eduncan911/podcast"
"github.com/gabe565/transsmute/internal/util"
Expand Down Expand Up @@ -45,8 +44,8 @@ func (p *Post) FeedItem() *feeds.Item {
Id: p.ID,
Link: &feeds.Link{Href: p.URL().String()},
Title: p.Title,
Created: time.Time(p.Published),
Updated: time.Time(p.Edited),
Created: p.Published.Time,
Updated: p.Edited.Time,
}

var buf strings.Builder
Expand Down Expand Up @@ -89,7 +88,7 @@ func (p *Post) PodcastItem(ctx context.Context) (*podcast.Item, *Attachment, err
TypeFormatted: audioInfo.MIMEType,
},
}
item.AddPubDate((*time.Time)(&p.Published))
item.AddPubDate(&p.Published.Time)
if image != nil {
item.AddImage(image.ThumbURL().String())
}
Expand Down
11 changes: 6 additions & 5 deletions internal/kemono/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func formatServiceName(name string) string {

var ErrInvalidTimeType = errors.New("invalid time type")

type Time time.Time
type Time struct {
time.Time
}

func (d *Time) UnmarshalJSON(data []byte) error {
decoder := json.NewDecoder(bytes.NewReader(data))
Expand All @@ -45,19 +47,18 @@ func (d *Time) UnmarshalJSON(data []byte) error {
return err
}

*d = Time(parsed.UTC())
d.Time = parsed.UTC()
return nil
case json.Number:
val, err := t.Int64()
if err != nil {
return err
}

parsed := time.Unix(val, 0).UTC()
*d = Time(parsed)
d.Time = time.Unix(val, 0).UTC()
return nil
case nil:
*d = Time(time.Time{}.UTC())
d.Time = time.Time{}.UTC()
return nil
}
return ErrInvalidTimeType
Expand Down
2 changes: 1 addition & 1 deletion internal/kemono/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestTime_UnmarshalJSON(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.wantErr(t, tt.d.UnmarshalJSON(tt.args.bytes))
assert.Equal(t, tt.want, time.Time(tt.d))
assert.Equal(t, tt.want, tt.d.Time)
})
}
}
Expand Down

0 comments on commit 7265437

Please sign in to comment.