aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNeonXP <i@neonxp.dev>2022-11-21 03:45:23 +0300
committerNeonXP <i@neonxp.dev>2022-11-21 03:45:23 +0300
commit4934a51c69e1975dc808e094e113b187ebb2c328 (patch)
treec9944bf5e5a499fb12e0d0f94cc63b12eb747493
parent59e421989e5a1b8da5200323c4478645c339d66b (diff)
Some fixes
-rw-r--r--model/array.go14
-rw-r--r--model/condition.go6
-rw-r--r--model/map.go16
-rw-r--r--model/node.go63
-rw-r--r--parser/lexer.go1
5 files changed, 53 insertions, 47 deletions
diff --git a/model/array.go b/model/array.go
index 5d64209..528f1ef 100644
--- a/model/array.go
+++ b/model/array.go
@@ -4,26 +4,26 @@ import "fmt"
// Index returns node by index from array
func (n *Node) Index(idx int) (*Node, error) {
- arrlen := len(n.arrayValue)
+ arrlen := len(n.ArrayValue)
if idx >= arrlen {
return nil, fmt.Errorf("index %d out of range (len=%d)", idx, arrlen)
}
- return n.arrayValue[idx], nil
+ return n.ArrayValue[idx], nil
}
// SetIndex sets node to array by index
func (n *Node) SetIndex(idx int, value *Node) error {
- arrlen := len(n.arrayValue)
+ arrlen := len(n.ArrayValue)
if idx >= arrlen {
return fmt.Errorf("index %d out of range (len=%d)", idx, arrlen)
}
- n.arrayValue[idx] = value
+ n.ArrayValue[idx] = value
return nil
}
// Each applies callback to each element of array
func (n *Node) Each(cb func(idx int, value *Node) error) error {
- for i, v := range n.arrayValue {
+ for i, v := range n.ArrayValue {
if err := cb(i, v); err != nil {
return err
}
@@ -33,10 +33,10 @@ func (n *Node) Each(cb func(idx int, value *Node) error) error {
// RemoveIndex from array
func (n *Node) RemoveIndex(idx int) error {
- arrlen := len(n.arrayValue)
+ arrlen := len(n.ArrayValue)
if idx >= arrlen {
return fmt.Errorf("index %d out of range (len=%d)", idx, arrlen)
}
- n.arrayValue = append(n.arrayValue[:idx], n.arrayValue[idx:]...)
+ n.ArrayValue = append(n.ArrayValue[:idx], n.ArrayValue[idx:]...)
return nil
}
diff --git a/model/condition.go b/model/condition.go
index 0b558b4..82c964a 100644
--- a/model/condition.go
+++ b/model/condition.go
@@ -19,7 +19,7 @@ func (n *Node) Compare(op Operand, node *Node) bool {
if n.Type != ArrayNode {
return false
}
- for _, v := range n.arrayValue {
+ for _, v := range n.ArrayValue {
if v.Value() == node.Value() {
return true
}
@@ -34,9 +34,9 @@ func less(n1 *Node, n2 *Node) bool {
}
switch n1.Type {
case NumberNode:
- return n1.numberValue < n2.numberValue
+ return n1.NumberValue < n2.NumberValue
case StringNode:
- return n1.stringValue < n2.stringValue
+ return n1.StringValue < n2.StringValue
default:
return false
}
diff --git a/model/map.go b/model/map.go
index 7bfdaa3..676288c 100644
--- a/model/map.go
+++ b/model/map.go
@@ -10,10 +10,10 @@ func (n *Node) Get(key string) (*Node, error) {
if n.Type != ObjectNode {
return nil, fmt.Errorf("node must be object, got %s", n.Type)
}
- node, ok := n.objectValue[key]
+ node, ok := n.ObjectValue[key]
if !ok {
- keys := make([]string, 0, len(n.objectValue))
- for k := range n.objectValue {
+ keys := make([]string, 0, len(n.ObjectValue))
+ for k := range n.ObjectValue {
keys = append(keys, k)
}
return nil, fmt.Errorf("field '%s' does not exist in object (keys %s)", key, strings.Join(keys, ", "))
@@ -22,22 +22,22 @@ func (n *Node) Get(key string) (*Node, error) {
}
// Set node to object by key
-func (n *Node) Set(key string, value *Node) error {
+func (n *Node) Set(key string, value Node) error {
if n.Type != ObjectNode {
return fmt.Errorf("node must be object, got %s", n.Type)
}
- n.objectValue[key] = value
+ n.ObjectValue[key] = &value
return nil
}
// Map callback to each key value pair of object
func (n *Node) Map(cb func(key string, value *Node) (*Node, error)) error {
- for k, v := range n.objectValue {
+ for k, v := range n.ObjectValue {
newNode, err := cb(k, v)
if err != nil {
return err
}
- n.objectValue[k] = newNode
+ n.ObjectValue[k] = newNode
}
return nil
}
@@ -47,6 +47,6 @@ func (n *Node) Remove(key string) error {
if n.Type != ObjectNode {
return fmt.Errorf("node must be object, got %s", n.Type)
}
- delete(n.objectValue, key)
+ delete(n.ObjectValue, key)
return nil
}
diff --git a/model/node.go b/model/node.go
index a4a509f..0aabfcc 100644
--- a/model/node.go
+++ b/model/node.go
@@ -9,12 +9,12 @@ import (
// Node of JSON tree
type Node struct {
Type NodeType
- Meta Meta
- stringValue string
- numberValue float64
- objectValue NodeObjectValue
- arrayValue NodeArrayValue
- booleanValue bool
+ Meta NodeObjectValue
+ StringValue string
+ NumberValue float64
+ ObjectValue NodeObjectValue
+ ArrayValue NodeArrayValue
+ BooleanValue bool
}
// NewNode creates new node from value
@@ -28,15 +28,15 @@ func NewNode(value any) *Node {
func (n *Node) Value() any {
switch n.Type {
case StringNode:
- return n.stringValue
+ return n.StringValue
case NumberNode:
- return n.numberValue
+ return n.NumberValue
case ObjectNode:
- return n.objectValue
+ return n.ObjectValue
case ArrayNode:
- return n.arrayValue
+ return n.ArrayValue
case BooleanNode:
- return n.booleanValue
+ return n.BooleanValue
default:
return nil
}
@@ -47,22 +47,27 @@ func (n *Node) SetValue(value any) {
switch value := value.(type) {
case string:
n.Type = StringNode
- n.stringValue = value
+ n.StringValue = value
case float64:
n.Type = NumberNode
- n.numberValue = value
+ n.NumberValue = value
case int:
n.Type = NumberNode
- n.numberValue = float64(value)
+ n.NumberValue = float64(value)
case NodeObjectValue:
n.Type = ObjectNode
- n.objectValue = value
+ meta, hasMeta := value["@"]
+ if hasMeta {
+ n.Meta = meta.ObjectValue
+ delete(value, "@")
+ }
+ n.ObjectValue = value
case NodeArrayValue:
n.Type = ArrayNode
- n.arrayValue = value
+ n.ArrayValue = value
case bool:
n.Type = BooleanNode
- n.booleanValue = value
+ n.BooleanValue = value
default:
n.Type = NullNode
}
@@ -72,12 +77,12 @@ func (n *Node) SetValue(value any) {
func (n *Node) MarshalJSON() ([]byte, error) {
switch n.Type {
case StringNode:
- return []byte(`"` + n.stringValue + `"`), nil
+ return []byte(`"` + n.StringValue + `"`), nil
case NumberNode:
- return []byte(strconv.FormatFloat(n.numberValue, 'g', -1, 64)), nil
+ return []byte(strconv.FormatFloat(n.NumberValue, 'g', -1, 64)), nil
case ObjectNode:
- result := make([][]byte, 0, len(n.objectValue))
- for k, v := range n.objectValue {
+ result := make([][]byte, 0, len(n.ObjectValue))
+ for k, v := range n.ObjectValue {
b, err := v.MarshalJSON()
if err != nil {
return nil, err
@@ -91,8 +96,8 @@ func (n *Node) MarshalJSON() ([]byte, error) {
[]byte("}"),
}, []byte("")), nil
case ArrayNode:
- result := make([][]byte, 0, len(n.arrayValue))
- for _, v := range n.arrayValue {
+ result := make([][]byte, 0, len(n.ArrayValue))
+ for _, v := range n.ArrayValue {
b, err := v.MarshalJSON()
if err != nil {
return nil, err
@@ -106,7 +111,7 @@ func (n *Node) MarshalJSON() ([]byte, error) {
[]byte("]"),
}, []byte("")), nil
case BooleanNode:
- if n.booleanValue {
+ if n.BooleanValue {
return []byte("true"), nil
}
return []byte("false"), nil
@@ -122,11 +127,11 @@ func (n *Node) Merge(node *Node) error {
}
switch n.Type {
case ObjectNode:
- for k, v := range node.objectValue {
- n.objectValue[k] = v
+ for k, v := range node.ObjectValue {
+ n.ObjectValue[k] = v
}
case ArrayNode:
- n.arrayValue = append(n.arrayValue, node.arrayValue...)
+ n.ArrayValue = append(n.ArrayValue, node.ArrayValue...)
default:
return fmt.Errorf("merge not implemented for type %s", n.Type)
}
@@ -137,9 +142,9 @@ func (n *Node) Merge(node *Node) error {
func (n *Node) Len() (int, error) {
switch n.Type {
case ObjectNode:
- return len(n.objectValue), nil
+ return len(n.ObjectValue), nil
case ArrayNode:
- return len(n.arrayValue), nil
+ return len(n.ArrayValue), nil
default:
return 0, fmt.Errorf("merge not implemented for type %s", n.Type)
}
diff --git a/parser/lexer.go b/parser/lexer.go
index a772698..5034f6a 100644
--- a/parser/lexer.go
+++ b/parser/lexer.go
@@ -15,6 +15,7 @@ type lexem struct {
End int // End position at input string.
}
+//go:generate stringer -type=lexType
type lexType int
const (