Fixed bug in alternative op, dont evaluate RHS if LHS is truthy

This commit is contained in:
Mike Farah 2022-09-09 10:18:49 +10:00
parent a12c1a6610
commit a64eea3b1a
3 changed files with 69 additions and 1 deletions

View File

@ -79,3 +79,36 @@ will output
cat cat
``` ```
## Update or create - entity exists
This initialises `a` if it's not present
Given a sample.yml file of:
```yaml
a: 1
```
then
```bash
yq '(.a // (.a = 0)) += 1' sample.yml
```
will output
```yaml
a: 2
```
## Update or create - entity does not exist
This initialises `a` if it's not present
Given a sample.yml file of:
```yaml
b: camel
```
then
```bash
yq '(.a // (.a = 0)) += 1' sample.yml
```
will output
```yaml
b: camel
a: 1
```

View File

@ -2,7 +2,24 @@ package yqlib
func alternativeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { func alternativeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- alternative") log.Debugf("-- alternative")
return crossFunction(d, context, expressionNode, alternativeFunc, true) prefs := crossFunctionPreferences{
CalcWhenEmpty: true,
Calculation: alternativeFunc,
LhsResultValue: func(lhs *CandidateNode) (*CandidateNode, error) {
if lhs == nil {
return nil, nil
}
truthy, err := isTruthy(lhs)
if err != nil {
return nil, err
}
if truthy {
return lhs, nil
}
return nil, nil
},
}
return crossFunctionWithPrefs(d, context, expressionNode, prefs)
} }
func alternativeFunc(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { func alternativeFunc(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {

View File

@ -85,6 +85,24 @@ var alternativeOperatorScenarios = []expressionScenario{
"D0, P[], (!!bool)::true\n", "D0, P[], (!!bool)::true\n",
}, },
}, },
{
description: "Update or create - entity exists",
subdescription: "This initialises `a` if it's not present",
expression: "(.a // (.a = 0)) += 1",
document: `a: 1`,
expected: []string{
"D0, P[], (doc)::a: 2\n",
},
},
{
description: "Update or create - entity does not exist",
subdescription: "This initialises `a` if it's not present",
expression: "(.a // (.a = 0)) += 1",
document: `b: camel`,
expected: []string{
"D0, P[], (!!map)::b: camel\na: 1\n",
},
},
} }
func TestAlternativeOperatorScenarios(t *testing.T) { func TestAlternativeOperatorScenarios(t *testing.T) {