aboutsummaryrefslogtreecommitdiff
path: root/model/array.go
diff options
context:
space:
mode:
authorNeonXP <i@neonxp.dev>2022-11-16 05:11:19 +0300
committerNeonXP <i@neonxp.dev>2022-11-16 05:11:19 +0300
commita321bfe7b2f6db5078de7b2e5ed5ddcccd65f319 (patch)
treed11c187bceee610a7843463949df128569142680 /model/array.go
initial commit
Diffstat (limited to 'model/array.go')
-rw-r--r--model/array.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/model/array.go b/model/array.go
new file mode 100644
index 0000000..b3d2586
--- /dev/null
+++ b/model/array.go
@@ -0,0 +1,32 @@
+package model
+
+import "fmt"
+
+// Index returns node by index from array
+func (n *Node) Index(idx int) (*Node, error) {
+ 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
+}
+
+// SetIndex sets node to array by index
+func (n *Node) SetIndex(idx int, value *Node) error {
+ arrlen := len(n.arrayValue)
+ if idx >= arrlen {
+ return fmt.Errorf("index %d out of range (len=%d)", idx, arrlen)
+ }
+ 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 {
+ if err := cb(i, v); err != nil {
+ return err
+ }
+ }
+ return nil
+}