Implement defer

Closes #8086
Closes #12589
This commit is contained in:
Bjørn Erik Pedersen
2024-06-08 11:52:22 +02:00
parent 8731d88222
commit 6cd0784e44
33 changed files with 1033 additions and 148 deletions

View File

@@ -28,12 +28,12 @@ var (
type hasBytesFs struct {
afero.Fs
shouldCheck func(name string) bool
hasBytesCallback func(name string, match bool)
pattern []byte
hasBytesCallback func(name string, match []byte)
patterns [][]byte
}
func NewHasBytesReceiver(delegate afero.Fs, shouldCheck func(name string) bool, hasBytesCallback func(name string, match bool), pattern []byte) afero.Fs {
return &hasBytesFs{Fs: delegate, shouldCheck: shouldCheck, hasBytesCallback: hasBytesCallback, pattern: pattern}
func NewHasBytesReceiver(delegate afero.Fs, shouldCheck func(name string) bool, hasBytesCallback func(name string, match []byte), patterns ...[]byte) afero.Fs {
return &hasBytesFs{Fs: delegate, shouldCheck: shouldCheck, hasBytesCallback: hasBytesCallback, patterns: patterns}
}
func (fs *hasBytesFs) UnwrapFilesystem() afero.Fs {
@@ -60,10 +60,15 @@ func (fs *hasBytesFs) wrapFile(f afero.File) afero.File {
if !fs.shouldCheck(f.Name()) {
return f
}
patterns := make([]*hugio.HasBytesPattern, len(fs.patterns))
for i, p := range fs.patterns {
patterns[i] = &hugio.HasBytesPattern{Pattern: p}
}
return &hasBytesFile{
File: f,
hbw: &hugio.HasBytesWriter{
Pattern: fs.pattern,
Patterns: patterns,
},
hasBytesCallback: fs.hasBytesCallback,
}
@@ -74,7 +79,7 @@ func (fs *hasBytesFs) Name() string {
}
type hasBytesFile struct {
hasBytesCallback func(name string, match bool)
hasBytesCallback func(name string, match []byte)
hbw *hugio.HasBytesWriter
afero.File
}
@@ -88,6 +93,10 @@ func (h *hasBytesFile) Write(p []byte) (n int, err error) {
}
func (h *hasBytesFile) Close() error {
h.hasBytesCallback(h.Name(), h.hbw.Match)
for _, p := range h.hbw.Patterns {
if p.Match {
h.hasBytesCallback(h.Name(), p.Pattern)
}
}
return h.File.Close()
}