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 var posts []Post
for _, f := range dir { for _, f := range dir {
post, err := getPost(fileSystem, f) post, err := getPost(fileSystem, f.Name())
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -26,8 +26,8 @@ func NewPostsFromFS(fileSystem fs.FS) ([]Post, error) {
return posts, nil return posts, nil
} }
func getPost(fileSystem fs.FS, f fs.DirEntry) (Post, error) { func getPost(fileSystem fs.FS, fileName string) (Post, error) {
postFile, err := fileSystem.Open(f.Name()) postFile, err := fileSystem.Open(fileName)
if err != nil { if err != nil {
return Post{}, err 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 ? // 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? // 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) postData, err := io.ReadAll(postFile)
if err != nil { if err != nil {
return Post{}, err return Post{}, err