mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-06 18:24:57 +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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user