fix: Update FsFileInfo to use pointer slices for children and remove redundant getter methods

This commit is contained in:
Marvin Zhang
2025-07-23 09:42:07 +08:00
parent 3c3ff09723
commit b4288b08a5
3 changed files with 16 additions and 56 deletions

View File

@@ -6,56 +6,16 @@ import (
)
type FsFileInfo struct {
Name string `json:"name"` // file name
Path string `json:"path"` // file path
FullPath string `json:"full_path"` // file full path
Extension string `json:"extension"` // file extension
IsDir bool `json:"is_dir"` // whether it is directory
FileSize int64 `json:"file_size"` // file size (bytes)
Children []FsFileInfo `json:"children"` // children for subdirectory
ModTime time.Time `json:"mod_time"` // modification time
Mode os.FileMode `json:"mode"` // file mode
Hash string `json:"hash"` // file hash
}
func (f *FsFileInfo) GetName() string {
return f.Name
}
func (f *FsFileInfo) GetPath() string {
return f.Path
}
func (f *FsFileInfo) GetFullPath() string {
return f.FullPath
}
func (f *FsFileInfo) GetExtension() string {
return f.Extension
}
func (f *FsFileInfo) GetIsDir() bool {
return f.IsDir
}
func (f *FsFileInfo) GetFileSize() int64 {
return f.FileSize
}
func (f *FsFileInfo) GetModTime() time.Time {
return f.ModTime
}
func (f *FsFileInfo) GetMode() os.FileMode {
return f.Mode
}
func (f *FsFileInfo) GetHash() string {
return f.Hash
}
func (f *FsFileInfo) GetChildren() []FsFileInfo {
return f.Children
Name string `json:"name"` // file name
Path string `json:"path"` // file path
FullPath string `json:"full_path"` // file full path
Extension string `json:"extension"` // file extension
IsDir bool `json:"is_dir"` // whether it is directory
FileSize int64 `json:"file_size"` // file size (bytes)
Children []*FsFileInfo `json:"children"` // children for subdirectory
ModTime time.Time `json:"mod_time"` // modification time
Mode os.FileMode `json:"mode"` // file mode
Hash string `json:"hash"` // file hash
}
type FsFileInfoMap map[string]FsFileInfo

View File

@@ -67,15 +67,15 @@ func (svc *Service) List(path string) (files []entity.FsFileInfo, err error) {
}
if parentDir := filepath.Dir(p); parentDir != p && dirMap[parentDir] != nil {
dirMap[parentDir].Children = append(dirMap[parentDir].Children, *fi)
dirMap[parentDir].Children = append(dirMap[parentDir].Children, fi)
}
return nil
})
if rootInfo, ok := dirMap[fullPath]; ok {
for _, info := range rootInfo.GetChildren() {
files = append(files, info)
for _, info := range rootInfo.Children {
files = append(files, *info)
}
}

View File

@@ -42,7 +42,7 @@ func TestService_List(t *testing.T) {
// Use a map to verify presence and characteristics of files/directories to avoid order issues
items := make(map[string]bool)
for _, item := range files {
items[item.GetName()] = item.GetIsDir()
items[item.Name] = item.IsDir
}
_, file1Exists := items["file1.txt"]
@@ -55,8 +55,8 @@ func TestService_List(t *testing.T) {
assert.True(t, subdirExists)
assert.True(t, emptyExists) // Verify that the empty directory is included
if subdirExists && len(files[2].GetChildren()) > 0 {
assert.Equal(t, "file3.txt", files[2].GetChildren()[0].GetName())
if subdirExists && len(files[2].Children) > 0 {
assert.Equal(t, "file3.txt", files[2].Children[0].Name)
}
}