From 5205f012482583a180724d60612967ff24a41a6c Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 25 Nov 2020 15:01:12 +1100 Subject: [PATCH] Fixed recursive decent on empty objects/arrays --- pkg/yqlib/doc/Add.md | 1 + pkg/yqlib/doc/headers/Add.md | 2 +- pkg/yqlib/lib.go | 4 +--- pkg/yqlib/operator_multiply_test.go | 7 +++++++ pkg/yqlib/operator_recursive_descent.go | 2 +- pkg/yqlib/operator_recursive_descent_test.go | 16 ++++++++++++++++ 6 files changed, 27 insertions(+), 5 deletions(-) diff --git a/pkg/yqlib/doc/Add.md b/pkg/yqlib/doc/Add.md index 6eee9cc0..82550b40 100644 --- a/pkg/yqlib/doc/Add.md +++ b/pkg/yqlib/doc/Add.md @@ -2,6 +2,7 @@ Add behaves differently according to the type of the LHS: - arrays: concatenate - number scalars: arithmetic addition (soon) - string scalars: concatenate (soon) + ## Concatenate arrays Given a sample.yml file of: ```yaml diff --git a/pkg/yqlib/doc/headers/Add.md b/pkg/yqlib/doc/headers/Add.md index dd9f2a38..f451aa95 100644 --- a/pkg/yqlib/doc/headers/Add.md +++ b/pkg/yqlib/doc/headers/Add.md @@ -1,4 +1,4 @@ Add behaves differently according to the type of the LHS: - arrays: concatenate - number scalars: arithmetic addition (soon) -- string scalars: concatenate (soon) \ No newline at end of file +- string scalars: concatenate (soon) diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index e9a393b8..8883d392 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -17,6 +17,7 @@ type OperationType struct { } // operators TODO: +// - cookbook doc for common things // - write in place // - mergeAppend (merges and appends arrays) // - mergeEmpty (sets only if the document is empty, do I do that now?) @@ -70,9 +71,6 @@ var Select = &OperationType{Type: "SELECT", NumArgs: 1, Precedence: 50, Handler: var Has = &OperationType{Type: "HAS", NumArgs: 1, Precedence: 50, Handler: HasOperator} var DeleteChild = &OperationType{Type: "DELETE", NumArgs: 1, Precedence: 40, Handler: DeleteChildOperator} -// var Exists = &OperationType{Type: "Length", NumArgs: 2, Precedence: 35} -// filters matches if they have the existing path - type Operation struct { OperationType *OperationType Value interface{} diff --git a/pkg/yqlib/operator_multiply_test.go b/pkg/yqlib/operator_multiply_test.go index ea29cf47..d16de6fa 100644 --- a/pkg/yqlib/operator_multiply_test.go +++ b/pkg/yqlib/operator_multiply_test.go @@ -13,6 +13,13 @@ var multiplyOperatorScenarios = []expressionScenario{ "D0, P[], (!!map)::{a: {also: me}, b: {also: me}}\n", }, }, + { + skipDoc: true, + expression: `{} * {"cat":"dog"}`, + expected: []string{ + "D0, P[], (!!map)::cat: dog\n", + }, + }, { skipDoc: true, document: `{a: {also: me}, b: {also: [1]}}`, diff --git a/pkg/yqlib/operator_recursive_descent.go b/pkg/yqlib/operator_recursive_descent.go index 78dd67cc..51e1439c 100644 --- a/pkg/yqlib/operator_recursive_descent.go +++ b/pkg/yqlib/operator_recursive_descent.go @@ -26,7 +26,7 @@ func recursiveDecent(d *dataTreeNavigator, results *list.List, matchMap *list.Li log.Debugf("Recursive Decent, added %v", NodeToString(candidate)) results.PushBack(candidate) - if candidate.Node.Kind != yaml.AliasNode { + if candidate.Node.Kind != yaml.AliasNode && len(candidate.Node.Content) > 0 { children, err := Splat(d, nodeToMap(candidate)) diff --git a/pkg/yqlib/operator_recursive_descent_test.go b/pkg/yqlib/operator_recursive_descent_test.go index e08a98ed..c5c7254a 100644 --- a/pkg/yqlib/operator_recursive_descent_test.go +++ b/pkg/yqlib/operator_recursive_descent_test.go @@ -5,6 +5,22 @@ import ( ) var recursiveDescentOperatorScenarios = []expressionScenario{ + { + skipDoc: true, + document: `{}`, + expression: `..`, + expected: []string{ + "D0, P[], (!!map)::{}\n", + }, + }, + { + skipDoc: true, + document: `[]`, + expression: `..`, + expected: []string{ + "D0, P[], (!!seq)::[]\n", + }, + }, { skipDoc: true, document: `cat`,