From 4dde97000fe016496cf2a999d4382c066ffdc56f Mon Sep 17 00:00:00 2001 From: Erik Unger Date: Tue, 26 Nov 2024 14:33:55 +0100 Subject: [PATCH] replace MemFilesAsFileReaders with generic AsFileReaders --- filereader.go | 18 ++++++++++++++++++ memfile.go | 12 ------------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/filereader.go b/filereader.go index 65a301a..17a9f08 100644 --- a/filereader.go +++ b/filereader.go @@ -76,6 +76,24 @@ type FileReader interface { GobEncode() ([]byte, error) } +// AsFileReaders converts the passed files to []FileReader. +// If the passed files are already of type []FileReader +// then they are returned as is. +func AsFileReaders[F FileReader](files []F) []FileReader { + if len(files) == 0 { + return nil + } + fileReaders, ok := any(files).([]FileReader) + if ok { + return fileReaders + } + fileReaders = make([]FileReader, len(files)) + for i, memFile := range files { + fileReaders[i] = memFile + } + return fileReaders +} + // FileReaderNameIndex returns the slice index of the first FileReader // with the passed filename returned from its Name method // or -1 in case of no match. diff --git a/memfile.go b/memfile.go index 8eea130..e32f6ac 100644 --- a/memfile.go +++ b/memfile.go @@ -134,18 +134,6 @@ func ReadAllMemFile(ctx context.Context, r io.Reader, name string) (MemFile, err return MemFile{FileName: name, FileData: data}, nil } -// MemFilesAsFileReaders converts []MemFile to []FileReader -func MemFilesAsFileReaders(memFiles []MemFile) []FileReader { - if len(memFiles) == 0 { - return nil - } - fileReaders := make([]FileReader, len(memFiles)) - for i, memFile := range memFiles { - fileReaders[i] = memFile - } - return fileReaders -} - // String returns the metadata of the file formatted as a string. // String implements the fmt.Stringer interface. func (f MemFile) String() string {