2023-03-15 09:14:23 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func divideOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
log.Debugf("Divide operator")
|
|
|
|
|
|
|
|
return crossFunction(d, context.ReadOnlyClone(), expressionNode, divide, false)
|
|
|
|
}
|
|
|
|
|
2023-04-11 02:39:22 +00:00
|
|
|
func divide(d *dataTreeNavigator, context Context, lhsW *CandidateNode, rhsW *CandidateNode) (*CandidateNode, error) {
|
|
|
|
lhs := lhsW.unwrapDocument()
|
|
|
|
rhs := rhsW.unwrapDocument()
|
2023-03-15 09:14:23 +00:00
|
|
|
|
2023-04-11 02:39:22 +00:00
|
|
|
if lhs.Tag == "!!null" {
|
|
|
|
return nil, fmt.Errorf("%v (%v) cannot be divided by %v (%v)", lhs.Tag, lhs.GetNicePath(), rhs.Tag, rhs.GetNicePath())
|
2023-03-15 09:14:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 02:39:22 +00:00
|
|
|
target := lhs.CopyWithoutContent()
|
2023-03-15 09:14:23 +00:00
|
|
|
|
2023-04-11 02:39:22 +00:00
|
|
|
if lhs.Kind == ScalarNode && rhs.Kind == ScalarNode {
|
|
|
|
if err := divideScalars(target, lhs, rhs); err != nil {
|
2023-03-15 09:14:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-11 02:39:22 +00:00
|
|
|
return nil, fmt.Errorf("%v (%v) cannot be divided by %v (%v)", lhs.Tag, lhs.GetNicePath(), rhs.Tag, rhs.GetNicePath())
|
2023-03-15 09:14:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 02:39:22 +00:00
|
|
|
return target, nil
|
2023-03-15 09:14:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 02:39:22 +00:00
|
|
|
func divideScalars(target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {
|
2023-03-15 09:14:23 +00:00
|
|
|
lhsTag := lhs.Tag
|
2023-04-11 02:39:22 +00:00
|
|
|
rhsTag := rhs.guessTagFromCustomType()
|
2023-03-15 09:14:23 +00:00
|
|
|
lhsIsCustom := false
|
|
|
|
if !strings.HasPrefix(lhsTag, "!!") {
|
|
|
|
// custom tag - we have to have a guess
|
2023-04-11 02:39:22 +00:00
|
|
|
lhsTag = lhs.guessTagFromCustomType()
|
2023-03-15 09:14:23 +00:00
|
|
|
lhsIsCustom = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if lhsTag == "!!str" && rhsTag == "!!str" {
|
2023-04-11 02:39:22 +00:00
|
|
|
tKind, tTag, res := split(lhs.Value, rhs.Value)
|
|
|
|
target.Kind = tKind
|
|
|
|
target.Tag = tTag
|
|
|
|
target.Content = res
|
2023-03-15 09:14:23 +00:00
|
|
|
} else if (lhsTag == "!!int" || lhsTag == "!!float") && (rhsTag == "!!int" || rhsTag == "!!float") {
|
2023-04-11 02:39:22 +00:00
|
|
|
target.Kind = ScalarNode
|
2023-03-15 09:14:23 +00:00
|
|
|
target.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.Tag = lhs.Tag
|
|
|
|
} else {
|
|
|
|
target.Tag = "!!float"
|
|
|
|
}
|
|
|
|
target.Value = fmt.Sprintf("%v", quotient)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("%v cannot be divided by %v", lhsTag, rhsTag)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|