-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unmarshal/MarshalYAML for ChapterSegment
- Loading branch information
Showing
6 changed files
with
206 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cast | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Chapter struct { | ||
Segments []*ChapterSegment | ||
Body string | ||
} | ||
|
||
type ChapterSegment struct { | ||
Title string `json:"title"` | ||
Start uint64 `json:"start"` | ||
} | ||
|
||
func convertStartToString(start uint64) string { | ||
seconds := start % 60 | ||
minutes := (start / 60) % 60 | ||
hours := start / 3600 | ||
startTime := fmt.Sprintf("%d:%02d", minutes, seconds) | ||
if hours > 0 { | ||
startTime = fmt.Sprintf("%d:%02d:%02d", hours, minutes, seconds) | ||
} | ||
return startTime | ||
} | ||
|
||
func convertStringToStart(str string) (uint64, error) { | ||
if l := len(strings.Split(str, ":")); l > 3 { | ||
return 0, fmt.Errorf("invalid time format: %s", str) | ||
} else if l == 2 { | ||
str = "0:" + str | ||
} | ||
var h, m, s uint64 | ||
if _, err := fmt.Sscanf(str, "%d:%d:%d", &h, &m, &s); err != nil { | ||
return 0, fmt.Errorf("invalid time format: %s", str) | ||
} | ||
return h*3600 + m*60 + s, nil | ||
} | ||
|
||
func (chs *ChapterSegment) String() string { | ||
return fmt.Sprintf("%s %s", convertStartToString(chs.Start), chs.Title) | ||
} | ||
|
||
func (chs *ChapterSegment) UnmarshalYAML(b []byte) error { | ||
str := strings.TrimSpace(string(b)) | ||
stuff := strings.SplitN(str, " ", 2) | ||
if len(stuff) != 2 { | ||
return fmt.Errorf("invalid chapter format: %s", str) | ||
} | ||
start, err := convertStringToStart(stuff[0]) | ||
if err != nil { | ||
return fmt.Errorf("invalid chapter format: %s, %w", str, err) | ||
} | ||
*chs = ChapterSegment{ | ||
Title: stuff[1], | ||
Start: start, | ||
} | ||
return nil | ||
} | ||
|
||
func (chs *ChapterSegment) MarshalYAML() ([]byte, error) { | ||
return []byte(chs.String()), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package cast_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Songmu/podbard/internal/cast" | ||
"github.com/goccy/go-yaml" | ||
) | ||
|
||
func TestChapterSegment_MarshalYAML(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
chapter struct { | ||
Segment *cast.ChapterSegment | ||
} | ||
want string | ||
}{{ | ||
name: "simple", | ||
chapter: struct{ Segment *cast.ChapterSegment }{ | ||
Segment: &cast.ChapterSegment{ | ||
Title: "Chapter 1", | ||
Start: 0, | ||
}, | ||
}, | ||
want: "segment: 0:00 Chapter 1\n", | ||
}, { | ||
name: "with hours", | ||
chapter: struct{ Segment *cast.ChapterSegment }{ | ||
Segment: &cast.ChapterSegment{ | ||
Title: "Chapter 2", | ||
Start: 3600, | ||
}, | ||
}, | ||
want: "segment: 1:00:00 Chapter 2\n", | ||
}} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
b, err := yaml.Marshal(tt.chapter) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if got := string(b); got != tt.want { | ||
t.Errorf("got %q; want %q", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestChapterSegment_UnmarshalYAML(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
input string | ||
want cast.ChapterSegment | ||
wantErr bool | ||
}{{ | ||
name: "simple", | ||
input: "0:00 Chapter 1", | ||
want: cast.ChapterSegment{ | ||
Title: "Chapter 1", | ||
Start: 0, | ||
}, | ||
}, { | ||
name: "with hours", | ||
input: "1:00:00 Chapter 2", | ||
want: cast.ChapterSegment{ | ||
Title: "Chapter 2", | ||
Start: 3600, | ||
}, | ||
}, { | ||
name: "invalid format", | ||
input: "invalid", | ||
wantErr: true, | ||
}, { | ||
name: "invalid time format", | ||
input: "1:00:00:00 Chapter 2", | ||
wantErr: true, | ||
}, { | ||
name: "invalid hours", | ||
input: "a:00:00 Chapter 2", | ||
wantErr: true, | ||
}, { | ||
name: "invalid minutes", | ||
input: "1:a:00 Chapter 2", | ||
wantErr: true, | ||
}, { | ||
name: "invalid seconds", | ||
input: "1:00:a Chapter 2", | ||
wantErr: true, | ||
}} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var got cast.ChapterSegment | ||
err := yaml.Unmarshal([]byte(tt.input), &got) | ||
if err != nil { | ||
if !tt.wantErr { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("got %v; want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.