Skip to content

Commit

Permalink
Add FileIntoCallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatthm committed Sep 16, 2024
1 parent 6df45ae commit cc65aa8
Show file tree
Hide file tree
Showing 7 changed files with 495 additions and 143 deletions.
84 changes: 42 additions & 42 deletions file_callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,123 +26,123 @@ type FileCallbacks struct {
}

// Close satisfies the afero.File interface.
func (f *FileCallbacks) Close() error {
func (f FileCallbacks) Close() error {
return f.CloseFunc()
}

// Name satisfies the afero.File interface.
func (f *FileCallbacks) Name() string {
func (f FileCallbacks) Name() string {
return f.NameFunc()
}

// Read satisfies the afero.File interface.
func (f *FileCallbacks) Read(p []byte) (int, error) {
func (f FileCallbacks) Read(p []byte) (int, error) {
return f.ReadFunc(p)
}

// ReadAt satisfies the afero.File interface.
func (f *FileCallbacks) ReadAt(p []byte, off int64) (int, error) {
func (f FileCallbacks) ReadAt(p []byte, off int64) (int, error) {
return f.ReadAtFunc(p, off)
}

// Readdir satisfies the afero.File interface.
func (f *FileCallbacks) Readdir(count int) ([]fs.FileInfo, error) {
func (f FileCallbacks) Readdir(count int) ([]fs.FileInfo, error) {
return f.ReaddirFunc(count)
}

// Readdirnames satisfies the afero.File interface.
func (f *FileCallbacks) Readdirnames(n int) ([]string, error) {
func (f FileCallbacks) Readdirnames(n int) ([]string, error) {
return f.ReaddirnamesFunc(n)
}

// Seek satisfies the afero.File interface.
func (f *FileCallbacks) Seek(offset int64, whence int) (int64, error) {
func (f FileCallbacks) Seek(offset int64, whence int) (int64, error) {
return f.SeekFunc(offset, whence)
}

// Stat satisfies the afero.File interface.
func (f *FileCallbacks) Stat() (fs.FileInfo, error) {
func (f FileCallbacks) Stat() (fs.FileInfo, error) {
return f.StatFunc()
}

// Sync satisfies the afero.File interface.
func (f *FileCallbacks) Sync() error {
func (f FileCallbacks) Sync() error {
return f.SyncFunc()
}

// Truncate satisfies the afero.File interface.
func (f *FileCallbacks) Truncate(size int64) error {
func (f FileCallbacks) Truncate(size int64) error {
return f.TruncateFunc(size)
}

// Write satisfies the afero.File interface.
func (f *FileCallbacks) Write(p []byte) (int, error) {
func (f FileCallbacks) Write(p []byte) (int, error) {
return f.WriteFunc(p)
}

// WriteAt satisfies the afero.File interface.
func (f *FileCallbacks) WriteAt(p []byte, off int64) (int, error) {
func (f FileCallbacks) WriteAt(p []byte, off int64) (int, error) {
return f.WriteAtFunc(p, off)
}

// WriteString satisfies the afero.File interface.
func (f *FileCallbacks) WriteString(s string) (int, error) {
func (f FileCallbacks) WriteString(s string) (int, error) {
return f.WriteStringFunc(s)
}

// OverrideFile overrides the file methods with the given callbacks.
func OverrideFile(file afero.File, callbacks FileCallbacks) *FileCallbacks { //nolint: cyclop,dupl
if callbacks.CloseFunc == nil {
callbacks.CloseFunc = file.Close
// OverrideFile overrides the afero.File methods with the provided callbacks.
func OverrideFile(file afero.File, c FileCallbacks) FileCallbacks { //nolint: cyclop,dupl
if c.CloseFunc == nil {
c.CloseFunc = file.Close
}

if callbacks.NameFunc == nil {
callbacks.NameFunc = file.Name
if c.NameFunc == nil {
c.NameFunc = file.Name
}

if callbacks.ReadFunc == nil {
callbacks.ReadFunc = file.Read
if c.ReadFunc == nil {
c.ReadFunc = file.Read
}

if callbacks.ReadAtFunc == nil {
callbacks.ReadAtFunc = file.ReadAt
if c.ReadAtFunc == nil {
c.ReadAtFunc = file.ReadAt
}

if callbacks.ReaddirFunc == nil {
callbacks.ReaddirFunc = file.Readdir
if c.ReaddirFunc == nil {
c.ReaddirFunc = file.Readdir
}

if callbacks.ReaddirnamesFunc == nil {
callbacks.ReaddirnamesFunc = file.Readdirnames
if c.ReaddirnamesFunc == nil {
c.ReaddirnamesFunc = file.Readdirnames
}

if callbacks.SeekFunc == nil {
callbacks.SeekFunc = file.Seek
if c.SeekFunc == nil {
c.SeekFunc = file.Seek
}

if callbacks.StatFunc == nil {
callbacks.StatFunc = file.Stat
if c.StatFunc == nil {
c.StatFunc = file.Stat
}

if callbacks.SyncFunc == nil {
callbacks.SyncFunc = file.Sync
if c.SyncFunc == nil {
c.SyncFunc = file.Sync
}

if callbacks.TruncateFunc == nil {
callbacks.TruncateFunc = file.Truncate
if c.TruncateFunc == nil {
c.TruncateFunc = file.Truncate
}

if callbacks.WriteFunc == nil {
callbacks.WriteFunc = file.Write
if c.WriteFunc == nil {
c.WriteFunc = file.Write
}

if callbacks.WriteAtFunc == nil {
callbacks.WriteAtFunc = file.WriteAt
if c.WriteAtFunc == nil {
c.WriteAtFunc = file.WriteAt
}

if callbacks.WriteStringFunc == nil {
callbacks.WriteStringFunc = file.WriteString
if c.WriteStringFunc == nil {
c.WriteStringFunc = file.WriteString
}

return &callbacks
return c
}
Loading

0 comments on commit cc65aa8

Please sign in to comment.