Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

feat(fsnode): add type helper #24

Merged
merged 2 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions mfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,3 +1182,31 @@ func TestTruncateAndWrite(t *testing.T) {
}
}
}

func TestFSNodeType(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ds, rt := setupRoot(ctx, t)

// check for IsDir
nd := dag.NodeWithData(ft.FolderPBData())
di, err := NewDirectory(ctx, "test", nd, rt.GetDirectory(), ds)
if err != nil {
t.Fatal(err)
}
ret := IsDir(di)
if !ret {
t.Fatal("FSNode type should be dir, but not")
}

// check for IsFile
fnd := dag.NodeWithData(ft.FilePBData(nil, 0))
fi, err := NewFile("test", fnd, rt.GetDirectory(), ds)
if err != nil {
t.Fatal(err)
}
ret = IsFile(fi)
if !ret {
t.Fatal("FSNode type should be file, but not")
}
}
10 changes: 10 additions & 0 deletions system.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ type FSNode interface {
Type() NodeType
}

// IsDir checks whether the FSNode is dir type
func IsDir(fsn FSNode) bool {
return fsn.Type() == TDir
}

// IsFile checks whether the FSNode is file type
func IsFile(fsn FSNode) bool {
return fsn.Type() == TFile
}

// Root represents the root of a filesystem tree.
type Root struct {

Expand Down