Fixed recursive decent on empty objects/arrays

This commit is contained in:
Mike Farah 2020-11-25 15:01:12 +11:00
parent 0a66bb797d
commit 5205f01248
6 changed files with 27 additions and 5 deletions

View File

@ -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

View File

@ -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)
- string scalars: concatenate (soon)

View File

@ -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{}

View File

@ -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]}}`,

View File

@ -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))

View File

@ -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`,