Skip to content

Commit

Permalink
lib/memfs: add method Child to Node
Browse files Browse the repository at this point in the history
The Child method return the child node based on its node.
  • Loading branch information
shuLhan committed Oct 28, 2023
1 parent cea29b1 commit 72e51cb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/memfs/memfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,39 @@ func TestMemFS_MarshalJSON(t *testing.T) {
}
}

func TestMemFS_RemoveChild(t *testing.T) {
var (
opts = &Options{
Root: `testdata`,
MaxFileSize: -1,
}
mfs *MemFS
err error
)

mfs, err = New(opts)
if err != nil {
t.Fatal(err)
}

var child = mfs.Root.Child(`plain`)
if child == nil {
t.Fatal(`Expecting child "plain", got nil`)
}

var nodeRemoved = mfs.RemoveChild(mfs.Root, child)
if nodeRemoved == nil {
t.Fatal(`Expecting child "plain", got nil`)
}

test.Assert(t, `RemoveChild`, child, nodeRemoved)

child = mfs.Root.Child(`plain`)
if child != nil {
t.Fatalf(`Expecting child "plain" has been removed, got %v`, child)
}
}

func TestMemFS_isIncluded(t *testing.T) {
cases := []struct {
desc string
Expand Down
10 changes: 10 additions & 0 deletions lib/memfs/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ func (node *Node) AddChild(child *Node) {
child.Parent = node
}

// Child return the child node based on its name.
func (node *Node) Child(name string) (cnode *Node) {
for _, cnode = range node.Childs {
if cnode.name == name {
return cnode
}
}
return nil
}

// Close reset the offset position back to zero.
func (node *Node) Close() error {
node.off = 0
Expand Down

0 comments on commit 72e51cb

Please sign in to comment.