-
Notifications
You must be signed in to change notification settings - Fork 39
feat(analytics): add Source discriminated union with Fallback.Reason (SDK-79) #89
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
Open
tylerjroach
wants to merge
4
commits into
master
Choose a base branch
from
fix/sdk-79-variant-source-fallback-reason
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cf73221
fix(flags): tag fallback_reason so OpenFeature can distinguish causes
tylerjroach 385fbe2
fix(flags): drop unused Reason.NOT_READY enum case
tylerjroach 4e4d08c
chore: drop verbose NOT_READY comment
tylerjroach a58a93d
fix(flags): address review feedback on Source / SelectedVariant
tylerjroach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
100 changes: 100 additions & 0 deletions
100
src/main/java/com/mixpanel/mixpanelapi/featureflags/model/Source.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package com.mixpanel.mixpanelapi.featureflags.model; | ||
|
|
||
| /** | ||
| * Where a {@link SelectedVariant} came from. | ||
| * | ||
| * <p>The flags providers tag every variant they return so callers — especially | ||
| * the OpenFeature wrapper — can distinguish a real evaluation from each of the | ||
| * distinct fallback paths. Discriminated union (abstract class + nested static | ||
| * finals) rather than a string so the per-case data (the {@link Fallback.Reason} | ||
| * tag) lives on the variant it describes; that way invalid states like | ||
| * "successful evaluation with a fallback reason" are unrepresentable.</p> | ||
| * | ||
| * <p>Java 8 source level, so no {@code sealed} keyword — the package-private | ||
| * constructor is what makes this closed. Construct via {@link #local()}, | ||
| * {@link #remote()}, or {@link #fallback(Fallback.Reason)}.</p> | ||
| */ | ||
| public abstract class Source { | ||
| Source() {} | ||
|
|
||
| /** Singleton {@link Local} — every call returns the same instance. */ | ||
| public static Local local() { | ||
| return Local.INSTANCE; | ||
| } | ||
|
|
||
| /** Singleton {@link Remote} — every call returns the same instance. */ | ||
| public static Remote remote() { | ||
| return Remote.INSTANCE; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a {@link Fallback} tagged with the given reason. | ||
| * | ||
| * <p>The SDK uses this to explain why a fallback was returned (flag missing, | ||
| * required context absent, no rollout matched, network error, not ready) so | ||
| * the OpenFeature wrapper can map to the correct user-facing error code | ||
| * instead of collapsing every fallback to FLAG_NOT_FOUND.</p> | ||
| */ | ||
| public static Fallback fallback(Fallback.Reason reason) { | ||
| return new Fallback(reason); | ||
| } | ||
|
|
||
| /** Variant produced by local rule evaluation against cached flag definitions. */ | ||
| public static final class Local extends Source { | ||
| // Held inside the subclass so the outer class's <clinit> does not reference it, | ||
| // sidestepping the "subclass referenced from superclass initializer" deadlock pattern. | ||
| static final Local INSTANCE = new Local(); | ||
|
|
||
| Local() {} | ||
|
|
||
| @Override public String toString() { return "Local"; } | ||
| } | ||
|
|
||
| /** Variant returned by a remote /flags evaluation call. */ | ||
| public static final class Remote extends Source { | ||
| static final Remote INSTANCE = new Remote(); | ||
|
|
||
| Remote() {} | ||
|
|
||
| @Override public String toString() { return "Remote"; } | ||
| } | ||
|
tylerjroach marked this conversation as resolved.
|
||
|
|
||
| /** Developer-supplied fallback returned because the SDK had no value to serve. */ | ||
| public static final class Fallback extends Source { | ||
| /** | ||
| * Why the SDK returned the developer fallback. Matches the set of reasons | ||
| * used by the other Mixpanel SDKs (mixpanel-php in particular). | ||
| */ | ||
| public enum Reason { | ||
| /** Flag key is not in the local definitions or the remote response. */ | ||
| FLAG_NOT_FOUND, | ||
| /** A property the flag's rollout is keyed on was absent from the evaluation context. */ | ||
| MISSING_CONTEXT_KEY, | ||
| /** Flag exists, but no rollout in its ruleset matched the supplied context. */ | ||
| NO_ROLLOUT_MATCH, | ||
| /** Remote evaluation failed (network error, HTTP error, parse error). */ | ||
| BACKEND_ERROR, | ||
| } | ||
|
tylerjroach marked this conversation as resolved.
|
||
|
|
||
| /** Reason the SDK returned this fallback. */ | ||
| public final Reason reason; | ||
|
|
||
| Fallback(Reason reason) { | ||
| this.reason = reason; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (!(o instanceof Fallback)) return false; | ||
| return reason == ((Fallback) o).reason; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return reason.hashCode(); | ||
| } | ||
|
|
||
| @Override public String toString() { return "Fallback(" + reason + ")"; } | ||
| } | ||
| } | ||
|
tylerjroach marked this conversation as resolved.
|
||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.