summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-08-12 23:35:47 +0700
committerShulhan <ms@kilabit.info>2021-08-12 23:35:47 +0700
commit4aa4b560eb683d742dc7ff3136089c73f6a0023d (patch)
tree95c83604364b877ae3d42f10ca812731521237e9
parent8148fdb18dbcf22a9c9c724fc9b22b8adb5ee7dd (diff)
downloadpakakeh.go-4aa4b560eb683d742dc7ff3136089c73f6a0023d.tar.xz
lib/memfs: make the MarshalJSON on Node to return the content
Encoding the PathNode to JSON will include the content of each Node, but encoding only single Node will include the content.
-rw-r--r--lib/memfs/node.go9
-rw-r--r--lib/memfs/pathnode.go2
2 files changed, 7 insertions, 4 deletions
diff --git a/lib/memfs/node.go b/lib/memfs/node.go
index 2940fc6f..809df6cf 100644
--- a/lib/memfs/node.go
+++ b/lib/memfs/node.go
@@ -167,7 +167,7 @@ func (leaf *Node) IsDir() bool {
//
func (leaf *Node) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
- leaf.packAsJson(&buf)
+ leaf.packAsJson(&buf, true)
return buf.Bytes(), nil
}
@@ -334,7 +334,7 @@ func (leaf *Node) generateFuncName(in string) {
leaf.GenFuncName = "generate_" + syspath
}
-func (leaf *Node) packAsJson(buf *bytes.Buffer) {
+func (leaf *Node) packAsJson(buf *bytes.Buffer, withContent bool) {
_ = buf.WriteByte('{')
_, _ = fmt.Fprintf(buf, `%q:%q,`, "path", leaf.Path)
@@ -344,13 +344,16 @@ func (leaf *Node) packAsJson(buf *bytes.Buffer) {
_, _ = fmt.Fprintf(buf, `%q:%q,`, "mode_string", leaf.mode)
_, _ = fmt.Fprintf(buf, `%q:%d,`, "size", leaf.size)
_, _ = fmt.Fprintf(buf, `%q:%t,`, "is_dir", leaf.IsDir())
+ if withContent {
+ _, _ = fmt.Fprintf(buf, `%q:%q,`, "content", leaf.V)
+ }
_, _ = fmt.Fprintf(buf, `%q:[`, "childs")
for x, child := range leaf.Childs {
if x > 0 {
_ = buf.WriteByte(',')
}
- child.packAsJson(buf)
+ child.packAsJson(buf, withContent)
}
_ = buf.WriteByte(']')
diff --git a/lib/memfs/pathnode.go b/lib/memfs/pathnode.go
index 032b0dac..0449768e 100644
--- a/lib/memfs/pathnode.go
+++ b/lib/memfs/pathnode.go
@@ -67,7 +67,7 @@ func (pn *PathNode) MarshalJSON() ([]byte, error) {
}
fmt.Fprintf(&buf, "%q:", key)
node := pn.v[key]
- node.packAsJson(&buf)
+ node.packAsJson(&buf, false)
}
_ = buf.WriteByte('}')