2019-11-23 03:52:29 +00:00
|
|
|
package yqlib
|
2015-10-01 23:05:13 +00:00
|
|
|
|
|
|
|
import (
|
2019-12-08 04:37:30 +00:00
|
|
|
"bytes"
|
2015-10-01 23:05:13 +00:00
|
|
|
"strconv"
|
2017-09-20 23:40:33 +00:00
|
|
|
|
2019-12-01 19:44:44 +00:00
|
|
|
logging "gopkg.in/op/go-logging.v1"
|
2019-12-06 04:57:46 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2015-10-01 23:05:13 +00:00
|
|
|
)
|
|
|
|
|
2019-12-01 19:44:44 +00:00
|
|
|
type DataNavigator interface {
|
2019-12-09 02:44:53 +00:00
|
|
|
DebugNode(node *yaml.Node)
|
2019-12-12 09:47:22 +00:00
|
|
|
Get(rootNode *yaml.Node, path []string) (*yaml.Node, error)
|
|
|
|
Update(rootNode *yaml.Node, path []string, changesToApply yaml.Node) error
|
|
|
|
Delete(rootNode *yaml.Node, path []string) error
|
2019-12-01 19:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type navigator struct {
|
|
|
|
log *logging.Logger
|
|
|
|
}
|
|
|
|
|
2019-12-08 04:59:24 +00:00
|
|
|
type VisitorFn func(*yaml.Node) (*yaml.Node, error)
|
|
|
|
|
|
|
|
func identityVisitor(value *yaml.Node) (*yaml.Node, error) {
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2019-12-01 19:44:44 +00:00
|
|
|
func NewDataNavigator(l *logging.Logger) DataNavigator {
|
2019-12-01 20:10:42 +00:00
|
|
|
return &navigator{
|
2019-12-01 19:44:44 +00:00
|
|
|
log: l,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 04:57:46 +00:00
|
|
|
func (n *navigator) Get(value *yaml.Node, path []string) (*yaml.Node, error) {
|
2019-12-08 04:59:24 +00:00
|
|
|
return n.Visit(value, path, identityVisitor)
|
|
|
|
}
|
|
|
|
|
2019-12-12 09:47:22 +00:00
|
|
|
func (n *navigator) Update(rootNode *yaml.Node, path []string, changesToApply yaml.Node) error {
|
|
|
|
_, errorVisiting := n.Visit(rootNode, path, func(nodeToUpdate *yaml.Node) (*yaml.Node, error) {
|
2019-12-08 04:59:24 +00:00
|
|
|
n.log.Debug("going to update")
|
2019-12-09 02:44:53 +00:00
|
|
|
n.DebugNode(nodeToUpdate)
|
2019-12-08 04:59:24 +00:00
|
|
|
n.log.Debug("with")
|
2019-12-09 02:44:53 +00:00
|
|
|
n.DebugNode(&changesToApply)
|
2019-12-08 04:59:24 +00:00
|
|
|
nodeToUpdate.Value = changesToApply.Value
|
|
|
|
nodeToUpdate.Tag = changesToApply.Tag
|
|
|
|
nodeToUpdate.Kind = changesToApply.Kind
|
|
|
|
nodeToUpdate.Style = changesToApply.Style
|
|
|
|
nodeToUpdate.Content = changesToApply.Content
|
|
|
|
nodeToUpdate.HeadComment = changesToApply.HeadComment
|
|
|
|
nodeToUpdate.LineComment = changesToApply.LineComment
|
|
|
|
nodeToUpdate.FootComment = changesToApply.FootComment
|
|
|
|
return nodeToUpdate, nil
|
|
|
|
})
|
|
|
|
return errorVisiting
|
|
|
|
}
|
|
|
|
|
2019-12-12 09:47:22 +00:00
|
|
|
func (n *navigator) Delete(rootNode *yaml.Node, path []string) error {
|
|
|
|
|
|
|
|
lastBit, newTail := path[len(path)-1], path[:len(path)-1]
|
|
|
|
n.log.Debug("splitting path, %v", lastBit)
|
|
|
|
n.log.Debug("new tail, %v", newTail)
|
|
|
|
_, errorVisiting := n.Visit(rootNode, newTail, func(nodeToUpdate *yaml.Node) (*yaml.Node, error) {
|
|
|
|
n.log.Debug("need to find %v in here", lastBit)
|
|
|
|
n.DebugNode(nodeToUpdate)
|
2019-12-15 06:31:26 +00:00
|
|
|
original := nodeToUpdate.Content
|
2019-12-12 09:47:22 +00:00
|
|
|
if nodeToUpdate.Kind == yaml.SequenceNode {
|
|
|
|
var index, err = strconv.ParseInt(lastBit, 10, 64) // nolint
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if index >= int64(len(nodeToUpdate.Content)) {
|
|
|
|
n.log.Debug("index %v is greater than content lenth %v", index, len(nodeToUpdate.Content))
|
|
|
|
return nodeToUpdate, nil
|
|
|
|
}
|
|
|
|
nodeToUpdate.Content = append(original[:index], original[index+1:]...)
|
|
|
|
|
|
|
|
} else if nodeToUpdate.Kind == yaml.MappingNode {
|
2019-12-15 06:31:26 +00:00
|
|
|
//need to delete both the key and value children from Content
|
|
|
|
indexInMap := n.findIndexForKeyInMap(nodeToUpdate.Content, lastBit)
|
|
|
|
if indexInMap != -1 {
|
|
|
|
//skip two because its a key, value pair
|
|
|
|
nodeToUpdate.Content = append(original[:indexInMap], original[indexInMap+2:]...)
|
|
|
|
}
|
2019-12-12 09:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nodeToUpdate, nil
|
|
|
|
})
|
|
|
|
return errorVisiting
|
|
|
|
}
|
|
|
|
|
2019-12-08 04:59:24 +00:00
|
|
|
func (n *navigator) Visit(value *yaml.Node, path []string, visitor VisitorFn) (*yaml.Node, error) {
|
2019-12-06 04:57:46 +00:00
|
|
|
realValue := value
|
|
|
|
if realValue.Kind == yaml.DocumentNode {
|
2019-12-09 02:44:53 +00:00
|
|
|
n.log.Debugf("its a document! returning the first child")
|
2019-12-06 04:57:46 +00:00
|
|
|
realValue = value.Content[0]
|
2019-12-01 19:44:44 +00:00
|
|
|
}
|
2019-12-06 04:57:46 +00:00
|
|
|
if len(path) > 0 {
|
2019-12-06 05:36:42 +00:00
|
|
|
n.log.Debugf("diving into %v", path[0])
|
2019-12-09 02:44:53 +00:00
|
|
|
n.DebugNode(value)
|
2019-12-08 04:59:24 +00:00
|
|
|
return n.recurse(realValue, path[0], path[1:], visitor)
|
2019-05-12 23:32:08 +00:00
|
|
|
}
|
2019-12-08 04:59:24 +00:00
|
|
|
return visitor(realValue)
|
2019-05-12 23:32:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 02:44:53 +00:00
|
|
|
func (n *navigator) guessKind(tail []string, guess yaml.Kind) yaml.Kind {
|
2019-12-06 04:57:46 +00:00
|
|
|
n.log.Debug("tail %v", tail)
|
2019-12-09 02:44:53 +00:00
|
|
|
if len(tail) == 0 && guess == 0 {
|
2019-12-08 04:37:30 +00:00
|
|
|
n.log.Debug("end of path, must be a scalar")
|
2019-12-06 04:57:46 +00:00
|
|
|
return yaml.ScalarNode
|
2019-12-09 02:44:53 +00:00
|
|
|
} else if len(tail) == 0 {
|
|
|
|
return guess
|
2017-09-24 00:21:16 +00:00
|
|
|
}
|
2019-12-09 02:44:53 +00:00
|
|
|
|
2019-12-06 04:57:46 +00:00
|
|
|
var _, errorParsingInt = strconv.ParseInt(tail[0], 10, 64)
|
2019-12-09 02:44:53 +00:00
|
|
|
if tail[0] == "+" || errorParsingInt == nil {
|
2019-12-06 04:57:46 +00:00
|
|
|
return yaml.SequenceNode
|
2017-04-17 22:53:27 +00:00
|
|
|
}
|
2019-12-09 02:57:38 +00:00
|
|
|
if tail[0] == "*" && (guess == yaml.SequenceNode || guess == yaml.MappingNode) {
|
2019-12-09 02:44:53 +00:00
|
|
|
return guess
|
|
|
|
}
|
2019-12-06 04:57:46 +00:00
|
|
|
return yaml.MappingNode
|
2015-10-01 23:05:13 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 04:57:46 +00:00
|
|
|
func (n *navigator) getOrReplace(original *yaml.Node, expectedKind yaml.Kind) *yaml.Node {
|
2019-12-09 02:57:10 +00:00
|
|
|
if original.Kind != expectedKind {
|
2019-12-08 04:37:30 +00:00
|
|
|
n.log.Debug("wanted %v but it was %v, overriding", expectedKind, original.Kind)
|
2019-12-06 04:57:46 +00:00
|
|
|
return &yaml.Node{Kind: expectedKind}
|
2017-02-26 22:01:52 +00:00
|
|
|
}
|
2019-12-06 04:57:46 +00:00
|
|
|
return original
|
2015-10-01 23:05:13 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 02:44:53 +00:00
|
|
|
func (n *navigator) DebugNode(value *yaml.Node) {
|
2019-12-08 04:37:30 +00:00
|
|
|
if n.log.IsEnabledFor(logging.DEBUG) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
encoder := yaml.NewEncoder(buf)
|
|
|
|
encoder.Encode(value)
|
|
|
|
encoder.Close()
|
|
|
|
n.log.Debug("Tag: %v", value.Tag)
|
|
|
|
n.log.Debug("%v", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 04:59:24 +00:00
|
|
|
func (n *navigator) recurse(value *yaml.Node, head string, tail []string, visitor VisitorFn) (*yaml.Node, error) {
|
2019-12-06 04:57:46 +00:00
|
|
|
switch value.Kind {
|
|
|
|
case yaml.MappingNode:
|
|
|
|
n.log.Debug("its a map with %v entries", len(value.Content)/2)
|
2019-12-09 02:44:53 +00:00
|
|
|
if head == "*" {
|
2019-12-09 02:57:10 +00:00
|
|
|
return n.splatMap(value, tail, visitor)
|
2019-12-09 02:44:53 +00:00
|
|
|
}
|
2019-12-09 02:57:10 +00:00
|
|
|
return n.recurseMap(value, head, tail, visitor)
|
2019-12-06 04:57:46 +00:00
|
|
|
case yaml.SequenceNode:
|
2019-12-08 04:37:30 +00:00
|
|
|
n.log.Debug("its a sequence of %v things!, %v", len(value.Content))
|
2019-12-06 04:57:46 +00:00
|
|
|
if head == "*" {
|
2019-12-09 02:57:10 +00:00
|
|
|
return n.splatArray(value, tail, visitor)
|
2019-12-06 04:57:46 +00:00
|
|
|
} else if head == "+" {
|
2019-12-09 02:57:10 +00:00
|
|
|
return n.appendArray(value, tail, visitor)
|
|
|
|
}
|
|
|
|
return n.recurseArray(value, head, tail, visitor)
|
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
2018-05-04 15:46:58 +00:00
|
|
|
|
2019-12-09 02:57:10 +00:00
|
|
|
func (n *navigator) splatMap(value *yaml.Node, tail []string, visitor VisitorFn) (*yaml.Node, error) {
|
|
|
|
var newNode = yaml.Node{Kind: yaml.SequenceNode}
|
|
|
|
for index, content := range value.Content {
|
|
|
|
if index%2 == 0 {
|
|
|
|
continue
|
2019-05-14 01:20:41 +00:00
|
|
|
}
|
2019-12-09 02:57:10 +00:00
|
|
|
content = n.getOrReplace(content, n.guessKind(tail, content.Kind))
|
|
|
|
var nestedValue, err = n.Visit(content, tail, visitor)
|
2019-05-14 01:20:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-09 02:57:10 +00:00
|
|
|
newNode.Content = append(newNode.Content, nestedValue)
|
|
|
|
}
|
|
|
|
return &newNode, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *navigator) recurseMap(value *yaml.Node, head string, tail []string, visitor VisitorFn) (*yaml.Node, error) {
|
2019-12-15 06:31:26 +00:00
|
|
|
indexInMap := n.findIndexForKeyInMap(value.Content, head)
|
|
|
|
|
|
|
|
if indexInMap != -1 {
|
|
|
|
value.Content[indexInMap+1] = n.getOrReplace(value.Content[indexInMap+1], n.guessKind(tail, value.Content[indexInMap+1].Kind))
|
|
|
|
return n.Visit(value.Content[indexInMap+1], tail, visitor)
|
2019-12-09 02:57:10 +00:00
|
|
|
}
|
2019-12-15 06:31:26 +00:00
|
|
|
|
|
|
|
//didn't find it, lets add it.
|
2019-12-09 02:57:10 +00:00
|
|
|
value.Content = append(value.Content, &yaml.Node{Value: head, Kind: yaml.ScalarNode})
|
|
|
|
mapEntryValue := yaml.Node{Kind: n.guessKind(tail, 0)}
|
|
|
|
value.Content = append(value.Content, &mapEntryValue)
|
|
|
|
n.log.Debug("adding new node %v", value.Content)
|
|
|
|
return n.Visit(&mapEntryValue, tail, visitor)
|
|
|
|
}
|
|
|
|
|
2019-12-15 06:31:26 +00:00
|
|
|
func (n *navigator) findIndexForKeyInMap(contents []*yaml.Node, key string) int {
|
|
|
|
for index, content := range contents {
|
|
|
|
// value.Content is a concatenated array of key, value,
|
|
|
|
// so keys are in the even indexes, values in odd.
|
|
|
|
if index%2 == 1 || (content.Value != key) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2019-12-09 02:57:10 +00:00
|
|
|
func (n *navigator) splatArray(value *yaml.Node, tail []string, visitor VisitorFn) (*yaml.Node, error) {
|
|
|
|
var newNode = yaml.Node{Kind: yaml.SequenceNode, Style: value.Style}
|
|
|
|
newNode.Content = make([]*yaml.Node, len(value.Content))
|
|
|
|
|
|
|
|
for index, childValue := range value.Content {
|
|
|
|
n.log.Debug("processing")
|
|
|
|
n.DebugNode(childValue)
|
|
|
|
childValue = n.getOrReplace(childValue, n.guessKind(tail, childValue.Kind))
|
|
|
|
var nestedValue, err = n.Visit(childValue, tail, visitor)
|
|
|
|
n.log.Debug("nestedValue")
|
|
|
|
n.DebugNode(nestedValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newNode.Content[index] = nestedValue
|
|
|
|
}
|
|
|
|
return &newNode, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *navigator) appendArray(value *yaml.Node, tail []string, visitor VisitorFn) (*yaml.Node, error) {
|
|
|
|
var newNode = yaml.Node{Kind: n.guessKind(tail, 0)}
|
|
|
|
value.Content = append(value.Content, &newNode)
|
|
|
|
n.log.Debug("appending a new node, %v", value.Content)
|
|
|
|
return n.Visit(&newNode, tail, visitor)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *navigator) recurseArray(value *yaml.Node, head string, tail []string, visitor VisitorFn) (*yaml.Node, error) {
|
|
|
|
var index, err = strconv.ParseInt(head, 10, 64) // nolint
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if index >= int64(len(value.Content)) {
|
2019-12-06 04:57:46 +00:00
|
|
|
return nil, nil
|
2018-05-04 15:46:58 +00:00
|
|
|
}
|
2019-12-09 02:57:10 +00:00
|
|
|
value.Content[index] = n.getOrReplace(value.Content[index], n.guessKind(tail, value.Content[index].Kind))
|
|
|
|
return n.Visit(value.Content[index], tail, visitor)
|
2019-05-14 01:20:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 04:57:46 +00:00
|
|
|
// func matchesKey(key string, actual interface{}) bool {
|
|
|
|
// var actualString = fmt.Sprintf("%v", actual)
|
|
|
|
// var prefixMatch = strings.TrimSuffix(key, "*")
|
|
|
|
// if prefixMatch != key {
|
|
|
|
// return strings.HasPrefix(actualString, prefixMatch)
|
|
|
|
// }
|
|
|
|
// return actualString == key
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func entriesInSlice(context yaml.MapSlice, key string) []*yaml.MapItem {
|
|
|
|
// var matches = make([]*yaml.MapItem, 0)
|
|
|
|
// for idx := range context {
|
|
|
|
// var entry = &context[idx]
|
|
|
|
// if matchesKey(key, entry.Key) {
|
|
|
|
// matches = append(matches, entry)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return matches
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func getMapSlice(context interface{}) yaml.MapSlice {
|
|
|
|
// var mapSlice yaml.MapSlice
|
|
|
|
// switch context := context.(type) {
|
|
|
|
// case yaml.MapSlice:
|
|
|
|
// mapSlice = context
|
|
|
|
// default:
|
|
|
|
// mapSlice = make(yaml.MapSlice, 0)
|
|
|
|
// }
|
|
|
|
// return mapSlice
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func getArray(context interface{}) (array []interface{}, ok bool) {
|
|
|
|
// switch context := context.(type) {
|
|
|
|
// case []interface{}:
|
|
|
|
// array = context
|
|
|
|
// ok = true
|
|
|
|
// default:
|
|
|
|
// array = make([]interface{}, 0)
|
|
|
|
// ok = false
|
|
|
|
// }
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func writeMap(context interface{}, paths []string, value interface{}) interface{} {
|
|
|
|
// log.Debugf("writeMap with path %v for %v to set value %v\n", paths, context, value)
|
|
|
|
|
|
|
|
// mapSlice := getMapSlice(context)
|
|
|
|
|
|
|
|
// if len(paths) == 0 {
|
|
|
|
// return context
|
|
|
|
// }
|
|
|
|
|
|
|
|
// children := entriesInSlice(mapSlice, paths[0])
|
|
|
|
|
|
|
|
// if len(children) == 0 && paths[0] == "*" {
|
|
|
|
// log.Debugf("\tNo matches, return map as is")
|
|
|
|
// return context
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if len(children) == 0 {
|
|
|
|
// newChild := yaml.MapItem{Key: paths[0]}
|
|
|
|
// mapSlice = append(mapSlice, newChild)
|
|
|
|
// children = entriesInSlice(mapSlice, paths[0])
|
|
|
|
// log.Debugf("\tAppended child at %v for mapSlice %v\n", paths[0], mapSlice)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// remainingPaths := paths[1:]
|
|
|
|
// for _, child := range children {
|
|
|
|
// 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
|
|
|
|
// }
|
|
|
|
// log.Debugf("updatedChildValue for child %v with path %v to set value %v", child, remainingPaths, value)
|
|
|
|
// log.Debugf("type of child is %v", reflect.TypeOf(child))
|
|
|
|
|
|
|
|
// switch child := child.(type) {
|
|
|
|
// case nil:
|
|
|
|
// if remainingPaths[0] == "+" || remainingPaths[0] == "*" {
|
|
|
|
// return writeArray(child, remainingPaths, value)
|
|
|
|
// }
|
|
|
|
// case []interface{}:
|
|
|
|
// _, nextIndexErr := strconv.ParseInt(remainingPaths[0], 10, 64)
|
|
|
|
// arrayCommand := nextIndexErr == nil || remainingPaths[0] == "+" || remainingPaths[0] == "*"
|
|
|
|
// if arrayCommand {
|
|
|
|
// return writeArray(child, remainingPaths, value)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return writeMap(child, remainingPaths, value)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func writeArray(context interface{}, paths []string, value interface{}) []interface{} {
|
|
|
|
// log.Debugf("writeArray with path %v for %v to set value %v\n", paths, context, value)
|
|
|
|
// array, _ := getArray(context)
|
|
|
|
|
|
|
|
// if len(paths) == 0 {
|
|
|
|
// return array
|
|
|
|
// }
|
|
|
|
|
|
|
|
// log.Debugf("\tarray %v\n", array)
|
|
|
|
|
|
|
|
// rawIndex := paths[0]
|
|
|
|
// remainingPaths := paths[1:]
|
|
|
|
// var index int64
|
|
|
|
// // the append array indicator
|
|
|
|
// if rawIndex == "+" {
|
|
|
|
// index = int64(len(array))
|
|
|
|
// } else if rawIndex == "*" {
|
|
|
|
// for index, oldChild := range array {
|
|
|
|
// array[index] = updatedChildValue(oldChild, remainingPaths, value)
|
|
|
|
// }
|
|
|
|
// return array
|
|
|
|
// } else {
|
|
|
|
// index, _ = strconv.ParseInt(rawIndex, 10, 64) // nolint
|
|
|
|
// // writeArray is only called by updatedChildValue which handles parsing the
|
|
|
|
// // index, as such this renders this dead code.
|
|
|
|
// }
|
|
|
|
|
|
|
|
// for index >= int64(len(array)) {
|
|
|
|
// array = append(array, nil)
|
|
|
|
// }
|
|
|
|
// currentChild := array[index]
|
|
|
|
|
|
|
|
// log.Debugf("\tcurrentChild %v\n", currentChild)
|
|
|
|
|
|
|
|
// array[index] = updatedChildValue(currentChild, remainingPaths, value)
|
|
|
|
// log.Debugf("\tReturning array %v\n", array)
|
|
|
|
// return array
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func readMap(context yaml.MapSlice, head string, tail []string) (interface{}, error) {
|
|
|
|
// log.Debugf("readingMap %v with key %v\n", context, head)
|
|
|
|
// if head == "*" {
|
|
|
|
// return readMapSplat(context, tail)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// entries := entriesInSlice(context, head)
|
|
|
|
// if len(entries) == 1 {
|
|
|
|
// return calculateValue(entries[0].Value, tail)
|
|
|
|
// } else if len(entries) == 0 {
|
|
|
|
// return nil, nil
|
|
|
|
// }
|
|
|
|
// var errInIdx error
|
|
|
|
// values := make([]interface{}, len(entries))
|
|
|
|
// for idx, entry := range entries {
|
|
|
|
// values[idx], errInIdx = calculateValue(entry.Value, tail)
|
|
|
|
// if errInIdx != nil {
|
|
|
|
// log.Errorf("Error updating index %v in %v", idx, context)
|
|
|
|
// return nil, errInIdx
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
// return values, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func readMapSplat(context yaml.MapSlice, tail []string) (interface{}, error) {
|
|
|
|
// var newArray = make([]interface{}, len(context))
|
|
|
|
// var i = 0
|
|
|
|
// for _, entry := range context {
|
|
|
|
// if len(tail) > 0 {
|
|
|
|
// val, err := recurse(entry.Value, tail[0], tail[1:])
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// newArray[i] = val
|
|
|
|
// } else {
|
|
|
|
// newArray[i] = entry.Value
|
|
|
|
// }
|
|
|
|
// i++
|
|
|
|
// }
|
|
|
|
// return newArray, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func recurse(value interface{}, head string, tail []string) (interface{}, error) {
|
|
|
|
// switch value := value.(type) {
|
|
|
|
// case []interface{}:
|
|
|
|
// if head == "*" {
|
|
|
|
// return readArraySplat(value, tail)
|
|
|
|
// }
|
|
|
|
// index, err := strconv.ParseInt(head, 10, 64)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, fmt.Errorf("error accessing array: %v", err)
|
|
|
|
// }
|
|
|
|
// return readArray(value, index, tail)
|
|
|
|
// case yaml.MapSlice:
|
|
|
|
// return readMap(value, head, tail)
|
|
|
|
// default:
|
|
|
|
// return nil, nil
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func readArray(array []interface{}, head int64, tail []string) (interface{}, error) {
|
|
|
|
// if head >= int64(len(array)) {
|
|
|
|
// return nil, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// value := array[head]
|
|
|
|
// return calculateValue(value, tail)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func readArraySplat(array []interface{}, tail []string) (interface{}, error) {
|
|
|
|
// var newArray = make([]interface{}, len(array))
|
|
|
|
// for index, value := range array {
|
|
|
|
// val, err := calculateValue(value, tail)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// newArray[index] = val
|
|
|
|
// }
|
|
|
|
// return newArray, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func calculateValue(value interface{}, tail []string) (interface{}, error) {
|
|
|
|
// if len(tail) > 0 {
|
|
|
|
// return recurse(value, tail[0], tail[1:])
|
|
|
|
// }
|
|
|
|
// return value, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func deleteMap(context interface{}, paths []string) (yaml.MapSlice, error) {
|
|
|
|
// log.Debugf("deleteMap for %v for %v\n", paths, context)
|
|
|
|
|
|
|
|
// mapSlice := getMapSlice(context)
|
|
|
|
|
|
|
|
// if len(paths) == 0 {
|
|
|
|
// return mapSlice, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// var index int
|
|
|
|
// var child yaml.MapItem
|
|
|
|
// for index, child = range mapSlice {
|
|
|
|
// if matchesKey(paths[0], child.Key) {
|
|
|
|
// log.Debugf("\tMatched [%v] with [%v] at index %v", paths[0], child.Key, index)
|
|
|
|
// var badDelete error
|
|
|
|
// mapSlice, badDelete = deleteEntryInMap(mapSlice, child, index, paths)
|
|
|
|
// if badDelete != nil {
|
|
|
|
// return nil, badDelete
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return mapSlice, nil
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func deleteEntryInMap(original yaml.MapSlice, child yaml.MapItem, index int, paths []string) (yaml.MapSlice, error) {
|
|
|
|
// remainingPaths := paths[1:]
|
|
|
|
|
|
|
|
// var newSlice yaml.MapSlice
|
|
|
|
// if len(remainingPaths) > 0 {
|
|
|
|
// newChild := yaml.MapItem{Key: child.Key}
|
|
|
|
// var errorDeleting error
|
|
|
|
// newChild.Value, errorDeleting = deleteChildValue(child.Value, remainingPaths)
|
|
|
|
// if errorDeleting != nil {
|
|
|
|
// return nil, errorDeleting
|
|
|
|
// }
|
|
|
|
|
|
|
|
// newSlice = make(yaml.MapSlice, len(original))
|
|
|
|
// for i := range original {
|
|
|
|
// item := original[i]
|
|
|
|
// if i == index {
|
|
|
|
// item = newChild
|
|
|
|
// }
|
|
|
|
// newSlice[i] = item
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// // Delete item from slice at index
|
|
|
|
// newSlice = append(original[:index], original[index+1:]...)
|
|
|
|
// log.Debugf("\tDeleted item index %d from original", index)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// log.Debugf("\tReturning original %v\n", original)
|
|
|
|
// return newSlice, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func deleteArraySplat(array []interface{}, tail []string) (interface{}, error) {
|
|
|
|
// log.Debugf("deleteArraySplat for %v for %v\n", tail, array)
|
|
|
|
// var newArray = make([]interface{}, len(array))
|
|
|
|
// for index, value := range array {
|
|
|
|
// val, err := deleteChildValue(value, tail)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// newArray[index] = val
|
|
|
|
// }
|
|
|
|
// return newArray, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func deleteArray(array []interface{}, paths []string, index int64) (interface{}, error) {
|
|
|
|
// log.Debugf("deleteArray for %v for %v\n", paths, array)
|
|
|
|
|
|
|
|
// if index >= int64(len(array)) {
|
|
|
|
// return array, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// remainingPaths := paths[1:]
|
|
|
|
// if len(remainingPaths) > 0 {
|
|
|
|
// // Recurse into the array element at index
|
|
|
|
// var errorDeleting error
|
|
|
|
// array[index], errorDeleting = deleteMap(array[index], remainingPaths)
|
|
|
|
// if errorDeleting != nil {
|
|
|
|
// return nil, errorDeleting
|
|
|
|
// }
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
// // Delete the array element at index
|
|
|
|
// array = append(array[:index], array[index+1:]...)
|
|
|
|
// log.Debugf("\tDeleted item index %d from array, leaving %v", index, array)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// log.Debugf("\tReturning array: %v\n", array)
|
|
|
|
// return array, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func deleteChildValue(child interface{}, remainingPaths []string) (interface{}, error) {
|
|
|
|
// log.Debugf("deleteChildValue for %v for %v\n", remainingPaths, child)
|
|
|
|
// var head = remainingPaths[0]
|
|
|
|
// var tail = remainingPaths[1:]
|
|
|
|
// switch child := child.(type) {
|
|
|
|
// case yaml.MapSlice:
|
|
|
|
// return deleteMap(child, remainingPaths)
|
|
|
|
// case []interface{}:
|
|
|
|
// if head == "*" {
|
|
|
|
// return deleteArraySplat(child, tail)
|
|
|
|
// }
|
|
|
|
// index, err := strconv.ParseInt(head, 10, 64)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, fmt.Errorf("error accessing array: %v", err)
|
|
|
|
// }
|
|
|
|
// return deleteArray(child, remainingPaths, index)
|
|
|
|
// }
|
|
|
|
// return child, nil
|
|
|
|
// }
|