blogposts: use io.Reader interface to decouple

This commit is contained in:
vinchent 2024-09-24 21:04:06 +02:00
parent dec5f045a7
commit 1153a0a5bd

View File

@ -17,7 +17,7 @@ func NewPostsFromFS(fileSystem fs.FS) ([]Post, error) {
}
var posts []Post
for _, f := range dir {
post, err := getPost(fileSystem, f)
post, err := getPost(fileSystem, f.Name())
if err != nil {
return nil, err
}
@ -26,8 +26,8 @@ func NewPostsFromFS(fileSystem fs.FS) ([]Post, error) {
return posts, nil
}
func getPost(fileSystem fs.FS, f fs.DirEntry) (Post, error) {
postFile, err := fileSystem.Open(f.Name())
func getPost(fileSystem fs.FS, fileName string) (Post, error) {
postFile, err := fileSystem.Open(fileName)
if err != nil {
return Post{}, err
}
@ -38,7 +38,7 @@ func getPost(fileSystem fs.FS, f fs.DirEntry) (Post, error) {
// NOTE: Does newPost have to be coupled to an fs.File ?
// Do we use all the methods and data from this type? What do we really need?
func newPost(postFile fs.File) (Post, error) {
func newPost(postFile io.Reader) (Post, error) {
postData, err := io.ReadAll(postFile)
if err != nil {
return Post{}, err