aboutsummaryrefslogtreecommitdiff
path: root/model
diff options
context:
space:
mode:
Diffstat (limited to 'model')
-rw-r--r--model/arrayNode.go57
-rw-r--r--model/booleanNode.go27
-rw-r--r--model/node.go44
-rw-r--r--model/node_test.go108
-rw-r--r--model/nullNode.go15
-rw-r--r--model/numberNode.go28
-rw-r--r--model/objectNode.go63
-rw-r--r--model/query.go33
-rw-r--r--model/stringNode.go24
-rw-r--r--model/types.go19
10 files changed, 0 insertions, 418 deletions
diff --git a/model/arrayNode.go b/model/arrayNode.go
deleted file mode 100644
index 319f223..0000000
--- a/model/arrayNode.go
+++ /dev/null
@@ -1,57 +0,0 @@
-package model
-
-import (
- "bytes"
- "fmt"
-)
-
-type ArrayNode struct {
- Value NodeArrayValue
-}
-
-func (n ArrayNode) Type() NodeType {
- return ArrayType
-}
-
-func (n *ArrayNode) MarshalJSON() ([]byte, error) {
- result := make([][]byte, 0, len(n.Value))
- for _, v := range n.Value {
- b, err := v.MarshalJSON()
- if err != nil {
- return nil, err
- }
- result = append(result, b)
- }
- return bytes.Join(
- [][]byte{
- []byte("["),
- bytes.Join(result, []byte(", ")),
- []byte("]"),
- }, []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)
- }
- return n.Value[idx], nil
-}
-
-func (n *ArrayNode) Merge(n2 *ArrayNode) {
- n.Value = append(n.Value, n2.Value...)
-}
-
-func (n *ArrayNode) Len() int {
- return len(n.Value)
-}
-
-type NodeArrayValue []Node
diff --git a/model/booleanNode.go b/model/booleanNode.go
deleted file mode 100644
index aba286e..0000000
--- a/model/booleanNode.go
+++ /dev/null
@@ -1,27 +0,0 @@
-package model
-
-import "fmt"
-
-type BooleanNode struct {
- Value bool
-}
-
-func (n BooleanNode) Type() NodeType {
- return BooleanType
-}
-
-func (n *BooleanNode) MarshalJSON() ([]byte, error) {
- if n.Value {
- return []byte("true"), nil
- }
- 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
deleted file mode 100644
index fb6bae5..0000000
--- a/model/node.go
+++ /dev/null
@@ -1,44 +0,0 @@
-package model
-
-// Node of JSON tree
-type Node interface {
- Type() NodeType
- MarshalJSON() ([]byte, error)
- Set(v any) error
-}
-
-// NewNode creates new node from value
-func NewNode(value any) Node {
- if value, ok := value.(Node); ok {
- return value
- }
- switch value := value.(type) {
- case string:
- return &StringNode{
- Value: value,
- }
- case float64:
- return &NumberNode{
- Value: value,
- }
- case int:
- return &NumberNode{
- Value: float64(value),
- }
- case NodeObjectValue:
- return &ObjectNode{
- Value: value,
- Meta: make(map[string]any),
- }
- case NodeArrayValue:
- return &ArrayNode{
- Value: value,
- }
- case bool:
- return &BooleanNode{
- Value: value,
- }
- default:
- return NullNode{}
- }
-}
diff --git a/model/node_test.go b/model/node_test.go
deleted file mode 100644
index 1f46361..0000000
--- a/model/node_test.go
+++ /dev/null
@@ -1,108 +0,0 @@
-package model
-
-import (
- stdJSON "encoding/json"
- "reflect"
- "testing"
-)
-
-func TestNode_MarshalJSON(t *testing.T) {
- type fields struct {
- node Node
- }
- tests := []struct {
- name string
- fields fields
- want []byte
- wantErr bool
- }{
- {
- name: "empty",
- fields: fields{
- node: NewNode(nil),
- },
- want: []byte(`null`),
- },
- {
- name: "string",
- fields: fields{
- node: NewNode("this is a string"),
- },
- want: []byte(`"this is a string"`),
- },
- {
- name: "int",
- fields: fields{
- node: NewNode(123),
- },
- want: []byte(`123`),
- },
- {
- name: "float",
- fields: fields{
- node: NewNode(123.321),
- },
- want: []byte(`123.321`),
- },
- {
- name: "booleant",
- fields: fields{
- node: NewNode(true),
- },
- want: []byte(`true`),
- },
- {
- name: "booleanf",
- fields: fields{
- node: NewNode(false),
- },
- want: []byte(`false`),
- },
- {
- name: "complex",
- fields: fields{
- node: NewNode(
- NodeObjectValue{
- "string key": NewNode("string value"),
- "number key": NewNode(1337),
- "float key": NewNode(123.3),
- "object key": NewNode(NodeObjectValue{
- "ab": NewNode("cd"),
- }),
- "array key": NewNode(NodeArrayValue{
- NewNode(1), NewNode(2), NewNode("three"),
- }),
- "boolean key": NewNode(true),
- "null key": NewNode(nil),
- },
- ),
- },
- want: []byte(
- `{"string key": "string value", "number key": 1337, "float key": 123.3, "object key": {"ab": "cd"}, "array key": [1, 2, "three"], "boolean key": true, "null key": null}`,
- ),
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- var (
- gotObj any
- wantObj any
- )
-
- got, err := tt.fields.node.MarshalJSON()
- if (err != nil) != tt.wantErr {
- t.Errorf("Node.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- err = stdJSON.Unmarshal(got, &gotObj) // TODO use own unmarshaller
- if err != nil {
- t.Errorf("Generated invalid json = %s, error = %v", got, err)
- }
- _ = stdJSON.Unmarshal(tt.want, &wantObj) // I belive, test is correct
- if !reflect.DeepEqual(gotObj, wantObj) {
- t.Errorf("Node.MarshalJSON() = %s, want %s", got, tt.want)
- }
- })
- }
-}
diff --git a/model/nullNode.go b/model/nullNode.go
deleted file mode 100644
index d510828..0000000
--- a/model/nullNode.go
+++ /dev/null
@@ -1,15 +0,0 @@
-package model
-
-type NullNode struct{}
-
-func (n NullNode) Type() NodeType {
- return NullType
-}
-
-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
deleted file mode 100644
index b61b63c..0000000
--- a/model/numberNode.go
+++ /dev/null
@@ -1,28 +0,0 @@
-package model
-
-import (
- "fmt"
- "strconv"
-)
-
-type NumberNode struct {
- Value float64
-}
-
-func (n NumberNode) Type() NodeType {
- return NumberType
-}
-
-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
deleted file mode 100644
index 989fd9e..0000000
--- a/model/objectNode.go
+++ /dev/null
@@ -1,63 +0,0 @@
-package model
-
-import (
- "bytes"
- "fmt"
-)
-
-type ObjectNode struct {
- Value NodeObjectValue
- Meta map[string]any
-}
-
-func (n ObjectNode) Type() NodeType {
- return ObjectType
-}
-
-func (n *ObjectNode) MarshalJSON() ([]byte, error) {
- result := make([][]byte, 0, len(n.Value))
- for k, v := range n.Value {
- b, err := v.MarshalJSON()
- if err != nil {
- return nil, err
- }
- result = append(result, []byte(fmt.Sprintf("\"%s\": %s", k, b)))
- }
- return bytes.Join(
- [][]byte{
- []byte("{"),
- bytes.Join(result, []byte(", ")),
- []byte("}"),
- }, []byte("")), nil
-}
-
-func (n *ObjectNode) Get(k string) (Node, error) {
- child, ok := n.Value[k]
- if !ok {
- return nil, fmt.Errorf("field %s not found", k)
- }
- return child, nil
-}
-
-func (n *ObjectNode) Merge(n2 *ObjectNode) {
- for k, v := range n2.Value {
- n.Value[k] = v
- }
-}
-
-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
-}
-
-func (n *ObjectNode) Remove(key string) {
- delete(n.Value, key)
-}
diff --git a/model/query.go b/model/query.go
deleted file mode 100644
index 56a0dfe..0000000
--- a/model/query.go
+++ /dev/null
@@ -1,33 +0,0 @@
-package model
-
-import (
- "fmt"
- "strconv"
-)
-
-// Query returns node by array query
-func Query(n Node, query []string) (Node, error) {
- if len(query) == 0 {
- return n, nil
- }
- head, rest := query[0], query[1:]
- switch n := n.(type) {
- case *ArrayNode:
- idx, err := strconv.Atoi(head)
- if err != nil {
- return nil, fmt.Errorf("index must be a number, got %s", head)
- }
- next, err := n.Index(idx)
- if err != nil {
- return nil, err
- }
- return Query(next, rest)
- case *ObjectNode:
- next, err := n.Get(head)
- if err != nil {
- return nil, err
- }
- return Query(next, rest)
- }
- return nil, fmt.Errorf("can't get %s from node type %s", head, n.Type())
-}
diff --git a/model/stringNode.go b/model/stringNode.go
deleted file mode 100644
index b8dfcfb..0000000
--- a/model/stringNode.go
+++ /dev/null
@@ -1,24 +0,0 @@
-package model
-
-import "fmt"
-
-type StringNode struct {
- Value string
-}
-
-func (n StringNode) Type() NodeType {
- return StringType
-}
-
-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
-}
diff --git a/model/types.go b/model/types.go
deleted file mode 100644
index 72dce5f..0000000
--- a/model/types.go
+++ /dev/null
@@ -1,19 +0,0 @@
-package model
-
-type NodeType string
-
-const (
- StringType NodeType = "string"
- NumberType NodeType = "number"
- ObjectType NodeType = "object"
- ArrayType NodeType = "array"
- BooleanType NodeType = "boolean"
- NullType NodeType = "null"
-)
-
-type NodeObjectValue map[string]Node
-
-func (n NodeObjectValue) Set(k string, v any) error {
- n[k] = NewNode(v)
- return nil
-}