hack-pad / hackpadfs

An extensible file system abstraction for Go. File systems, composable interfaces, and test suites.
Apache License 2.0
251 stars 11 forks source link

Return concrete *os.FS from Sub() and Subvolume() #40

Open antoineco opened 7 months ago

antoineco commented 7 months ago

First of all thanks for sharing this great project!

While switching between mem.FS and os.FS for different use cases, I noticed that the value returned by (*os.FS).Sub() could not be passed verbatim to receivers that accept an interface such as hpfs.RemoveFS. This is because both Sub() and Subvolume() return a generic hpfs.FS interface instead of the concrete *os.FS type.

It is a common practice in Go to "accept interfaces, return concrete types" and, since there is no ambiguity here, I believe that returning the concrete *os.FS is the right thing to do. It avoids awkward assertions such as:

fs := os.NewFS()
fsi, err := fs.Sub(webroot)
if err != nil { /* ... */ }
fs = fsi.(*os.FS)

myfunc(fs)