mirror of
https://github.com/mikefarah/yq.git
synced 2026-08-31 21:14:46 +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:
@@ -7,8 +7,6 @@ import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func configureEncoder(format PrinterOutputFormat, indent int) Encoder {
|
||||
@@ -78,9 +76,8 @@ func encodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
|
||||
if originalList != nil && originalList.Len() > 0 && hasOnlyOneNewLine.MatchString(stringValue) {
|
||||
|
||||
original := originalList.Front().Value.(*CandidateNode)
|
||||
originalNode := unwrapDoc(original.Node)
|
||||
// original block did not have a newline at the end, get rid of this one too
|
||||
if !endWithNewLine.MatchString(originalNode.Value) {
|
||||
if !endWithNewLine.MatchString(original.Value) {
|
||||
stringValue = chomper.ReplaceAllString(stringValue, "")
|
||||
}
|
||||
}
|
||||
@@ -92,8 +89,7 @@ func encodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
|
||||
stringValue = chomper.ReplaceAllString(stringValue, "")
|
||||
}
|
||||
|
||||
stringContentNode := &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: stringValue}
|
||||
results.PushBack(candidate.CreateReplacement(stringContentNode))
|
||||
results.PushBack(candidate.CreateReplacement(ScalarNode, "!!str", stringValue))
|
||||
}
|
||||
return context.ChildContext(results), nil
|
||||
}
|
||||
@@ -141,21 +137,21 @@ func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
|
||||
|
||||
context.SetVariable("decoded: "+candidate.GetKey(), candidate.AsList())
|
||||
|
||||
log.Debugf("got: [%v]", candidate.Node.Value)
|
||||
log.Debugf("got: [%v]", candidate.Value)
|
||||
|
||||
err := decoder.Init(strings.NewReader(unwrapDoc(candidate.Node).Value))
|
||||
err := decoder.Init(strings.NewReader(candidate.Value))
|
||||
if err != nil {
|
||||
return Context{}, err
|
||||
}
|
||||
|
||||
decodedNode, errorReading := decoder.Decode()
|
||||
node, errorReading := decoder.Decode()
|
||||
if errorReading != nil {
|
||||
return Context{}, errorReading
|
||||
}
|
||||
//first node is a doc
|
||||
node := unwrapDoc(decodedNode.Node)
|
||||
node.Key = candidate.Key
|
||||
node.Parent = candidate.Parent
|
||||
|
||||
results.PushBack(candidate.CreateReplacement(node))
|
||||
results.PushBack(node)
|
||||
}
|
||||
return context.ChildContext(results), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user