Add JSON Logic arithmetic operators (+, -, *, /, %)#3483
Draft
ajpallares wants to merge 4 commits into
Draft
Conversation
Extends the JSON Logic operator set with arithmetic so predicates can express counter and sum conditions before the comparison and string operators land. What's new: - `+`, `-`, `*`, `/`, `%` per the JSON Logic spec, including variadic `+` and `*`, the 1-arg numeric-cast form of `+`, and unary negation via `-`. All arithmetic returns `Value.FloatValue(Double)` for consistency with the JS reference (which `parseFloat`s every operand). `looseEq` and `strictEq` already bridge `IntValue(n) ↔ FloatValue(n.0)`, so existing comparisons keep working. - Non-numeric operands (`ObjectValue`, `ArrayValue`, unparseable strings) coerce to `Double.NaN` and propagate through arithmetic — the result is `FloatValue(NaN)`, falsy under `isTruthy`. - Division and modulo by zero return `Value.Null` (deliberate deviation from JS's `Infinity` / `NaN` — friendlier for rule authors and matches the engine's "missing value" convention). Documented in the type's KDoc. Tests: - `ArithmeticOperatorsTest` covers each operator (basic, variadic, coercion, NaN propagation, divide/mod by zero, arity errors). - `EvaluatorTest` adds two integration tests through dispatch: `var * 2 == 6` and the divide-by-zero → null → falsy flow. Co-authored-by: Cursor <cursoragent@cursor.com>
…nto pallares/json-logic-arithmetic-operators
Follow-up to the merge of `pallares/json-logic-evaluator`. The base branch routes engine warnings through the module-level `Rules.logger` now (commit 162574d), so threading a `logger: RulesEngineLogger` through every operator call is no longer needed. `ArithmeticOperators.opAdd / opMul / opSub / opDiv / opMod` lose the trailing `logger` parameter and the corresponding `Operators.evalArgs(args, vars, logger)` / `Operators.evalTwo(args, vars, logger, opName)` calls collapse onto the simpler post-merge signatures. The `RulesEngineLogger` import goes with them. Drive-by from the merge auto-resolution: drop the now-stale `, logger` references the auto-merge left in `Operators.dispatch` for the arithmetic cases (`+`, `-`, `*`, `/`, `%`). Test side: `ArithmeticOperatorsTest.run` helper drops its `PrintlnLogger` argument and the `RulesEngineLogger` typealias on the function-reference parameter. The arithmetic operators have a 1:1 parity with the iOS counterpart PR (commit `f2d7e084b`) — same operator set, same NaN-poison + `Null`-on-zero-divisor semantics, same test coverage — so no new behavior tests are added by this commit. Verified: `:rules-engine:testDefaultsDebugUnitTest`, `detektAll`, and `scripts/check-rules-engine-internal-only.sh` all green. Co-authored-by: Cursor <cursoragent@cursor.com>
…nto pallares/json-logic-arithmetic-operators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves SDK-4330
Motivation
Extends the JSON Logic operator set with arithmetic so predicates can express counter and sum conditions before the comparison and string operators land.
Summary
+,-,*,/,%per the JSON Logic spec, including variadic+/*, the 1-arg numeric-cast form of+, and unary negation via-.Value.FloatValue(Double)for consistency with the JS reference.looseEq/strictEqalready bridgeIntValue(n) ↔ FloatValue(n.0), so existing comparisons keep working.ObjectValue,ArrayValue, unparseable strings) coerce toDouble.NaNand propagate through arithmetic — the result isFloatValue(NaN), falsy underisTruthy.Value.Null(deliberate deviation from JS'sInfinity/NaN— friendlier for rule authors and matches the engine's "missing value" convention). Documented in the type's KDoc.Tests
ArithmeticOperatorsTestcovers each operator (basic, variadic, coercion, NaN propagation, divide/mod by zero, arity errors).EvaluatorTestadds two integration tests through dispatch:var * 2 == 6and the divide-by-zero → null → falsy flow.Notes
Stacked on top of #3482. Re-target to
mainonce that lands.Made with Cursor