This commit is contained in:
Mike Farah 2020-02-28 11:37:27 +11:00
parent 10029420a5
commit 6ef04e1e77
2 changed files with 38 additions and 27 deletions

View File

@ -256,7 +256,7 @@ holderB:
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "holderA: 2\nholderB: 2", result.Output) test.AssertResult(t, "holderA: 2\nholderB: 2\n", result.Output)
} }
func TestReadObjectLengthCmd(t *testing.T) { func TestReadObjectLengthCmd(t *testing.T) {
@ -338,7 +338,7 @@ func TestReadScalarLengthCmd(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
test.AssertResult(t, "2\n", result.Output) test.AssertResult(t, "4\n", result.Output)
} }
func TestReadDeepSplatCmd(t *testing.T) { func TestReadDeepSplatCmd(t *testing.T) {

View File

@ -77,6 +77,30 @@ func appendDocument(originalMatchingNodes []*yqlib.NodeContext, dataBucket yaml.
return append(originalMatchingNodes, matchingNodes...), nil return append(originalMatchingNodes, matchingNodes...), nil
} }
func lengthOf(node *yaml.Node) int {
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:
return len(node.Content) / 2
default:
return len(node.Content)
}
}
// transforms node before printing, if required
func transformNode(node *yaml.Node) *yaml.Node {
if printLength {
return &yaml.Node{Kind: yaml.ScalarNode, Value: fmt.Sprintf("%v", lengthOf(node))}
}
return node
}
func printNode(node *yaml.Node, writer io.Writer) error { func printNode(node *yaml.Node, writer io.Writer) error {
var encoder yqlib.Encoder var encoder yqlib.Encoder
if outputToJSON { if outputToJSON {
@ -128,19 +152,6 @@ func explode(matchingNodes []*yqlib.NodeContext) error {
return nil return nil
} }
func convertToLength(node *yaml.Node) *yaml.Node {
switch kindToCheck {
case yaml.ScalarNode:
return len(node.Value)
case yaml.MappingNode:
return len(node.Content) / 2
default:
return len(node.Content)
}
}
func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error { func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
if prettyPrint { if prettyPrint {
setStyle(matchingNodes, 0) setStyle(matchingNodes, 0)
@ -177,12 +188,12 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
var parentNode = yaml.Node{Kind: yaml.MappingNode} var parentNode = yaml.Node{Kind: yaml.MappingNode}
parentNode.Content = make([]*yaml.Node, 2) parentNode.Content = make([]*yaml.Node, 2)
parentNode.Content[0] = &yaml.Node{Kind: yaml.ScalarNode, Value: lib.PathStackToString(mappedDoc.PathStack)} parentNode.Content[0] = &yaml.Node{Kind: yaml.ScalarNode, Value: lib.PathStackToString(mappedDoc.PathStack)}
parentNode.Content[1] = mappedDoc.Node parentNode.Content[1] = transformNode(mappedDoc.Node)
if err := printNode(&parentNode, bufferedWriter); err != nil { if err := printNode(&parentNode, bufferedWriter); err != nil {
return err return err
} }
default: default:
if err := printNode(mappedDoc.Node, bufferedWriter); err != nil { if err := printNode(transformNode(mappedDoc.Node), bufferedWriter); err != nil {
return err return err
} }
} }