2015-10-01 23:05:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-03 05:10:29 +00:00
|
|
|
// "fmt"
|
2015-10-01 23:05:13 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func write(context map[interface{}]interface{}, head string, tail []string, value interface{}) {
|
2015-10-05 04:59:59 +00:00
|
|
|
if len(tail) == 0 {
|
|
|
|
context[head] = value
|
|
|
|
} else {
|
2017-02-09 02:57:05 +00:00
|
|
|
// e.g. if updating a.b.c, we need to get the 'b', this could be a map or an array
|
|
|
|
var parent = readMap(context, head, tail[0:len(tail)-1])
|
|
|
|
switch parent.(type) {
|
|
|
|
case map[interface{}]interface{}:
|
|
|
|
toUpdate := parent.(map[interface{}]interface{})
|
|
|
|
// b is a map, update the key 'c' to the supplied value
|
|
|
|
key := (tail[len(tail)-1])
|
|
|
|
toUpdate[key] = value
|
|
|
|
case []interface{}:
|
|
|
|
toUpdate := parent.([]interface{})
|
|
|
|
// b is an array, update it at index 'c' to the supplied value
|
|
|
|
rawIndex := (tail[len(tail)-1])
|
|
|
|
index, err := strconv.ParseInt(rawIndex, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
die("Error accessing array: %v", err)
|
|
|
|
}
|
|
|
|
toUpdate[index] = value
|
|
|
|
}
|
|
|
|
|
2015-10-05 04:59:59 +00:00
|
|
|
}
|
2015-10-01 23:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} {
|
2015-10-05 23:01:33 +00:00
|
|
|
if head == "*" {
|
|
|
|
return readMapSplat(context, tail)
|
|
|
|
}
|
2015-10-01 23:05:13 +00:00
|
|
|
value := context[head]
|
2015-10-05 22:25:55 +00:00
|
|
|
return calculateValue(value, tail)
|
2015-10-01 23:05:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 23:01:33 +00:00
|
|
|
func readMapSplat(context map[interface{}]interface{}, tail []string) interface{} {
|
|
|
|
var newArray = make([]interface{}, len(context))
|
|
|
|
var i = 0
|
|
|
|
for _, value := range context {
|
|
|
|
if len(tail) > 0 {
|
|
|
|
newArray[i] = recurse(value, tail[0], tail[1:len(tail)])
|
|
|
|
} else {
|
|
|
|
newArray[i] = value
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return newArray
|
|
|
|
}
|
|
|
|
|
2015-10-01 23:05:13 +00:00
|
|
|
func recurse(value interface{}, head string, tail []string) interface{} {
|
|
|
|
switch value.(type) {
|
|
|
|
case []interface{}:
|
2015-10-05 03:41:01 +00:00
|
|
|
if head == "*" {
|
|
|
|
return readArraySplat(value.([]interface{}), tail)
|
|
|
|
}
|
2015-10-01 23:05:13 +00:00
|
|
|
index, err := strconv.ParseInt(head, 10, 64)
|
|
|
|
if err != nil {
|
2015-10-06 05:39:19 +00:00
|
|
|
die("Error accessing array: %v", err)
|
2015-10-01 23:05:13 +00:00
|
|
|
}
|
|
|
|
return readArray(value.([]interface{}), index, tail)
|
2015-10-05 03:47:24 +00:00
|
|
|
case map[interface{}]interface{}:
|
2015-10-01 23:05:13 +00:00
|
|
|
return readMap(value.(map[interface{}]interface{}), head, tail)
|
2015-10-05 03:47:24 +00:00
|
|
|
default:
|
|
|
|
return nil
|
2015-10-01 23:05:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readArray(array []interface{}, head int64, tail []string) interface{} {
|
2016-03-16 05:11:06 +00:00
|
|
|
if head >= int64(len(array)) {
|
2015-10-03 06:50:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-01 23:05:13 +00:00
|
|
|
value := array[head]
|
2015-10-05 22:25:55 +00:00
|
|
|
|
|
|
|
return calculateValue(value, tail)
|
2015-10-01 23:05:13 +00:00
|
|
|
}
|
2015-10-05 03:41:01 +00:00
|
|
|
|
|
|
|
func readArraySplat(array []interface{}, tail []string) interface{} {
|
|
|
|
var newArray = make([]interface{}, len(array))
|
|
|
|
for index, value := range array {
|
|
|
|
newArray[index] = calculateValue(value, tail)
|
|
|
|
}
|
|
|
|
return newArray
|
|
|
|
}
|
|
|
|
|
|
|
|
func calculateValue(value interface{}, tail []string) interface{} {
|
|
|
|
if len(tail) > 0 {
|
|
|
|
return recurse(value, tail[0], tail[1:len(tail)])
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|