diff options
author | Matthew Lemon <y@yulqen.org> | 2024-04-20 20:13:20 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-04-20 20:13:20 +0100 |
commit | 00c3d5dac0545133831f41ea6692aa53d8eb4cde (patch) | |
tree | cbdb85d7dd97fb809252c9d77f8057658b848971 | |
parent | 6925bc8676fb24fad7bebe7b7b921cc3a5bc5ab9 (diff) |
Nice refactoring
The FilePreparer interface is now nicer.
-rw-r--r-- | cmd/dbasik-api/return.go | 43 | ||||
-rw-r--r-- | cmd/dbasik-api/return_test.go | 6 |
2 files changed, 36 insertions, 13 deletions
diff --git a/cmd/dbasik-api/return.go b/cmd/dbasik-api/return.go index f172452..2fa6793 100644 --- a/cmd/dbasik-api/return.go +++ b/cmd/dbasik-api/return.go @@ -117,16 +117,41 @@ func contains(slice []string, str string) bool { } type FilePreparer interface { - Prepare(filePath string) error + Prepare() ([]string, error) } -type DirectoryFilePackage struct { +type FileSource struct { FilePath string } +type DirectoryFilePackage struct { + FileSource +} + +func PrepareFiles(fp FilePreparer) ([]string, error) { + ch := make(chan string, 100) + + go func() { + defer close(ch) + files, err := fp.Prepare() + if err != nil { + ch <- err.Error() + } + + for _, f := range files { + ch <- f + } + }() + + var files []string + for f := range ch { + files = append(files, f) + } + + return files, nil +} + func (fp *DirectoryFilePackage) Prepare() ([]string, error) { - // return a slice of the files inside the directory pointed to by fh.FilePath - // and an error if any files, err := filepath.Glob(fp.FilePath + "/*") if err != nil { return nil, err @@ -135,12 +160,10 @@ func (fp *DirectoryFilePackage) Prepare() ([]string, error) { } type ZipFilePackage struct { - FilePath string + FileSource } func (fp *ZipFilePackage) Prepare() ([]string, error) { - // return a slice of the files from inside the zip file pointed to by fh.FilePath - // and an error if any files, err := zip.OpenReader(fp.FilePath) if err != nil { return nil, err @@ -153,10 +176,12 @@ func (fp *ZipFilePackage) Prepare() ([]string, error) { return out, nil } +// NewDirectoryFilePackage creates a new DirectoryFilePackage object with the given filePath to the directory func NewDirectoryFilePackage(filePath string) *DirectoryFilePackage { - return &DirectoryFilePackage{FilePath: filePath} + return &DirectoryFilePackage{FileSource{FilePath: filePath}} } +// NewZipFilePackage creates a new ZipFilePackage object with the given filePath to the zip file func NewZipFilePackage(filePath string) *ZipFilePackage { - return &ZipFilePackage{FilePath: filePath} + return &ZipFilePackage{FileSource{FilePath: filePath}} } diff --git a/cmd/dbasik-api/return_test.go b/cmd/dbasik-api/return_test.go index d6c9daa..2aa0ac9 100644 --- a/cmd/dbasik-api/return_test.go +++ b/cmd/dbasik-api/return_test.go @@ -170,11 +170,10 @@ func TestParseXLSX(t *testing.T) { func TestPrepareFiles(t *testing.T) { fp := NewDirectoryFilePackage("../../testdata") - files, err := fp.Prepare() + files, err := PrepareFiles(fp) if err != nil { t.Error(err) } - t.Log(files) if !slices.Contains(files, "../../testdata/valid_excel.xlsx") { t.Errorf("Prepare() did not return ../../testdata/valid_excel.xlsx") } @@ -182,11 +181,10 @@ func TestPrepareFiles(t *testing.T) { func TestUnzipFiles(t *testing.T) { fp := NewZipFilePackage("../../testdata/test.zip") - files, err := fp.Prepare() + files, err := PrepareFiles(fp) if err != nil { t.Error(err) } - t.Log(files) if !slices.Contains(files, "valid_excel.xlsx") { t.Errorf("Prepare() did not return test.xlsx") } |