From d5946954af5051448784b4376c48d47a09760a83 Mon Sep 17 00:00:00 2001 From: Songmu Date: Wed, 9 Oct 2024 15:13:47 +0900 Subject: [PATCH] rename type to Chapter from ChapterSegment --- internal/cast/audio.go | 14 +++++++------- internal/cast/chapter.go | 10 +++++----- internal/cast/chapter_test.go | 18 +++++++++--------- internal/cast/episode.go | 16 ++++++++-------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/internal/cast/audio.go b/internal/cast/audio.go index 045306d..6e043e5 100644 --- a/internal/cast/audio.go +++ b/internal/cast/audio.go @@ -19,11 +19,11 @@ import ( ) type Audio struct { - Name string `json:"-"` - Title string `json:"title"` - FileSize int64 `json:"file_size"` - Duration uint64 `json:"duration"` - Chapters []*ChapterSegment `json:"chapters,omitempty"` + Name string `json:"-"` + Title string `json:"title"` + FileSize int64 `json:"file_size"` + Duration uint64 `json:"duration"` + Chapters []*Chapter `json:"chapters,omitempty"` rawDuration time.Duration modTime time.Time @@ -119,7 +119,7 @@ func (au *Audio) SaveMeta(rootDir string) error { return nil } -func (au *Audio) UpdateChapter(fpath string, chs []*ChapterSegment) error { +func (au *Audio) UpdateChapter(fpath string, chs []*Chapter) error { // XXX: check the fpath is valid f, err := os.OpenFile(fpath, os.O_RDWR, 0666) if err != nil { @@ -226,7 +226,7 @@ func (au *Audio) readMP3(r io.ReadSeeker) error { for _, frame := range tag.GetFrames("CHAP") { chapterFrame, ok := frame.(id3v2.ChapterFrame) if ok { - au.Chapters = append(au.Chapters, &ChapterSegment{ + au.Chapters = append(au.Chapters, &Chapter{ Title: chapterFrame.Title.Text, Start: uint64(chapterFrame.StartTime.Seconds()), }) diff --git a/internal/cast/chapter.go b/internal/cast/chapter.go index 1a33af2..d6fd2f4 100644 --- a/internal/cast/chapter.go +++ b/internal/cast/chapter.go @@ -5,7 +5,7 @@ import ( "strings" ) -type ChapterSegment struct { +type Chapter struct { Title string `json:"title"` Start uint64 `json:"start"` } @@ -34,11 +34,11 @@ func convertStringToStart(str string) (uint64, error) { return h*3600 + m*60 + s, nil } -func (chs *ChapterSegment) String() string { +func (chs *Chapter) String() string { return fmt.Sprintf("%s %s", convertStartToString(chs.Start), chs.Title) } -func (chs *ChapterSegment) UnmarshalYAML(b []byte) error { +func (chs *Chapter) UnmarshalYAML(b []byte) error { str := strings.TrimSpace(string(b)) stuff := strings.SplitN(str, " ", 2) if len(stuff) != 2 { @@ -48,13 +48,13 @@ func (chs *ChapterSegment) UnmarshalYAML(b []byte) error { if err != nil { return fmt.Errorf("invalid chapter format: %s, %w", str, err) } - *chs = ChapterSegment{ + *chs = Chapter{ Title: stuff[1], Start: start, } return nil } -func (chs *ChapterSegment) MarshalYAML() ([]byte, error) { +func (chs *Chapter) MarshalYAML() ([]byte, error) { return []byte(chs.String()), nil } diff --git a/internal/cast/chapter_test.go b/internal/cast/chapter_test.go index a828480..b1d0656 100644 --- a/internal/cast/chapter_test.go +++ b/internal/cast/chapter_test.go @@ -13,13 +13,13 @@ func TestChapterSegment_MarshalYAML(t *testing.T) { tests := []struct { name string chapter struct { - Segment *cast.ChapterSegment + Segment *cast.Chapter } want string }{{ name: "simple", - chapter: struct{ Segment *cast.ChapterSegment }{ - Segment: &cast.ChapterSegment{ + chapter: struct{ Segment *cast.Chapter }{ + Segment: &cast.Chapter{ Title: "Chapter 1", Start: 0, }, @@ -27,8 +27,8 @@ func TestChapterSegment_MarshalYAML(t *testing.T) { want: "segment: 0:00 Chapter 1\n", }, { name: "with hours", - chapter: struct{ Segment *cast.ChapterSegment }{ - Segment: &cast.ChapterSegment{ + chapter: struct{ Segment *cast.Chapter }{ + Segment: &cast.Chapter{ Title: "Chapter 2", Start: 3600, }, @@ -55,19 +55,19 @@ func TestChapterSegment_UnmarshalYAML(t *testing.T) { tests := []struct { name string input string - want cast.ChapterSegment + want cast.Chapter wantErr bool }{{ name: "simple", input: "0:00 Chapter 1", - want: cast.ChapterSegment{ + want: cast.Chapter{ Title: "Chapter 1", Start: 0, }, }, { name: "with hours", input: "1:00:00 Chapter 2", - want: cast.ChapterSegment{ + want: cast.Chapter{ Title: "Chapter 2", Start: 3600, }, @@ -95,7 +95,7 @@ func TestChapterSegment_UnmarshalYAML(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var got cast.ChapterSegment + var got cast.Chapter err := yaml.Unmarshal([]byte(tt.input), &got) if err != nil { if !tt.wantErr { diff --git a/internal/cast/episode.go b/internal/cast/episode.go index 8ce57dc..d7b7f8a 100644 --- a/internal/cast/episode.go +++ b/internal/cast/episode.go @@ -328,11 +328,11 @@ type Episode struct { } type EpisodeFrontMatter struct { - AudioFile string `yaml:"audio"` - Title string `yaml:"title"` - Date string `yaml:"date"` - Subtitle string `yaml:"subtitle"` - Chapters []*ChapterSegment `yaml:"chapters,omitempty"` + AudioFile string `yaml:"audio"` + Title string `yaml:"title"` + Date string `yaml:"date"` + Subtitle string `yaml:"subtitle"` + Chapters []*Chapter `yaml:"chapters,omitempty"` audio *Audio pubDate time.Time @@ -422,9 +422,9 @@ const chaperTmplStr = ` ` -var chaptertmpl = template.Must(template.New("chapters").Parse(chaperTmplStr)) +var chapterTmpl = template.Must(template.New("chapters").Parse(chaperTmplStr)) -func buildChaptersBody(chapters []*ChapterSegment) (string, error) { +func buildChaptersBody(chapters []*Chapter) (string, error) { data := []struct { Title string Start string @@ -441,7 +441,7 @@ func buildChaptersBody(chapters []*ChapterSegment) (string, error) { } var buf bytes.Buffer - if err := chaptertmpl.Execute(&buf, chapters); err != nil { + if err := chapterTmpl.Execute(&buf, data); err != nil { return "", err } return buf.String(), nil