-
Notifications
You must be signed in to change notification settings - Fork 2
/
array.go
37 lines (30 loc) · 903 Bytes
/
array.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package dot
import (
"fmt"
"reflect"
"strconv"
)
// inArray inserts the data into the array at the specified path
func (d *Dot) inArray(innerObj reflect.Value, currentPath string, parts []string) error {
// Getting the array index
index, err := strconv.Atoi(parts[0])
if err != nil {
return fmt.Errorf(`invalid value "%s" as an array index`, parts[0])
}
// Getting the array index
if innerObj.Len() <= index || index < 0 {
return fmt.Errorf(
"index %d out of range in path %s of type %s",
index, currentPath, innerObj.Type(),
)
}
// Create a variable that matches the type of value
value := reflect.New(innerObj.Type().Elem()).Elem()
// Recursively insert the value in the path indicated
if err = d.insert(value, currentPath, parts[1:], Array); err != nil {
return err
}
// Insert a value at the specified array index
innerObj.Index(index).Set(value)
return nil
}