mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-07 02:48:27 +08:00
Generic ast (#1829)
Remove dependency on yaml.Node for internal AST representation. Yaml decoder is now just another decoder.
This commit is contained in:
+20
-22
@@ -3,33 +3,31 @@ package yqlib
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
|
||||
yaml "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func pickMap(original *yaml.Node, indices *yaml.Node) *yaml.Node {
|
||||
func pickMap(original *CandidateNode, indices *CandidateNode) *CandidateNode {
|
||||
|
||||
filteredContent := make([]*yaml.Node, 0)
|
||||
filteredContent := make([]*CandidateNode, 0)
|
||||
for index := 0; index < len(indices.Content); index = index + 1 {
|
||||
keyToFind := indices.Content[index]
|
||||
|
||||
indexInMap := findKeyInMap(original, keyToFind)
|
||||
if indexInMap > -1 {
|
||||
clonedKey := deepClone(original.Content[indexInMap])
|
||||
clonedValue := deepClone(original.Content[indexInMap+1])
|
||||
clonedKey := original.Content[indexInMap].Copy()
|
||||
clonedValue := original.Content[indexInMap+1].Copy()
|
||||
filteredContent = append(filteredContent, clonedKey, clonedValue)
|
||||
}
|
||||
}
|
||||
|
||||
newNode := deepCloneNoContent(original)
|
||||
newNode.Content = filteredContent
|
||||
newNode := original.CopyWithoutContent()
|
||||
newNode.AddChildren(filteredContent)
|
||||
|
||||
return newNode
|
||||
}
|
||||
|
||||
func pickSequence(original *yaml.Node, indices *yaml.Node) (*yaml.Node, error) {
|
||||
func pickSequence(original *CandidateNode, indices *CandidateNode) (*CandidateNode, error) {
|
||||
|
||||
filteredContent := make([]*yaml.Node, 0)
|
||||
filteredContent := make([]*CandidateNode, 0)
|
||||
for index := 0; index < len(indices.Content); index = index + 1 {
|
||||
indexInArray, err := parseInt(indices.Content[index].Value)
|
||||
if err != nil {
|
||||
@@ -37,12 +35,12 @@ func pickSequence(original *yaml.Node, indices *yaml.Node) (*yaml.Node, error) {
|
||||
}
|
||||
|
||||
if indexInArray > -1 && indexInArray < len(original.Content) {
|
||||
filteredContent = append(filteredContent, deepClone(original.Content[indexInArray]))
|
||||
filteredContent = append(filteredContent, original.Content[indexInArray].Copy())
|
||||
}
|
||||
}
|
||||
|
||||
newNode := deepCloneNoContent(original)
|
||||
newNode.Content = filteredContent
|
||||
newNode := original.CopyWithoutContent()
|
||||
newNode.AddChildren(filteredContent)
|
||||
|
||||
return newNode, nil
|
||||
}
|
||||
@@ -55,31 +53,31 @@ func pickOperator(d *dataTreeNavigator, context Context, expressionNode *Express
|
||||
if err != nil {
|
||||
return Context{}, err
|
||||
}
|
||||
indicesToPick := &yaml.Node{}
|
||||
indicesToPick := &CandidateNode{}
|
||||
if contextIndicesToPick.MatchingNodes.Len() > 0 {
|
||||
indicesToPick = contextIndicesToPick.MatchingNodes.Front().Value.(*CandidateNode).Node
|
||||
indicesToPick = contextIndicesToPick.MatchingNodes.Front().Value.(*CandidateNode)
|
||||
}
|
||||
|
||||
var results = list.New()
|
||||
|
||||
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
||||
candidate := el.Value.(*CandidateNode)
|
||||
node := unwrapDoc(candidate.Node)
|
||||
node := el.Value.(*CandidateNode)
|
||||
|
||||
var replacement *yaml.Node
|
||||
if node.Kind == yaml.MappingNode {
|
||||
var replacement *CandidateNode
|
||||
if node.Kind == MappingNode {
|
||||
replacement = pickMap(node, indicesToPick)
|
||||
} else if node.Kind == yaml.SequenceNode {
|
||||
} else if node.Kind == SequenceNode {
|
||||
replacement, err = pickSequence(node, indicesToPick)
|
||||
if err != nil {
|
||||
return Context{}, err
|
||||
}
|
||||
|
||||
} else {
|
||||
return Context{}, fmt.Errorf("cannot pick indices from type %v (%v)", node.Tag, candidate.GetNicePath())
|
||||
return Context{}, fmt.Errorf("cannot pick indices from type %v (%v)", node.Tag, node.GetNicePath())
|
||||
}
|
||||
|
||||
results.PushBack(candidate.CreateReplacementWithDocWrappers(replacement))
|
||||
replacement.LeadingContent = node.LeadingContent
|
||||
results.PushBack(replacement)
|
||||
}
|
||||
|
||||
return context.ChildContext(results), nil
|
||||
|
||||
Reference in New Issue
Block a user