From 2937645fcd7dd0237e663a1e4cc358ca764e1d0a Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 27 Feb 2020 10:06:43 +1100 Subject: [PATCH] wip --- cmd/commands_test.go | 151 +++++++++++++++++++++++++++++++++++++++++++ cmd/utils.go | 21 +++--- 2 files changed, 163 insertions(+), 9 deletions(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index d7d7b0b..fd1f5b1 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -190,6 +190,157 @@ func TestReadArrayCmd(t *testing.T) { 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) { cmd := getRootCommand() result := test.RunCmd(cmd, "read -p pv ../examples/sample.yaml b.**") diff --git a/cmd/utils.go b/cmd/utils.go index c06e738..96548a6 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -78,7 +78,12 @@ func appendDocument(originalMatchingNodes []*yqlib.NodeContext, dataBucket yaml. } 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: return len(node.Value) case yaml.MappingNode: @@ -175,7 +180,7 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error { return nil } - var arrayNode = yaml.Node{Kind: yaml.SequenceNode} + var counter = 0 var errorWriting error for _, mappedDoc := range matchingNodes { @@ -195,22 +200,20 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error { } else { parentNode.Content[1] = mappedDoc.Node } - if resultsAsArray { - arrayNode.Content = append(arrayNode.Content, &parentNode) - } else if err := printValue(&parentNode, bufferedWriter, false); err != nil { + if err := printValue(&parentNode, bufferedWriter, false); err != nil { return err } default: - if resultsAsArray || printLength { - arrayNode.Content = append(arrayNode.Content, mappedDoc.Node) + if printLength { + counter = counter + lengthOf(mappedDoc.Node) } else if err := printValue(mappedDoc.Node, bufferedWriter, false); err != nil { return err } } } - if resultsAsArray || (printMode == "v" && printLength) { - if err := printValue(&arrayNode, bufferedWriter, printLength); err != nil { + if printLength { + if err := writeString(bufferedWriter, fmt.Sprintf("%v\n", counter)); err != nil { return err } }