mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
Fixed writing to null document
This commit is contained in:
parent
0347516d82
commit
44322f0248
@ -1245,6 +1245,25 @@ func TestWriteCmd(t *testing.T) {
|
|||||||
test.AssertResult(t, expectedOutput, result.Output)
|
test.AssertResult(t, expectedOutput, result.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteEmptyMultiDocCmd(t *testing.T) {
|
||||||
|
content := `# this is empty
|
||||||
|
---
|
||||||
|
`
|
||||||
|
filename := test.WriteTempYamlFile(content)
|
||||||
|
defer test.RemoveTempYamlFile(filename)
|
||||||
|
|
||||||
|
cmd := getRootCommand()
|
||||||
|
result := test.RunCmd(cmd, fmt.Sprintf("write %s c 7", filename))
|
||||||
|
if result.Error != nil {
|
||||||
|
t.Error(result.Error)
|
||||||
|
}
|
||||||
|
expectedOutput := `c: 7
|
||||||
|
|
||||||
|
# this is empty
|
||||||
|
`
|
||||||
|
test.AssertResult(t, expectedOutput, result.Output)
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteFromFileCmd(t *testing.T) {
|
func TestWriteFromFileCmd(t *testing.T) {
|
||||||
content := `b:
|
content := `b:
|
||||||
c: 3
|
c: 3
|
||||||
|
10
cmd/utils.go
10
cmd/utils.go
@ -199,6 +199,11 @@ func parseDocumentIndex() (bool, int, error) {
|
|||||||
|
|
||||||
type updateDataFn func(dataBucket *yaml.Node, currentIndex int) error
|
type updateDataFn func(dataBucket *yaml.Node, currentIndex int) error
|
||||||
|
|
||||||
|
func isNullDocument(dataBucket *yaml.Node) bool {
|
||||||
|
return dataBucket.Kind == yaml.DocumentNode && (len(dataBucket.Content) == 0 ||
|
||||||
|
dataBucket.Content[0].Kind == yaml.ScalarNode && dataBucket.Content[0].Tag == "!!null")
|
||||||
|
}
|
||||||
|
|
||||||
func mapYamlDecoder(updateData updateDataFn, encoder yqlib.Encoder) yamlDecoderFn {
|
func mapYamlDecoder(updateData updateDataFn, encoder yqlib.Encoder) yamlDecoderFn {
|
||||||
return func(decoder *yaml.Decoder) error {
|
return func(decoder *yaml.Decoder) error {
|
||||||
var dataBucket yaml.Node
|
var dataBucket yaml.Node
|
||||||
@ -218,8 +223,11 @@ func mapYamlDecoder(updateData updateDataFn, encoder yqlib.Encoder) yamlDecoderF
|
|||||||
|
|
||||||
if errorReading == io.EOF && docIndexInt == 0 && currentIndex == 0 {
|
if errorReading == io.EOF && docIndexInt == 0 && currentIndex == 0 {
|
||||||
//empty document, lets just make one
|
//empty document, lets just make one
|
||||||
child := yaml.Node{Kind: yaml.MappingNode}
|
|
||||||
dataBucket = yaml.Node{Kind: yaml.DocumentNode, Content: make([]*yaml.Node, 1)}
|
dataBucket = yaml.Node{Kind: yaml.DocumentNode, Content: make([]*yaml.Node, 1)}
|
||||||
|
child := yaml.Node{Kind: yaml.MappingNode}
|
||||||
|
dataBucket.Content[0] = &child
|
||||||
|
} else if isNullDocument(&dataBucket) {
|
||||||
|
child := yaml.Node{Kind: yaml.MappingNode}
|
||||||
dataBucket.Content[0] = &child
|
dataBucket.Content[0] = &child
|
||||||
} else if errorReading == io.EOF {
|
} else if errorReading == io.EOF {
|
||||||
if !updateAll && currentIndex <= docIndexInt {
|
if !updateAll && currentIndex <= docIndexInt {
|
||||||
|
Loading…
Reference in New Issue
Block a user