aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNeonXP <i@neonxp.dev>2022-11-21 04:46:32 +0300
committerNeonXP <i@neonxp.dev>2022-11-21 04:46:32 +0300
commit0d431048d3e68609b90306efd9ed015143ae651f (patch)
tree2a5fe21654bb9af02a0419722f2cd80d588465a2
parent4054a50ce4232ebfc4dae043863ad96d46b25b43 (diff)
added set method
-rw-r--r--model/arrayNode.go9
-rw-r--r--model/booleanNode.go11
-rw-r--r--model/node.go1
-rw-r--r--model/nullNode.go4
-rw-r--r--model/numberNode.go15
-rw-r--r--model/objectNode.go15
-rw-r--r--model/stringNode.go11
7 files changed, 60 insertions, 6 deletions
diff --git a/model/arrayNode.go b/model/arrayNode.go
index 203e01c..319f223 100644
--- a/model/arrayNode.go
+++ b/model/arrayNode.go
@@ -30,6 +30,15 @@ func (n *ArrayNode) MarshalJSON() ([]byte, error) {
}, []byte("")), nil
}
+func (n *ArrayNode) Set(v any) error {
+ val, ok := v.(NodeArrayValue)
+ if !ok {
+ return fmt.Errorf("%v is not array", v)
+ }
+ n.Value = val
+ return nil
+}
+
func (n *ArrayNode) Index(idx int) (Node, error) {
if len(n.Value) <= idx {
return nil, fmt.Errorf("index %d out of range [0...%d]", idx, len(n.Value)-1)
diff --git a/model/booleanNode.go b/model/booleanNode.go
index bc2d60e..aba286e 100644
--- a/model/booleanNode.go
+++ b/model/booleanNode.go
@@ -1,5 +1,7 @@
package model
+import "fmt"
+
type BooleanNode struct {
Value bool
}
@@ -14,3 +16,12 @@ func (n *BooleanNode) MarshalJSON() ([]byte, error) {
}
return []byte("false"), nil
}
+
+func (n *BooleanNode) Set(v any) error {
+ val, ok := v.(bool)
+ if !ok {
+ return fmt.Errorf("%v is not boolean", v)
+ }
+ n.Value = val
+ return nil
+}
diff --git a/model/node.go b/model/node.go
index 20020f6..9a55dae 100644
--- a/model/node.go
+++ b/model/node.go
@@ -4,6 +4,7 @@ package model
type Node interface {
Type() NodeType
MarshalJSON() ([]byte, error)
+ Set(v any) error
}
// NewNode creates new node from value
diff --git a/model/nullNode.go b/model/nullNode.go
index 448b18b..d510828 100644
--- a/model/nullNode.go
+++ b/model/nullNode.go
@@ -9,3 +9,7 @@ func (n NullNode) Type() NodeType {
func (n NullNode) MarshalJSON() ([]byte, error) {
return []byte("null"), nil
}
+
+func (n NullNode) Set(v any) error {
+ return nil
+}
diff --git a/model/numberNode.go b/model/numberNode.go
index e1bffd9..b61b63c 100644
--- a/model/numberNode.go
+++ b/model/numberNode.go
@@ -1,6 +1,9 @@
package model
-import "strconv"
+import (
+ "fmt"
+ "strconv"
+)
type NumberNode struct {
Value float64
@@ -13,3 +16,13 @@ func (n NumberNode) Type() NodeType {
func (n *NumberNode) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatFloat(n.Value, 'g', -1, 64)), nil
}
+
+func (n *NumberNode) Set(v any) error {
+ switch v := v.(type) {
+ case float64:
+ n.Value = v
+ case int:
+ n.Value = float64(v)
+ }
+ return fmt.Errorf("%v is not number", v)
+}
diff --git a/model/objectNode.go b/model/objectNode.go
index 53f500d..cfa2a30 100644
--- a/model/objectNode.go
+++ b/model/objectNode.go
@@ -6,7 +6,7 @@ import (
)
type ObjectNode struct {
- Value map[string]Node
+ Value NodeObjectValue
}
func (n ObjectNode) Type() NodeType {
@@ -30,10 +30,6 @@ func (n *ObjectNode) MarshalJSON() ([]byte, error) {
}, []byte("")), nil
}
-func (n *ObjectNode) Set(k string, v any) {
- n.Value[k] = NewNode(v)
-}
-
func (n *ObjectNode) Get(k string) (Node, error) {
child, ok := n.Value[k]
if !ok {
@@ -51,3 +47,12 @@ func (n *ObjectNode) Merge(n2 *ObjectNode) {
func (n *ObjectNode) Len() int {
return len(n.Value)
}
+
+func (n *ObjectNode) Set(v any) error {
+ val, ok := v.(NodeObjectValue)
+ if !ok {
+ return fmt.Errorf("%v is not object", v)
+ }
+ n.Value = val
+ return nil
+}
diff --git a/model/stringNode.go b/model/stringNode.go
index afa4023..b8dfcfb 100644
--- a/model/stringNode.go
+++ b/model/stringNode.go
@@ -1,5 +1,7 @@
package model
+import "fmt"
+
type StringNode struct {
Value string
}
@@ -11,3 +13,12 @@ func (n StringNode) Type() NodeType {
func (n *StringNode) MarshalJSON() ([]byte, error) {
return []byte(`"` + n.Value + `"`), nil
}
+
+func (n *StringNode) Set(v any) error {
+ val, ok := v.(string)
+ if !ok {
+ return fmt.Errorf("%v is not string", v)
+ }
+ n.Value = val
+ return nil
+}