英文:
Wrapping a `fs.FS` in my own implementation of `fs.FS` makes `fs.Glob()` return no matches
问题
你的noLineBreakFile实现有一个问题,导致fs.Glob()无法返回匹配项。问题在于你在Read()方法中修改了传入的字节切片p,但没有更新返回的字节数n。这导致fs.Glob()无法正确解析文件内容并返回匹配项。
要解决这个问题,你需要在修改p之后更新n的值,确保它反映了实际读取的字节数。以下是修复后的代码:
func (f *noLineBreakFile) Read(p []byte) (n int, err error) {
	n, err = f.file.Read(p)
	pp := bytes.ReplaceAll(p[:n], []byte{'\n'}, []byte{' '})
	copy(p[:n], pp)
	return n, err
}
通过将bytes.ReplaceAll()应用于p[:n],你只会修改实际读取的字节,然后将修改后的内容复制回p[:n]。最后,确保返回正确的字节数n。
这样修改后,fs.Glob(&noLineBreakFS{embedFS})应该能够返回正确的匹配项了。
英文:
I embed some files in my application and access them using a embed.FS. At some point I use fs.Glob(embedFS, ...) in my code. Everything is working as expected. Now I have the requirement to replace newlines with whitespaces before proceeding to process a files contents. I could read the whole file and do something like bytes.ReplaceAll(...) but I wanted to make this a bit nicer and not inflict the requirement of doing the replacement when working with my package (although I could hide that from the user). I decided to implement a wrapper around fs.FS (and fs.File) that deals with the replacing while reading the file. But my implementation breaks fs.Glob() as it does not return any matches:
type noLineBreakFile struct {
	file fs.File
}
func (f *noLineBreakFile) Stat() (fs.FileInfo, error) {
	return f.file.Stat()
}
func (f *noLineBreakFile) Read(p []byte) (n int, err error) {
	n, err = f.file.Read(p)
	pp := bytes.ReplaceAll(p, []byte{'\n'}, []byte{' '})
	copy(p, pp)
	return
}
func (f *noLineBreakFile) Close() error {
	return f.file.Close()
}
type noLineBreakFS struct {
	fs fs.FS
}
func (fs *noLineBreakFS) Open(name string) (fs.File, error) {
	f, err := fs.fs.Open(name)
	if err != nil {
		return nil, err
	}
	return &noLineBreakFile{f}, nil // <- returning f without the wrapper works fine
}
//go:embed *.tmpl
var embedFS embed.FS
func main() {
	matches, err := fs.Glob(embedFS) // Works fine ...
	fmt.Println(matches, err)
	matches, err = fs.Glob(&noLineBreakFS{embedFS}) // No matches!
	fmt.Println(matches, err)
}
What is the problem with my implementation of fs.File (noLineBreakFile)?
答案1
得分: 3
实现ReadDirFile函数,以便Glob可以读取目录条目。
func (nfs *noLineBreakFS) ReadDir(name string) ([]fs.DirEntry, error) {
    return fs.ReadDir(nfs.fs, name)
}
英文:
Implement ReadDirFile so that Glob can read the directory entries.
func (nfs *noLineBreakFS) ReadDir(name string) ([]fs.DirEntry, error) {
	return fs.ReadDir(nfs.fs, name)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论