Repository navigation
feat(flagd-core): add terminal resolve operator #486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0488f91
413fe1d
d0e76f0
17f6ba9
236f533
9d0d3ce
ede2045
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| +1 −1 | .release-please-manifest.json | |
| +15 −0 | CHANGELOG.md | |
| +1 −1 | README.md | |
| +6 −1 | agents.md | |
| +2 −2 | docker-compose.yaml | |
| +24 −0 | evaluator/flags/testkit-flags.json | |
| +38 −0 | evaluator/gherkin/precision.feature | |
| +1 −1 | flagd/Dockerfile | |
| +127 −0 | flags/custom-ops.json | |
| +28 −0 | flags/precision-flags.json | |
| +660 −0 | flags/resolve-flags.json | |
| +29 −0 | gherkin/evaluation.feature | |
| +295 −0 | gherkin/resolve.feature | |
| +71 −0 | gherkin/targeting.feature | |
| +2 −1 | launchpad/configs/default.json | |
| +2 −1 | launchpad/configs/metadata.json | |
| +9 −0 | launchpad/configs/resolve.json | |
| +2 −1 | launchpad/configs/ssl.json | |
| +2 −1 | launchpad/configs/sync-payload.json | |
| +14 −1 | launchpad/pkg/json.go | |
| +4 −0 | launchpad/pkg/json_test.go | |
| +1 −1 | version.txt |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,20 @@ | ||
| import pytest | ||
|
|
||
| from tests.e2e.step.config_steps import * # noqa: F403 | ||
| from tests.e2e.step.context_steps import * # noqa: F403 | ||
| from tests.e2e.step.event_steps import * # noqa: F403 | ||
| from tests.e2e.step.flag_step import * # noqa: F403 | ||
| from tests.e2e.step.provider_steps import * # noqa: F403 | ||
|
|
||
| # Python's JsonLogic raises ZeroDivisionError for x/0, where the other cores compute | ||
| # Infinity or NaN, so the flag never reaches resolve's non-finite metadata check | ||
| SKIPPED_SCENARIOS = { | ||
| "Resolve with a non-finite computed number in metadata falls back to the default variant", | ||
| } | ||
|
|
||
|
|
||
| def pytest_collection_modifyitems(config, items): | ||
| for item in items: | ||
| scenario = getattr(getattr(item, "obj", None), "__scenario__", None) | ||
| if scenario is not None and scenario.name in SKIPPED_SCENARIOS: | ||
| item.add_marker(pytest.mark.skip(reason="JsonLogic raises on x/0")) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import logging | ||
| import math | ||
| import typing | ||
| from collections.abc import Sequence | ||
| from dataclasses import dataclass | ||
| from collections.abc import Mapping, Sequence | ||
| from dataclasses import dataclass, field | ||
|
|
||
| import cbor2 | ||
| import mmh3 | ||
|
|
@@ -17,10 +18,25 @@ | |
|
|
||
| @dataclass | ||
| class Fraction: | ||
| variant: str | float | int | bool | None | ||
| # whatever the variant expression evaluated to, the resolver decides what to do with it | ||
| variant: typing.Any | ||
| weight: int = 1 | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ResolveResult: | ||
| """Result of the terminal ``resolve`` operator. | ||
|
|
||
| No JSON input decodes to this type, so evaluation context data can't forge one. | ||
| """ | ||
|
|
||
| variant: str | ||
| # None keeps TARGETING_MATCH | ||
| reason: str | None = None | ||
| # merged over flag set and flag metadata | ||
| metadata: Mapping[str, str | bool | int | float] = field(default_factory=dict) | ||
|
|
||
|
|
||
| def _resolve_bucket_by(data: dict, args: tuple) -> tuple[typing.Any, tuple]: | ||
| if not isinstance(args[0], (list, tuple)): | ||
| return args[0], args[1:] | ||
|
|
@@ -54,7 +70,7 @@ def normalize_numbers(data: typing.Any) -> typing.Any: | |
| return data | ||
|
|
||
|
|
||
| def fractional(data: dict, *args: typing.Any) -> str | float | int | bool | None: | ||
| def fractional(data: dict, *args: typing.Any) -> typing.Any: | ||
| if not args: | ||
| logger.error("No arguments provided to fractional operator.") | ||
| return None | ||
|
|
@@ -216,3 +232,70 @@ def normalize_version(arg: typing.Any) -> semver.Version: | |
| version = numeric_part + ".0" * (2 - dot_count) + version[len(numeric_part) :] | ||
|
|
||
| return semver.Version.parse(version) | ||
|
|
||
|
|
||
| def resolve(data: dict, *args: typing.Any) -> ResolveResult | None: | ||
| """Terminal ``resolve`` operator: ``["variant", "reason"?, [["key", value], ...]?]``. | ||
|
|
||
| Placement is enforced by the targeting schema. Malformed (computed) arguments return | ||
| None like the other custom operators, so the flag falls back to its default variant. | ||
| """ | ||
| if not 1 <= len(args) <= 3: | ||
| logger.debug("resolve takes 1 to 3 arguments") | ||
| return None | ||
|
|
||
| variant = _resolve_variant_key(args[0]) | ||
| if variant is None: | ||
| logger.debug("resolve variant must be a string, boolean, or finite number") | ||
| return None | ||
|
|
||
| reason = args[1] if len(args) > 1 else None | ||
| if reason is not None and not isinstance(reason, str): | ||
| logger.debug("resolve reason must be a string or null") | ||
| return None | ||
|
|
||
| pairs = args[2] if len(args) > 2 else None | ||
| metadata = {} if pairs is None else _resolve_metadata(pairs) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reject a present metadata argument that evaluates to null. For Use an empty dictionary only when the third argument is absent. Otherwise, call
|
||
| if metadata is None: | ||
| return None | ||
|
|
||
| return ResolveResult(variant, reason, metadata) | ||
|
|
||
|
|
||
| def _resolve_variant_key(value: typing.Any) -> str | None: | ||
| # coerce like a plain targeting result | ||
| if isinstance(value, bool): | ||
| return str(value).lower() | ||
| if isinstance(value, str): | ||
| return value | ||
| if isinstance(value, int) or (isinstance(value, float) and math.isfinite(value)): | ||
| return str(value) | ||
| return None | ||
|
|
||
|
|
||
| def _resolve_metadata( | ||
| pairs: typing.Any, | ||
| ) -> dict[str, str | bool | int | float] | None: | ||
| if not isinstance(pairs, (list, tuple)): | ||
| logger.debug("resolve metadata must be an array of [key, value] pairs") | ||
| return None | ||
| metadata: dict[str, str | bool | int | float] = {} | ||
| for pair in pairs: | ||
| if ( | ||
| not isinstance(pair, (list, tuple)) | ||
| or len(pair) != 2 | ||
| or not isinstance(pair[0], str) | ||
| ): | ||
| logger.debug("resolve metadata pair must be a [string, value] array") | ||
| return None | ||
| key, value = pair | ||
| # NaN and Infinity (e.g. a computed 1/0) can't be encoded as JSON | ||
| if not isinstance(value, (str, bool, int)) and not ( | ||
| isinstance(value, float) and math.isfinite(value) | ||
| ): | ||
| logger.debug( | ||
| "resolve metadata value must be a string, finite number, or boolean" | ||
| ) | ||
| return None | ||
| metadata[key] = value | ||
| return metadata | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| from .custom_ops import ( | ||
| ends_with, | ||
| fractional, | ||
| resolve, | ||
| sem_ver, | ||
| starts_with, | ||
| ) | ||
|
|
@@ -22,6 +23,7 @@ | |
| "starts_with": starts_with, | ||
| "ends_with": ends_with, | ||
| "sem_ver": sem_ver, | ||
| "resolve": resolve, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate terminal placement before accepting flags. The load path does not enforce the placement claimed by Add placement validation after shared-evaluator expansion and before store replacement. Reject condition and other nonterminal uses. Preserve whole-rule, result-branch and fractional-bucket uses, and do not interpret literal data as executable rules.
|
||
| } | ||
|
|
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd strongly recommend dropping this branch and
RESOLVEinTestProviderType(L36). I've asked open-feature/flagd-testbed#448 to dropa resolve flagd provider(comment);resolvescenarios should use thestableprovider like the other custom ops.