2023-03-15 09:14:23 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func moduloOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
log.Debugf("Modulo operator")
|
|
|
|
|
|
|
|
return crossFunction(d, context.ReadOnlyClone(), expressionNode, modulo, false)
|
|
|
|
}
|
|
|
|
|
2023-06-07 17:45:42 +00:00
|
|
|
func modulo(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
2023-04-11 02:39:22 +00:00
|
|
|
if lhs.Tag == "!!null" {
|
|
|
|
return nil, fmt.Errorf("%v (%v) cannot modulo 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 := moduloScalars(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 modulo 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 moduloScalars(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 == "!!int" && rhsTag == "!!int" {
|
2023-04-11 02:39:22 +00:00
|
|
|
target.Kind = ScalarNode
|
2023-03-15 09:14:23 +00:00
|
|
|
target.Style = lhs.Style
|
|
|
|
|
|
|
|
format, lhsNum, err := parseInt64(lhs.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, rhsNum, err := parseInt64(rhs.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rhsNum == 0 {
|
|
|
|
return fmt.Errorf("cannot modulo by 0")
|
|
|
|
}
|
|
|
|
remainder := lhsNum % rhsNum
|
|
|
|
|
|
|
|
target.Tag = lhs.Tag
|
|
|
|
target.Value = fmt.Sprintf(format, remainder)
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
remainder := math.Mod(lhsNum, rhsNum)
|
|
|
|
if lhsIsCustom {
|
|
|
|
target.Tag = lhs.Tag
|
|
|
|
} else {
|
|
|
|
target.Tag = "!!float"
|
|
|
|
}
|
|
|
|
target.Value = fmt.Sprintf("%v", remainder)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("%v cannot modulo by %v", lhsTag, rhsTag)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|