diff options
Diffstat (limited to 'model/arrayNode.go')
-rw-r--r-- | model/arrayNode.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/model/arrayNode.go b/model/arrayNode.go new file mode 100644 index 0000000..203e01c --- /dev/null +++ b/model/arrayNode.go @@ -0,0 +1,48 @@ +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) 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 |