Generic ast (#1829)

Remove dependency on yaml.Node for internal AST representation. Yaml decoder is now just another decoder.
This commit is contained in:
Mike Farah
2023-10-18 12:11:53 +11:00
committed by GitHub
parent 7430419413
commit 13d1bbb45f
171 changed files with 6402 additions and 2421 deletions
+9 -8
View File
@@ -2,8 +2,6 @@ package yqlib
import (
"sort"
yaml "gopkg.in/yaml.v3"
)
func sortKeysOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
@@ -16,8 +14,8 @@ func sortKeysOperator(d *dataTreeNavigator, context Context, expressionNode *Exp
}
for childEl := rhs.MatchingNodes.Front(); childEl != nil; childEl = childEl.Next() {
node := unwrapDoc(childEl.Value.(*CandidateNode).Node)
if node.Kind == yaml.MappingNode {
node := childEl.Value.(*CandidateNode)
if node.Kind == MappingNode {
sortKeys(node)
}
if err != nil {
@@ -29,10 +27,10 @@ func sortKeysOperator(d *dataTreeNavigator, context Context, expressionNode *Exp
return context, nil
}
func sortKeys(node *yaml.Node) {
func sortKeys(node *CandidateNode) {
keys := make([]string, len(node.Content)/2)
keyBucket := map[string]*yaml.Node{}
valueBucket := map[string]*yaml.Node{}
keyBucket := map[string]*CandidateNode{}
valueBucket := map[string]*CandidateNode{}
var contents = node.Content
for index := 0; index < len(contents); index = index + 2 {
key := contents[index]
@@ -42,11 +40,14 @@ func sortKeys(node *yaml.Node) {
valueBucket[key.Value] = value
}
sort.Strings(keys)
sortedContent := make([]*yaml.Node, len(node.Content))
sortedContent := make([]*CandidateNode, len(node.Content))
for index := 0; index < len(keys); index = index + 1 {
keyString := keys[index]
sortedContent[index*2] = keyBucket[keyString]
sortedContent[1+(index*2)] = valueBucket[keyString]
}
// re-arranging children, no need to update their parent
// relationship
node.Content = sortedContent
}