summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-10-09 20:45:17 +0700
committerShulhan <ms@kilabit.info>2021-10-09 22:22:02 +0700
commit0b6d689e05ce4efd03c482aa3667652411adcb3b (patch)
tree4e4c6669f89fabb49a4f3cd1fa26639c19db68b4
parent7e255b79c1c1afaffb1b6f8790f7d77b382acb69 (diff)
downloadpakakeh.go-0b6d689e05ce4efd03c482aa3667652411adcb3b.tar.xz
lib/memfs: remove PathNode "f" field
Previously, the PathNode has two fields to store the node in memory, one is "v" that store map of path to *Node and another is "f" that store the map of path to function that return a *Node. The "f" is used to handle embedding Go generated code, but since the template now can handle generated dynamic path and Node this field is not used anymore.
-rw-r--r--lib/memfs/embed.go18
-rw-r--r--lib/memfs/embed_test/memfs_embed_test.go6
-rw-r--r--lib/memfs/memfs.go43
-rw-r--r--lib/memfs/memfs_test.go5
-rw-r--r--lib/memfs/pathnode.go68
-rw-r--r--lib/memfs/template.go5
6 files changed, 57 insertions, 88 deletions
diff --git a/lib/memfs/embed.go b/lib/memfs/embed.go
index 3f66f57b..918e0921 100644
--- a/lib/memfs/embed.go
+++ b/lib/memfs/embed.go
@@ -17,10 +17,10 @@ const (
)
type generateData struct {
- Opts *Options
- VarName string
- Node *Node
- Nodes map[string]*Node
+ Opts *Options
+ VarName string
+ Node *Node
+ PathNode *PathNode
}
//
@@ -54,9 +54,9 @@ func (mfs *MemFS) GoEmbed(pkgName, varName, out, contentEncoding string) (err er
out = DefaultEmbedGoFileName
}
genData := &generateData{
- Opts: mfs.Opts,
- VarName: varName,
- Nodes: mfs.PathNodes.v,
+ Opts: mfs.Opts,
+ VarName: varName,
+ PathNode: mfs.PathNodes,
}
tmpl, err := generateTemplate()
@@ -87,11 +87,11 @@ func (mfs *MemFS) GoEmbed(pkgName, varName, out, contentEncoding string) (err er
// Ignore and delete the file from map if its the output
// itself.
if strings.HasSuffix(names[x], out) {
- delete(mfs.PathNodes.v, names[x])
+ mfs.PathNodes.Delete(names[x])
continue
}
- genData.Node = mfs.PathNodes.v[names[x]]
+ genData.Node = mfs.PathNodes.Get(names[x])
err = tmpl.ExecuteTemplate(f, templateNameGenerateNode, genData)
if err != nil {
diff --git a/lib/memfs/embed_test/memfs_embed_test.go b/lib/memfs/embed_test/memfs_embed_test.go
index f188ebe8..e1b62f5e 100644
--- a/lib/memfs/embed_test/memfs_embed_test.go
+++ b/lib/memfs/embed_test/memfs_embed_test.go
@@ -54,16 +54,18 @@ func TestGeneratePathNode(t *testing.T) {
}}
for _, c := range cases {
+ t.Log(c.path)
+
got, err := memFS.Get(c.path)
if err != nil {
- test.Assert(t, c.path+": error", c.expError, err.Error())
+ test.Assert(t, "error", c.expError, err.Error())
continue
}
childs := got.Childs
got.Childs = nil
got.SetModTime(zeroTime)
- test.Assert(t, c.path+": Node", c.exp, got)
+ test.Assert(t, "Node", c.exp, got)
got.Childs = childs
}
}
diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go
index a6751ddf..fe924439 100644
--- a/lib/memfs/memfs.go
+++ b/lib/memfs/memfs.go
@@ -54,9 +54,7 @@ type MemFS struct {
//
func Merge(params ...*MemFS) (merged *MemFS) {
merged = &MemFS{
- PathNodes: &PathNode{
- v: make(map[string]*Node),
- },
+ PathNodes: NewPathNode(),
Root: &Node{
SysPath: "..",
Path: "/",
@@ -69,8 +67,8 @@ func Merge(params ...*MemFS) (merged *MemFS) {
for _, mfs := range params {
for _, child := range mfs.Root.Childs {
- _, exist := merged.PathNodes.v[child.Path]
- if exist {
+ gotNode := merged.PathNodes.Get(child.Path)
+ if gotNode != nil {
continue
}
merged.Root.AddChild(child)
@@ -80,9 +78,9 @@ func Merge(params ...*MemFS) (merged *MemFS) {
if path == "/" {
continue
}
- _, exist := merged.PathNodes.v[path]
- if !exist {
- merged.PathNodes.v[path] = mfs.PathNodes.Get(path)
+ gotNode := merged.PathNodes.Get(path)
+ if gotNode == nil {
+ merged.PathNodes.Set(path, mfs.PathNodes.Get(path))
}
}
}
@@ -102,11 +100,8 @@ func New(opts *Options) (mfs *MemFS, err error) {
opts.init()
mfs = &MemFS{
- PathNodes: &PathNode{
- v: make(map[string]*Node),
- f: nil,
- },
- Opts: opts,
+ PathNodes: NewPathNode(),
+ Opts: opts,
}
for _, inc := range opts.Includes {
@@ -326,22 +321,7 @@ func (mfs *MemFS) Get(path string) (node *Node, err error) {
// ListNames list all files in memory sorted by name.
//
func (mfs *MemFS) ListNames() (paths []string) {
- paths = make([]string, 0, len(mfs.PathNodes.f)+len(mfs.PathNodes.v))
-
- for k := range mfs.PathNodes.f {
- paths = append(paths, k)
- }
-
- vpaths := mfs.PathNodes.Paths()
- for _, k := range vpaths {
- _, ok := mfs.PathNodes.f[k]
- if !ok {
- paths = append(paths, k)
- }
- }
-
- sort.Strings(paths)
-
+ paths = mfs.PathNodes.Paths()
return paths
}
@@ -560,10 +540,7 @@ func (mfs *MemFS) mount() (err error) {
logp := "mount"
if mfs.PathNodes == nil {
- mfs.PathNodes = &PathNode{
- v: make(map[string]*Node),
- f: nil,
- }
+ mfs.PathNodes = NewPathNode()
}
err = mfs.createRoot()
diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go
index 65da48af..fb259bd3 100644
--- a/lib/memfs/memfs_test.go
+++ b/lib/memfs/memfs_test.go
@@ -58,9 +58,8 @@ func TestNew(t *testing.T) {
expErr string
expMapKeys []string
}{{
- desc: "With empty dir",
- expErr: "open : no such file or directory",
- expMapKeys: make([]string, 0),
+ desc: "With empty dir",
+ expErr: "open : no such file or directory",
}, {
desc: "With file",
opts: Options{
diff --git a/lib/memfs/pathnode.go b/lib/memfs/pathnode.go
index 68f58bae..f702c01a 100644
--- a/lib/memfs/pathnode.go
+++ b/lib/memfs/pathnode.go
@@ -17,7 +17,6 @@ import (
type PathNode struct {
mu sync.Mutex
v map[string]*Node
- f map[string]func() *Node
}
//
@@ -26,7 +25,6 @@ type PathNode struct {
func NewPathNode() *PathNode {
return &PathNode{
v: make(map[string]*Node),
- f: make(map[string]func() *Node),
}
}
@@ -42,52 +40,38 @@ func (pn *PathNode) Delete(path string) {
//
// Get the node by path, or nil if path is not exist.
//
-func (pn *PathNode) Get(path string) *Node {
+func (pn *PathNode) Get(path string) (node *Node) {
pn.mu.Lock()
defer pn.mu.Unlock()
-
- node, ok := pn.v[path]
- if ok {
- return node
- }
- if pn.f != nil {
- f, ok := pn.f[path]
- if ok {
- node = f()
- return node
- }
+ if pn.v == nil {
+ return nil
}
- return nil
+ return pn.v[path]
}
func (pn *PathNode) MarshalJSON() ([]byte, error) {
- pn.mu.Lock()
- defer pn.mu.Unlock()
-
- // Merge the path with function to node into v.
- for k, fn := range pn.f {
- pn.v[k] = fn()
- }
-
- buf := bytes.Buffer{}
-
- // Sort the paths.
- keys := make([]string, 0, len(pn.v))
- for path := range pn.v {
- keys = append(keys, path)
- }
- sort.Strings(keys)
+ var (
+ buf bytes.Buffer
+ paths = pn.Paths()
+ x int
+ path string
+ node *Node
+ )
+ pn.mu.Lock()
_ = buf.WriteByte('{')
- for x, key := range keys {
+ for x, path = range paths {
if x > 0 {
_ = buf.WriteByte(',')
}
- fmt.Fprintf(&buf, "%q:", key)
- node := pn.v[key]
- node.packAsJson(&buf, 0)
+ fmt.Fprintf(&buf, "%q:", path)
+ node = pn.v[path]
+ if node != nil {
+ node.packAsJson(&buf, 0)
+ }
}
_ = buf.WriteByte('}')
+ pn.mu.Unlock()
return buf.Bytes(), nil
}
@@ -96,8 +80,12 @@ func (pn *PathNode) MarshalJSON() ([]byte, error) {
// Nodes return all the nodes.
//
func (pn *PathNode) Nodes() (nodes []*Node) {
+ var (
+ node *Node
+ )
+
pn.mu.Lock()
- for _, node := range pn.v {
+ for _, node = range pn.v {
nodes = append(nodes, node)
}
pn.mu.Unlock()
@@ -105,13 +93,15 @@ func (pn *PathNode) Nodes() (nodes []*Node) {
}
//
-// Paths return all the nodes keys as list of path.
+// Paths return all the nodes paths sorted in ascending order.
//
func (pn *PathNode) Paths() (paths []string) {
+ var path string
pn.mu.Lock()
- for key := range pn.v {
- paths = append(paths, key)
+ for path = range pn.v {
+ paths = append(paths, path)
}
+ sort.Strings(paths)
pn.mu.Unlock()
return paths
}
diff --git a/lib/memfs/template.go b/lib/memfs/template.go
index b90d5d18..861e6ea5 100644
--- a/lib/memfs/template.go
+++ b/lib/memfs/template.go
@@ -103,9 +103,10 @@ func init() {
},
}
-{{- range $path, $node := .Nodes}}
+{{- range $x, $path := .PathNode.Paths }}
+ {{- $node := $.PathNode.Get $path }}
{{$varname}}.PathNodes.Set("{{$path}}",
- _{{$varname}}_getNode({{$varname}}, "{{$path}}", {{$node.GenFuncName}}))
+ _{{$varname}}_getNode({{$varname}}, "{{$path}}", {{ $node.GenFuncName }}))
{{- end}}
{{$varname}}.Root = {{$varname}}.PathNodes.Get("/")