Skip to content

Commit

Permalink
Added divide operator (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
teejaded committed Mar 9, 2023
1 parent 9539877 commit c1b58f7
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pkg/yqlib/doc/operators/divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

## String split
Given a sample.yml file of:
```yaml
a: cat_meow
b: _
```
then
```bash
yq '.c = .a / .b' sample.yml
```
will output
```yaml
a: cat_meow
b: _
c:
- cat
- meow
```

## Number division
The result during divison is calculated as a float

Given a sample.yml file of:
```yaml
a: 12
b: 2.5
```
then
```bash
yq '.a = .a / .b' sample.yml
```
will output
```yaml
a: 4.8
b: 2.5
```

2 changes: 2 additions & 0 deletions pkg/yqlib/lexer_participle.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ var participleYqRules = []*participleYqRule{
{"MultiplyAssign", `\*=[\+|\?cdn]*`, multiplyWithPrefs(multiplyAssignOpType), 0},
{"Multiply", `\*[\+|\?cdn]*`, multiplyWithPrefs(multiplyOpType), 0},

{"Divide", `\/`, opToken(divideOpType), 0},

{"AddAssign", `\+=`, opToken(addAssignOpType), 0},
{"Add", `\+`, opToken(addOpType), 0},

Expand Down
2 changes: 2 additions & 0 deletions pkg/yqlib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ var assignAliasOpType = &operationType{Type: "ASSIGN_ALIAS", NumArgs: 2, Precede
var multiplyOpType = &operationType{Type: "MULTIPLY", NumArgs: 2, Precedence: 42, Handler: multiplyOperator}
var multiplyAssignOpType = &operationType{Type: "MULTIPLY_ASSIGN", NumArgs: 2, Precedence: 42, Handler: multiplyAssignOperator}

var divideOpType = &operationType{Type: "DIVIDE", NumArgs: 2, Precedence: 42, Handler: divideOperator}

var addOpType = &operationType{Type: "ADD", NumArgs: 2, Precedence: 42, Handler: addOperator}
var subtractOpType = &operationType{Type: "SUBTRACT", NumArgs: 2, Precedence: 42, Handler: subtractOperator}
var alternativeOpType = &operationType{Type: "ALTERNATIVE", NumArgs: 2, Precedence: 42, Handler: alternativeOperator}
Expand Down
111 changes: 111 additions & 0 deletions pkg/yqlib/operator_divide.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package yqlib

import (
"fmt"
"strconv"
"strings"

yaml "gopkg.in/yaml.v3"
)

func createDivideOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode {
return &ExpressionNode{Operation: &Operation{OperationType: divideOpType},
LHS: lhs,
RHS: rhs}
}

// func divideAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
// return compoundAssignFunction(d, context, expressionNode, createDivideOp)
// }

func divideOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("Divide operator")

return crossFunction(d, context.ReadOnlyClone(), expressionNode, divide, false)
}

func divide(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
lhs.Node = unwrapDoc(lhs.Node)
rhs.Node = unwrapDoc(rhs.Node)

lhsNode := lhs.Node

if lhsNode.Tag == "!!null" {
return nil, fmt.Errorf("%v (%v) cannot be divided by %v (%v)", lhsNode.Tag, lhs.GetNicePath(), rhs.Node.Tag, rhs.GetNicePath())
}

target := lhs.CreateReplacement(&yaml.Node{
Anchor: lhs.Node.Anchor,
})

switch lhsNode.Kind {
case yaml.MappingNode:
return nil, fmt.Errorf("%v (%v) cannot be divided by %v (%v)", lhsNode.Tag, lhs.GetNicePath(), rhs.Node.Tag, rhs.GetNicePath())

case yaml.SequenceNode:
return nil, fmt.Errorf("%v (%v) cannot be divided by %v (%v)", lhsNode.Tag, lhs.GetNicePath(), rhs.Node.Tag, rhs.GetNicePath())

case yaml.ScalarNode:
if rhs.Node.Kind != yaml.ScalarNode {
return nil, fmt.Errorf("%v (%v) cannot be divided by %v (%v)", lhsNode.Tag, lhs.GetNicePath(), rhs.Node.Tag, rhs.GetNicePath())
}
if err := divideScalars(context, target, lhsNode, rhs.Node); err != nil {
return nil, err
}
}
return target, nil
}

func divideScalars(context Context, target *CandidateNode, lhs *yaml.Node, rhs *yaml.Node) error {
lhsTag := lhs.Tag
rhsTag := guessTagFromCustomType(rhs)
lhsIsCustom := false
if !strings.HasPrefix(lhsTag, "!!") {
// custom tag - we have to have a guess
lhsTag = guessTagFromCustomType(lhs)
lhsIsCustom = true
}

if lhsTag == "!!str" && rhsTag == "!!str" {
target.Node = split(lhs.Value, rhs.Value)
target.Node.Anchor = lhs.Anchor
} else if lhsTag == "!!int" && rhsTag == "!!int" {
target.Node.Kind = yaml.ScalarNode
target.Node.Style = lhs.Style

format, lhsNum, err := parseInt64(lhs.Value)
if err != nil {
return err
}
_, rhsNum, err := parseInt64(rhs.Value)
if err != nil {
return err
}
quotient := float64(lhsNum) / float64(rhsNum)

target.Node.Tag = "!!float"
target.Node.Value = fmt.Sprintf(format, quotient)
} else if (lhsTag == "!!int" || lhsTag == "!!float") && (rhsTag == "!!int" || rhsTag == "!!float") {
target.Node.Kind = yaml.ScalarNode
target.Node.Style = lhs.Style

lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
if err != nil {
return err
}
rhsNum, err := strconv.ParseFloat(rhs.Value, 64)
if err != nil {
return err
}
quotient := lhsNum / rhsNum
if lhsIsCustom {
target.Node.Tag = lhs.Tag
} else {
target.Node.Tag = "!!float"
}
target.Node.Value = fmt.Sprintf("%v", quotient)
} else {
return fmt.Errorf("%v cannot be divided by %v", lhsTag, rhsTag)
}
return nil
}
121 changes: 121 additions & 0 deletions pkg/yqlib/operator_divide_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package yqlib

import (
"testing"
)

var divideOperatorScenarios = []expressionScenario{
{
skipDoc: true,
document: `[{a: foo_bar, b: _}, {a: 4, b: 2}]`,
expression: ".[] | .a / .b",
expected: []string{
"D0, P[0 a], (!!seq)::- foo\n- bar\n",
"D0, P[1 a], (!!float)::2\n",
},
},
{
skipDoc: true,
document: `{}`,
expression: "(.a / .b) as $x | .",
expected: []string{
"D0, P[], (doc)::{}\n",
},
},
{
description: "String split",
document: `{a: cat_meow, b: _}`,
expression: `.c = .a / .b`,
expected: []string{
"D0, P[], (doc)::{a: cat_meow, b: _, c: [cat, meow]}\n",
},
},
{
description: "Number division",
subdescription: "The result during divison is calculated as a float",
document: `{a: 12, b: 2.5}`,
expression: `.a = .a / .b`,
expected: []string{
"D0, P[], (doc)::{a: 4.8, b: 2.5}\n",
},
},
{
skipDoc: true,
description: "Custom types: that are really strings",
document: "a: !horse cat_meow\nb: !goat _",
expression: `.a = .a / .b`,
expected: []string{
"D0, P[], (doc)::a: !horse\n - cat\n - meow\nb: !goat _\n",
},
},
{
skipDoc: true,
description: "Custom types: that are really numbers",
document: "a: !horse 1.2\nb: !goat 2.3",
expression: `.a = .a / .b`,
expected: []string{
"D0, P[], (doc)::a: !horse 0.5217391304347826\nb: !goat 2.3\n",
},
},
{
skipDoc: true,
document: "a: 2\nb: !goat 2.3",
expression: `.a = .a / .b`,
expected: []string{
"D0, P[], (doc)::a: 0.8695652173913044\nb: !goat 2.3\n",
},
},
{
skipDoc: true,
description: "Custom types: that are really ints",
document: "a: !horse 2\nb: !goat 3",
expression: `.a += .b`,
expected: []string{
"D0, P[], (doc)::a: !horse 5\nb: !goat 3\n",
},
},
{
skipDoc: true,
description: "Keep anchors",
document: "a: &horse [1]",
expression: `.a[1] = .a[0] / 2`,
expected: []string{
"D0, P[], (doc)::a: &horse [1, 0.5]\n",
},
},
{
skipDoc: true,
description: "Divide int by string",
document: "a: 123\nb: '2'",
expression: `.a / .b`,
expectedError: "!!int cannot be divided by !!str",
},
{
skipDoc: true,
description: "Divide string by int",
document: "a: 2\nb: '123'",
expression: `.b / .a`,
expectedError: "!!str cannot be divided by !!int",
},
{
skipDoc: true,
description: "Divide map by int",
document: "a: {\"a\":1}\nb: 2",
expression: `.a / .b`,
expectedError: "!!map (a) cannot be divided by !!int (b)",
},
{
skipDoc: true,
description: "Divide array by str",
document: "a: [1,2]\nb: '2'",
expression: `.a / .b`,
expectedError: "!!seq (a) cannot be divided by !!str (b)",
},
}

func TestDivideOperatorScenarios(t *testing.T) {
for _, tt := range divideOperatorScenarios {
testScenario(t, &tt)
}
documentOperatorScenarios(t, "divide", divideOperatorScenarios)
}

0 comments on commit c1b58f7

Please sign in to comment.