-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathoptions.go
34 lines (29 loc) · 974 Bytes
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package memfs
type Option interface {
setOption(*fsOption)
}
type fsOption struct {
openHook func(path string, existingContent []byte, origErr error) ([]byte, error)
}
type openHookOption struct {
hook func(string, []byte, error) ([]byte, error)
}
func (o *openHookOption) setOption(fsOpt *fsOption) {
fsOpt.openHook = o.hook
}
// WithOpenHook returns an Option that sets a hook function to be called
// when opening files in the MemFS.
//
// The hook function takes three parameters:
// - path: the path of the file being opened
// - content: the original content of the file (may be nil if the file doesn't exist)
// - origError: the original error returned when trying to open the file (may be nil)
//
// The hook function returns:
// - []byte: the new content of the file
// - error: any error that occurred during the hook's execution
func WithOpenHook(f func(string, []byte, error) ([]byte, error)) Option {
return &openHookOption{
hook: f,
}
}