Fixed explode anchors for array roots

This commit is contained in:
Mike Farah 2020-02-12 11:03:40 +11:00
parent 5cc01e43bc
commit 96a4161a92
2 changed files with 33 additions and 2 deletions

View File

@ -322,6 +322,36 @@ pointer: *value-pointer`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadMergeAnchorsExplodeSimpleArrayCmd(t *testing.T) {
content := `- things`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -X %s", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `- things
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadMergeAnchorsExplodeSimpleArrayJsonCmd(t *testing.T) {
content := `- things`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -j %s", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `["things"]
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadMergeAnchorsExplodeSimpleValueForValueCmd(t *testing.T) {
content := `value: &value-pointer the value
pointer: *value-pointer`

View File

@ -115,9 +115,9 @@ func writeString(writer io.Writer, txt string) error {
}
func explode(matchingNodes []*yqlib.NodeContext) error {
log.Debug("exploding nodes")
for _, nodeContext := range matchingNodes {
var targetNode = yaml.Node{Kind: yaml.MappingNode}
var targetNode = yaml.Node{Kind: nodeContext.Node.Kind}
explodedNodes, errorRetrieving := lib.Get(nodeContext.Node, "**", true)
if errorRetrieving != nil {
return errorRetrieving
@ -132,6 +132,7 @@ func explode(matchingNodes []*yqlib.NodeContext) error {
}
nodeContext.Node = &targetNode
}
log.Debug("done exploding nodes")
return nil
}