将`fs.FS`包装在我自己实现的`fs.FS`中会导致`fs.Glob()`返回没有匹配项。

huangapple go评论78阅读模式
英文:

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)
}

huangapple
  • 本文由 发表于 2022年7月20日 21:50:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/73052897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定