1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00
This commit is contained in:
Mike Farah 2020-02-27 10:06:43 +11:00
parent 1a97b27041
commit 2937645fcd
2 changed files with 163 additions and 9 deletions

View File

@ -190,6 +190,157 @@ func TestReadArrayCmd(t *testing.T) {
test.AssertResult(t, "b.e.[1].name: sam\n", result.Output) test.AssertResult(t, "b.e.[1].name: sam\n", result.Output)
} }
func TestReadArrayLengthCmd(t *testing.T) {
content := `- things
- whatever
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "2\n", result.Output)
}
func TestReadArrayLengthDeepCmd(t *testing.T) {
content := `holder:
- things
- whatever
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s holder", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "2\n", result.Output)
}
func TestReadArrayLengthDeepMultipleCmd(t *testing.T) {
content := `holderA:
- things
- whatever
holderB:
- other things
- cool
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s holder*", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "4\n", result.Output)
}
func TestReadArrayLengthDeepMultipleWithPathCmd(t *testing.T) {
content := `holderA:
- things
- whatever
holderB:
- other things
- cool
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s -ppv holder*", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "holderA: 2\nholderB: 2", result.Output)
}
func TestReadObjectLengthCmd(t *testing.T) {
content := `cat: meow
dog: bark
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "2\n", result.Output)
}
func TestReadObjectLengthDeepCmd(t *testing.T) {
content := `holder:
cat: meow
dog: bark
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s holder", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "2\n", result.Output)
}
func TestReadObjectLengthDeepMultipleCmd(t *testing.T) {
content := `holderA:
cat: meow
dog: bark
holderB:
elephant: meow
zebra: bark
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s holder*", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "4\n", result.Output)
}
func TestReadObjectLengthDeepMultipleWithPathsCmd(t *testing.T) {
content := `holderA:
cat: meow
dog: bark
holderB:
elephant: meow
zebra: bark
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -l -ppv %s holder*", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "holderA: 2\nholderB: 2\n", result.Output)
}
func TestReadScalarLengthCmd(t *testing.T) {
content := `meow`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -l %s", filename))
if result.Error != nil {
t.Error(result.Error)
}
test.AssertResult(t, "2\n", result.Output)
}
func TestReadDeepSplatCmd(t *testing.T) { func TestReadDeepSplatCmd(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := test.RunCmd(cmd, "read -p pv ../examples/sample.yaml b.**") result := test.RunCmd(cmd, "read -p pv ../examples/sample.yaml b.**")

View File

@ -78,7 +78,12 @@ func appendDocument(originalMatchingNodes []*yqlib.NodeContext, dataBucket yaml.
} }
func lengthOf(node *yaml.Node) int { func lengthOf(node *yaml.Node) int {
switch node.Kind { kindToCheck := node.Kind
if node.Kind == yaml.DocumentNode && len(node.Content) == 1 {
log.Debugf("length of document node, calculating length of child")
kindToCheck = node.Content[0].Kind
}
switch kindToCheck {
case yaml.ScalarNode: case yaml.ScalarNode:
return len(node.Value) return len(node.Value)
case yaml.MappingNode: case yaml.MappingNode:
@ -175,7 +180,7 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
return nil return nil
} }
var arrayNode = yaml.Node{Kind: yaml.SequenceNode} var counter = 0
var errorWriting error var errorWriting error
for _, mappedDoc := range matchingNodes { for _, mappedDoc := range matchingNodes {
@ -195,22 +200,20 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
} else { } else {
parentNode.Content[1] = mappedDoc.Node parentNode.Content[1] = mappedDoc.Node
} }
if resultsAsArray { if err := printValue(&parentNode, bufferedWriter, false); err != nil {
arrayNode.Content = append(arrayNode.Content, &parentNode)
} else if err := printValue(&parentNode, bufferedWriter, false); err != nil {
return err return err
} }
default: default:
if resultsAsArray || printLength { if printLength {
arrayNode.Content = append(arrayNode.Content, mappedDoc.Node) counter = counter + lengthOf(mappedDoc.Node)
} else if err := printValue(mappedDoc.Node, bufferedWriter, false); err != nil { } else if err := printValue(mappedDoc.Node, bufferedWriter, false); err != nil {
return err return err
} }
} }
} }
if resultsAsArray || (printMode == "v" && printLength) { if printLength {
if err := printValue(&arrayNode, bufferedWriter, printLength); err != nil { if err := writeString(bufferedWriter, fmt.Sprintf("%v\n", counter)); err != nil {
return err return err
} }
} }