yq/data_navigator.go

167 lines
3.9 KiB
Go
Raw Normal View History

2015-10-01 23:05:13 +00:00
package main
import (
2017-02-26 22:01:52 +00:00
"github.com/mikefarah/yaml/Godeps/_workspace/src/gopkg.in/yaml.v2"
2015-10-01 23:05:13 +00:00
"strconv"
)
2017-02-26 22:01:52 +00:00
func entryInSlice(context yaml.MapSlice, key interface{}) *yaml.MapItem {
for idx := range context {
var entry = &context[idx]
if entry.Key == key {
return entry
}
}
return nil
}
2017-04-11 23:16:54 +00:00
func writeMap(context interface{}, paths []string, value interface{}) yaml.MapSlice {
log.Debugf("writeMap for %v for %v with value %v\n", paths, context, value)
var mapSlice yaml.MapSlice
switch context.(type) {
case yaml.MapSlice:
mapSlice = context.(yaml.MapSlice)
default:
mapSlice = make(yaml.MapSlice, 0)
}
if len(paths) == 0 {
return mapSlice
}
child := entryInSlice(mapSlice, paths[0])
if child == nil {
newChild := yaml.MapItem{Key: paths[0]}
mapSlice = append(mapSlice, newChild)
child = entryInSlice(mapSlice, paths[0])
log.Debugf("\tAppended child at %v for mapSlice %v\n", paths[0], mapSlice)
}
log.Debugf("\tchild.Value %v\n", child.Value)
remainingPaths := paths[1:len(paths)]
child.Value = updatedChildValue(child.Value, remainingPaths, value)
log.Debugf("\tReturning mapSlice %v\n", mapSlice)
return mapSlice
}
func updatedChildValue(child interface{}, remainingPaths []string, value interface{}) interface{} {
if len(remainingPaths) == 0 {
return value
}
_, nextIndexErr := strconv.ParseInt(remainingPaths[0], 10, 64)
if nextIndexErr != nil {
// must be a map
return writeMap(child, remainingPaths, value)
}
// must be an array
return writeArray(child, remainingPaths, value)
}
2017-04-11 23:16:54 +00:00
func writeArray(context interface{}, paths []string, value interface{}) []interface{} {
log.Debugf("writeArray for %v for %v with value %v\n", paths, context, value)
var array []interface{}
switch context.(type) {
case []interface{}:
array = context.([]interface{})
default:
array = make([]interface{}, 1)
}
2017-04-11 23:16:54 +00:00
if len(paths) == 0 {
return array
}
log.Debugf("\tarray %v\n", array)
rawIndex := paths[0]
index, err := strconv.ParseInt(rawIndex, 10, 64)
if err != nil {
die("Error accessing array: %v", err)
}
2017-04-17 22:53:27 +00:00
for index >= int64(len(array)) {
array = append(array, nil)
}
2017-04-11 23:16:54 +00:00
currentChild := array[index]
log.Debugf("\tcurrentChild %v\n", currentChild)
remainingPaths := paths[1:len(paths)]
array[index] = updatedChildValue(currentChild, remainingPaths, value)
log.Debugf("\tReturning array %v\n", array)
return array
2015-10-01 23:05:13 +00:00
}
2017-02-26 22:01:52 +00:00
func readMap(context yaml.MapSlice, head string, tail []string) interface{} {
2015-10-05 23:01:33 +00:00
if head == "*" {
return readMapSplat(context, tail)
}
2017-02-26 22:01:52 +00:00
var value interface{}
2017-04-11 23:16:54 +00:00
entry := entryInSlice(context, head)
2017-02-26 22:01:52 +00:00
if entry != nil {
value = entry.Value
}
2015-10-05 22:25:55 +00:00
return calculateValue(value, tail)
2015-10-01 23:05:13 +00:00
}
2017-02-26 22:01:52 +00:00
func readMapSplat(context yaml.MapSlice, tail []string) interface{} {
2015-10-05 23:01:33 +00:00
var newArray = make([]interface{}, len(context))
var i = 0
2017-02-26 22:01:52 +00:00
for _, entry := range context {
2015-10-05 23:01:33 +00:00
if len(tail) > 0 {
2017-02-26 22:01:52 +00:00
newArray[i] = recurse(entry.Value, tail[0], tail[1:len(tail)])
2015-10-05 23:01:33 +00:00
} else {
2017-02-26 22:01:52 +00:00
newArray[i] = entry.Value
2015-10-05 23:01:33 +00:00
}
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)
2017-02-26 22:01:52 +00:00
case yaml.MapSlice:
return readMap(value.(yaml.MapSlice), head, tail)
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)) {
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
}