diff options
| author | Shulhan <ms@kilabit.info> | 2023-10-29 00:46:53 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-10-29 00:46:53 +0700 |
| commit | 72e51cb4606155d9a3ecc9777aeb41579045f868 (patch) | |
| tree | e0a36218e18a6ebc59beec73844c27fde4dee5d6 /lib | |
| parent | cea29b1890e2e03c4744d50c3c9d6d3dc4535bdf (diff) | |
| download | pakakeh.go-72e51cb4606155d9a3ecc9777aeb41579045f868.tar.xz | |
lib/memfs: add method Child to Node
The Child method return the child node based on its node.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/memfs/memfs_test.go | 33 | ||||
| -rw-r--r-- | lib/memfs/node.go | 10 |
2 files changed, 43 insertions, 0 deletions
diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go index f2ac87f1..6d8dbadc 100644 --- a/lib/memfs/memfs_test.go +++ b/lib/memfs/memfs_test.go @@ -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 diff --git a/lib/memfs/node.go b/lib/memfs/node.go index cbb2b5da..43f16be4 100644 --- a/lib/memfs/node.go +++ b/lib/memfs/node.go @@ -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 |
