summaryrefslogtreecommitdiff
path: root/internal/node
diff options
context:
space:
mode:
Diffstat (limited to 'internal/node')
-rw-r--r--internal/node/acl.go12
-rw-r--r--internal/node/array.go23
-rw-r--r--internal/node/bool.go9
-rw-r--r--internal/node/node.go71
-rw-r--r--internal/node/number.go9
-rw-r--r--internal/node/object.go22
-rw-r--r--internal/node/string.go9
7 files changed, 155 insertions, 0 deletions
diff --git a/internal/node/acl.go b/internal/node/acl.go
new file mode 100644
index 0000000..ca2e3dc
--- /dev/null
+++ b/internal/node/acl.go
@@ -0,0 +1,12 @@
+package node
+
+type ACL struct {
+ Read []Actor
+ Write []Actor
+ Delete []Actor
+}
+
+type Actor struct {
+ UserID string
+ Group string
+}
diff --git a/internal/node/array.go b/internal/node/array.go
new file mode 100644
index 0000000..780f99f
--- /dev/null
+++ b/internal/node/array.go
@@ -0,0 +1,23 @@
+package node
+
+import "go.neonxp.dev/json"
+
+func (n *Node) Append(v json.Node) {
+ n.arrayValue = append(n.arrayValue, v.(*Node))
+}
+
+func (n *Node) Index(i int) json.Node {
+ return n.arrayValue[i]
+}
+
+func (n *Node) SetByIndex(i int, v *Node) {
+ n.arrayValue[i] = v
+}
+
+func (n *Node) RemoveByIndex(i int) {
+ n.arrayValue = append(n.arrayValue[:i], n.arrayValue[i:]...)
+}
+
+func (n *Node) Len() int {
+ return len(n.arrayValue)
+}
diff --git a/internal/node/bool.go b/internal/node/bool.go
new file mode 100644
index 0000000..91edaf9
--- /dev/null
+++ b/internal/node/bool.go
@@ -0,0 +1,9 @@
+package node
+
+func (n *Node) SetBool(v bool) {
+ n.boolValue = v
+}
+
+func (n *Node) GetBool() bool {
+ return n.boolValue
+}
diff --git a/internal/node/node.go b/internal/node/node.go
new file mode 100644
index 0000000..b8bec52
--- /dev/null
+++ b/internal/node/node.go
@@ -0,0 +1,71 @@
+package node
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+
+ "go.neonxp.dev/json"
+ "go.neonxp.dev/objectid"
+)
+
+func Factory(typ json.NodeType) (json.Node, error) {
+ n := &Node{
+ ID: objectid.New(),
+ Type: typ,
+ }
+ switch typ {
+ case json.ObjectType:
+ n.objectValue = make(map[string]*Node)
+ case json.ArrayType:
+ n.arrayValue = make([]*Node, 0)
+ }
+ return n, nil
+}
+
+type Node struct {
+ ID objectid.ID
+ Type json.NodeType
+ Parent objectid.ID
+ stringValue string
+ numberValue float64
+ boolValue bool
+ objectValue map[string]*Node
+ arrayValue []*Node
+ ACL ACL
+}
+
+func (n *Node) String() string {
+ switch n.Type {
+ case json.NullType:
+ return "null"
+ case json.StringType:
+ return `"` + n.stringValue + `"`
+ case json.NumberType:
+ return strconv.FormatFloat(n.numberValue, 'g', 15, 64)
+ case json.BooleanType:
+ if n.boolValue {
+ return "true"
+ }
+ return "false"
+ case json.ObjectType:
+ res := make([]string, 0, len(n.objectValue))
+ for k, n := range n.objectValue {
+ res = append(res, fmt.Sprintf(`"%s":%s`, k, n.String()))
+ }
+ return fmt.Sprintf(`{%s}`, strings.Join(res, ","))
+ case json.ArrayType:
+ res := make([]string, 0, len(n.arrayValue))
+ for _, v := range n.arrayValue {
+ res = append(res, v.String())
+ }
+ return fmt.Sprintf(`[%s]`, strings.Join(res, ","))
+ }
+ return ""
+}
+
+func (n *Node) SetParent(v json.Node) {
+ if v != nil {
+ n.Parent = v.(*Node).ID
+ }
+}
diff --git a/internal/node/number.go b/internal/node/number.go
new file mode 100644
index 0000000..055ae00
--- /dev/null
+++ b/internal/node/number.go
@@ -0,0 +1,9 @@
+package node
+
+func (n *Node) SetNumber(v float64) {
+ n.numberValue = v
+}
+
+func (n *Node) GetNumber() float64 {
+ return n.numberValue
+}
diff --git a/internal/node/object.go b/internal/node/object.go
new file mode 100644
index 0000000..35a3141
--- /dev/null
+++ b/internal/node/object.go
@@ -0,0 +1,22 @@
+package node
+
+import "go.neonxp.dev/json"
+
+func (n *Node) SetKeyValue(k string, v json.Node) {
+ n.objectValue[k] = v.(*Node)
+}
+
+func (n *Node) GetByKey(k string) (json.Node, bool) {
+ node, ok := n.objectValue[k]
+ return node, ok
+}
+
+func (n *Node) Merge(n2 *Node) {
+ for k, v := range n2.objectValue {
+ n.objectValue[k] = v
+ }
+}
+
+func (n *Node) RemoveByKey(k string) {
+ delete(n.objectValue, k)
+}
diff --git a/internal/node/string.go b/internal/node/string.go
new file mode 100644
index 0000000..90de4a7
--- /dev/null
+++ b/internal/node/string.go
@@ -0,0 +1,9 @@
+package node
+
+func (n *Node) SetString(v string) {
+ n.stringValue = v
+}
+
+func (n *Node) GetString() string {
+ return n.stringValue
+}