more docs

This commit is contained in:
Mike Farah
2020-11-14 13:38:44 +11:00
parent 860655b4cd
commit db4762ef7c
6 changed files with 117 additions and 52 deletions
+28 -20
View File
@@ -3,19 +3,15 @@ package yqlib
import (
"container/list"
"gopkg.in/yaml.v3"
yaml "gopkg.in/yaml.v3"
)
func DeleteChildOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
if err != nil {
return nil, err
}
// for each lhs, splat the node,
// the intersect it against the rhs expression
// recreate the contents using only the intersection result.
for el := lhs.Front(); el != nil; el = el.Next() {
for el := matchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
elMap := list.New()
elMap.PushBack(candidate)
@@ -25,20 +21,22 @@ func DeleteChildOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNod
return nil, err
}
if candidate.Node.Kind == yaml.SequenceNode {
realNode := UnwrapDoc(candidate.Node)
if realNode.Kind == yaml.SequenceNode {
deleteFromArray(candidate, nodesToDelete)
} else if candidate.Node.Kind == yaml.MappingNode {
} else if realNode.Kind == yaml.MappingNode {
deleteFromMap(candidate, nodesToDelete)
} else {
log.Debug("Cannot delete from node that's not a map or array %v", NodeToString(candidate))
}
}
return lhs, nil
return matchingNodes, nil
}
func deleteFromMap(candidate *CandidateNode, nodesToDelete *list.List) {
log.Debug("deleteFromMap")
node := candidate.Node
node := UnwrapDoc(candidate.Node)
contents := node.Content
newContents := make([]*yaml.Node, 0)
@@ -51,8 +49,13 @@ func deleteFromMap(candidate *CandidateNode, nodesToDelete *list.List) {
Document: candidate.Document,
Path: append(candidate.Path, key.Value),
}
// _, shouldDelete := nodesToDelete.Get(childCandidate.GetKey())
shouldDelete := true
shouldDelete := false
for el := nodesToDelete.Front(); el != nil && shouldDelete == false; el = el.Next() {
if el.Value.(*CandidateNode).GetKey() == childCandidate.GetKey() {
shouldDelete = true
}
}
log.Debugf("shouldDelete %v ? %v", childCandidate.GetKey(), shouldDelete)
@@ -65,21 +68,26 @@ func deleteFromMap(candidate *CandidateNode, nodesToDelete *list.List) {
func deleteFromArray(candidate *CandidateNode, nodesToDelete *list.List) {
log.Debug("deleteFromArray")
node := candidate.Node
node := UnwrapDoc(candidate.Node)
contents := node.Content
newContents := make([]*yaml.Node, 0)
for index := 0; index < len(contents); index = index + 1 {
value := contents[index]
// childCandidate := &CandidateNode{
// Node: value,
// Document: candidate.Document,
// Path: append(candidate.Path, index),
// }
childCandidate := &CandidateNode{
Node: value,
Document: candidate.Document,
Path: append(candidate.Path, index),
}
shouldDelete := false
for el := nodesToDelete.Front(); el != nil && shouldDelete == false; el = el.Next() {
if el.Value.(*CandidateNode).GetKey() == childCandidate.GetKey() {
shouldDelete = true
}
}
// _, shouldDelete := nodesToDelete.Get(childCandidate.GetKey())
shouldDelete := true
if !shouldDelete {
newContents = append(newContents, value)
}