Fixed null issue with entry operators

This commit is contained in:
Mike Farah 2021-05-10 10:42:43 +10:00
parent 6c3965dca3
commit a2bd463a91
3 changed files with 27 additions and 5 deletions

View File

@ -35,6 +35,19 @@ will output
value: b value: b
``` ```
## to_entries null
Given a sample.yml file of:
```yaml
null
```
then
```bash
yq eval 'to_entries' sample.yml
```
will output
```yaml
```
## from_entries map ## from_entries map
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml

View File

@ -59,9 +59,11 @@ func toEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *Ex
case yaml.SequenceNode: case yaml.SequenceNode:
results.PushBack(toEntriesfromSeq(candidate)) results.PushBack(toEntriesfromSeq(candidate))
default: default:
if candidateNode.Tag != "!!null" {
return Context{}, fmt.Errorf("%v has no keys", candidate.Node.Tag) return Context{}, fmt.Errorf("%v has no keys", candidate.Node.Tag)
} }
} }
}
return context.ChildContext(results), nil return context.ChildContext(results), nil
} }
@ -133,26 +135,26 @@ func withEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *
//to_entries on the context //to_entries on the context
toEntries, err := toEntriesOperator(d, context, expressionNode) toEntries, err := toEntriesOperator(d, context, expressionNode)
if err != nil { if err != nil {
return Context{}, nil return Context{}, err
} }
//run expression against entries //run expression against entries
// splat toEntries and pipe it into Rhs // splat toEntries and pipe it into Rhs
splatted, err := splat(d, toEntries, traversePreferences{}) splatted, err := splat(d, toEntries, traversePreferences{})
if err != nil { if err != nil {
return Context{}, nil return Context{}, err
} }
result, err := d.GetMatchingNodes(splatted, expressionNode.Rhs) result, err := d.GetMatchingNodes(splatted, expressionNode.Rhs)
log.Debug("expressionNode.Rhs %v", expressionNode.Rhs.Operation.OperationType) log.Debug("expressionNode.Rhs %v", expressionNode.Rhs.Operation.OperationType)
log.Debug("result %v", result) log.Debug("result %v", result)
if err != nil { if err != nil {
return Context{}, nil return Context{}, err
} }
collected, err := collectOperator(d, result, expressionNode) collected, err := collectOperator(d, result, expressionNode)
if err != nil { if err != nil {
return Context{}, nil return Context{}, err
} }
//from_entries on the result //from_entries on the result

View File

@ -21,6 +21,13 @@ var entriesOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::- key: 0\n value: a\n- key: 1\n value: b\n", "D0, P[], (!!seq)::- key: 0\n value: a\n- key: 1\n value: b\n",
}, },
}, },
{
description: "to_entries null",
document: `null`,
expression: `to_entries`,
expected: []string{
},
},
{ {
description: "from_entries map", description: "from_entries map",
document: `{a: 1, b: 2}`, document: `{a: 1, b: 2}`,