Skip to content

feat(fractional): support nested JSON Logic expressions as bucket variant names#31

Merged
aepfli merged 1 commit intomainfrom
feat/nested-fractional
Mar 16, 2026
Merged

feat(fractional): support nested JSON Logic expressions as bucket variant names#31
aepfli merged 1 commit intomainfrom
feat/nested-fractional

Conversation

@aepfli
Copy link
Copy Markdown
Member

@aepfli aepfli commented Mar 16, 2026

Summary

Implements flagd#1676 for the Rust core, mirroring the Java work in java-sdk-contrib#1737.

Allow arbitrary JSON Logic expressions as the variant name inside a fractional bucket definition:

{"fractional": [
  {"var": "email"},
  [{"if": [{"in": [{"var": "locale"}, ["us", "ca"]]}, "red", "grey"]}, 50],
  ["blue", 50]
]}

Nested fractional inside fractional is also supported:

{"fractional": [
  {"var": "email"},
  [{"fractional": [{"var": "tier"}, ["red", 50], ["blue", 50]]}, 50],
  [{"fractional": [{"var": "tier"}, ["green", 50], ["yellow", 50]]}, 50]
]}

Investigation

The operator already recurses into array elements via evaluator.evaluate(), so nested expressions worked at runtime. The only blocker was the JSON Schema enforcing "type": "string" on bucket variant names in strict mode, preventing configs from loading at all.

Changes

schemas/targeting.json

fractionalWeightArg first element changed from {"type": "string"} to {"oneOf": [{"type": "string"}, {"\": "#/definitions/anyRule"}]}, accepting any JSON Logic expression as a variant name.

src/operators/mod.rs

mod fractionalpub mod fractional to expose fractional::fractional for direct use in integration tests.

tests/integration_tests.rs

Five new tests:

  • test_nested_if_in_bucket_variant_name{"if": ...} resolves differently per locale context
  • test_nested_fractional_inside_fractional — nested {"fractional": ...} inside outer buckets
  • test_nested_var_in_bucket_variant_name{"var": "preferred_theme"} pulled from context
  • test_fractional_distribution_uniformity — statistical uniformity across the hash space (mirrors java-sdk-contrib#1740)
  • test_fractional_boundary_hashes_do_not_panic — boundary keys don't panic (mirrors Java PR #1740)

Closes #30

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces significant enhancements to the fractional targeting mechanism by enabling the use of nested JSON Logic expressions as bucket variant names. This change allows for highly dynamic and context-aware feature flag evaluations, aligning the Rust core with capabilities already present in other SDKs. The update primarily involves a schema modification to permit these expressions and is thoroughly validated with new integration tests covering various complex scenarios, including nested if and fractional rules, context variable usage, and statistical distribution integrity.

Highlights

  • Schema Update for Fractional Targeting: The schemas/targeting.json file was updated to allow JSON Logic expressions as variant names within fractional bucket definitions, enabling more dynamic and context-aware targeting configurations.
  • Module Visibility Adjustment: The fractional module in src/operators/mod.rs was made public to facilitate direct testing and integration.
  • Comprehensive Integration Testing: Five new integration tests were added to validate nested JSON Logic expressions, ensure statistical uniformity of fractional distribution, and confirm robust handling of boundary hash values, mirroring work in other SDKs.
Changelog
  • schemas/targeting.json
    • Updated the fractionalWeightArg definition to accept either a string or any JSON Logic rule (#/definitions/anyRule) for the variant name, thereby supporting dynamic variant resolution.
  • src/operators/mod.rs
    • Changed the mod fractional; declaration to pub mod fractional; to expose the module for direct use in integration tests.
  • tests/integration_tests.rs
    • Added test_nested_if_in_bucket_variant_name to verify that if expressions in variant names resolve correctly based on context.
    • Included test_nested_fractional_inside_fractional to confirm proper evaluation of nested fractional rules.
    • Implemented test_nested_var_in_bucket_variant_name to ensure context variables can be used as variant names.
    • Added test_fractional_distribution_uniformity to statistically validate the even distribution of fractional assignments.
    • Introduced test_fractional_boundary_hashes_do_not_panic to confirm robust handling of edge-case hash values without panics.
Activity
  • No specific activity (comments, reviews, or progress updates) has been recorded for this pull request beyond its initial creation and description.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly implements support for nested JSON Logic expressions as variant names in fractional bucketing. The schema change is appropriate, and the new integration tests provide good coverage for various nesting scenarios, including if, var, and nested fractional operators. The addition of statistical and boundary tests is also a great improvement. I have one suggestion to improve the performance of a new statistical test to prevent potential CI timeouts.

…iant names

The operator already recurses into array elements via evaluator.evaluate(),
so nested expressions (if, var, fractional) in bucket variant positions
worked at runtime. The only blocker was the JSON Schema enforcing
"type": "string" for fractionalWeightArg[0].

Change fractionalWeightArg first element to oneOf[string, anyRule] so
the schema accepts JSON Logic objects as variant names in strict mode.

Also:
- pub mod fractional (was private) to allow direct access in tests
- Add integration tests for nested if, nested fractional, and var
  as bucket variant names (closes #30)
- Add statistical distribution uniformity test (mirrors Java PR #1740)
- Add boundary-key test (mirrors Java PR #1740 edgeCasesDoNotThrow)

Signed-off-by: Simon Schrottner <simon.schrottner@aepfli.at>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Simon Schrottner <simon.schrottner@dynatrace.com>
@aepfli aepfli force-pushed the feat/nested-fractional branch from 3a4789e to 09faeef Compare March 16, 2026 11:19
@aepfli aepfli merged commit 3263743 into main Mar 16, 2026
20 checks passed
@github-actions github-actions bot mentioned this pull request Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(fractional): support nested JSON Logic expressions as bucket variant names

1 participant