Skip to content

Commit

Permalink
replace MemFilesAsFileReaders with generic AsFileReaders
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Nov 26, 2024
1 parent 1bc9d0e commit 4dde970
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
18 changes: 18 additions & 0 deletions filereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 0 additions & 12 deletions memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4dde970

Please sign in to comment.