-
Notifications
You must be signed in to change notification settings - Fork 25
feat: Add get_feature_flag_result, get_feature_flag_result! API
#82
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d6f6146
chore: "explicitely" -> "explicitly"
dustinbyrne d21c2cb
feat: Add `get_feature_flag_result`, `get_feature_flag_result!` API
dustinbyrne 2a3e23a
refactor: Rename files
rafaeelaudibert 76a6adf
docs: Add new FF method to README
rafaeelaudibert 39a304d
chore: Add changesets
rafaeelaudibert b37ae8c
fix credo
rafaeelaudibert 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| defmodule PostHog.FeatureFlagResult do | ||
| @moduledoc """ | ||
| Represents the result of a feature flag evaluation. | ||
|
|
||
| This struct contains all the information returned when evaluating a feature flag: | ||
| - `key` - The name of the feature flag | ||
| - `enabled` - Whether the flag is enabled for this user | ||
| - `variant` - The variant assigned to this user (nil for boolean flags) | ||
| - `payload` - The JSON payload configured for this flag/variant (nil if not set) | ||
|
|
||
| ## Examples | ||
|
|
||
| # Boolean flag result | ||
| %PostHog.FeatureFlagResult{ | ||
| key: "my-feature", | ||
| enabled: true, | ||
| variant: nil, | ||
| payload: nil | ||
| } | ||
|
|
||
| # Multivariant flag result with payload | ||
| %PostHog.FeatureFlagResult{ | ||
| key: "my-experiment", | ||
| enabled: true, | ||
| variant: "control", | ||
| payload: %{"button_color" => "blue"} | ||
| } | ||
| """ | ||
|
|
||
| @type t :: %__MODULE__{ | ||
| key: String.t(), | ||
| enabled: boolean(), | ||
| variant: String.t() | nil, | ||
| payload: any() | ||
| } | ||
|
|
||
| defstruct key: nil, enabled: false, variant: nil, payload: nil | ||
|
|
||
| @doc """ | ||
| Returns the value of the feature flag result. | ||
|
|
||
| If a variant is present, returns the variant string. Otherwise, returns the | ||
| enabled boolean status. This provides backwards compatibility with existing | ||
| code that expects a simple value from feature flag checks. | ||
|
|
||
| ## Examples | ||
|
|
||
| iex> result = %PostHog.FeatureFlagResult{key: "flag", enabled: true, variant: "control", payload: nil} | ||
| iex> PostHog.FeatureFlagResult.value(result) | ||
| "control" | ||
|
|
||
| iex> result = %PostHog.FeatureFlagResult{key: "flag", enabled: true, variant: nil, payload: nil} | ||
| iex> PostHog.FeatureFlagResult.value(result) | ||
| true | ||
|
|
||
| iex> result = %PostHog.FeatureFlagResult{key: "flag", enabled: false, variant: nil, payload: nil} | ||
| iex> PostHog.FeatureFlagResult.value(result) | ||
| false | ||
| """ | ||
| @spec value(t()) :: boolean() | String.t() | ||
| def value(%__MODULE__{variant: variant}) when not is_nil(variant), do: variant | ||
| def value(%__MODULE__{enabled: enabled}), do: enabled | ||
| end | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| defmodule PostHog.FeatureFlagResultTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| alias PostHog.FeatureFlagResult | ||
|
|
||
| describe "struct" do | ||
| test "creates struct with all fields" do | ||
| result = %FeatureFlagResult{ | ||
| key: "my-flag", | ||
| enabled: true, | ||
| variant: "control", | ||
| payload: %{"key" => "value"} | ||
| } | ||
|
|
||
| assert result.key == "my-flag" | ||
| assert result.enabled == true | ||
| assert result.variant == "control" | ||
| assert result.payload == %{"key" => "value"} | ||
| end | ||
|
|
||
| test "creates struct with nil defaults" do | ||
| result = %FeatureFlagResult{key: "my-flag", enabled: false} | ||
|
|
||
| assert result.key == "my-flag" | ||
| assert result.enabled == false | ||
| assert result.variant == nil | ||
| assert result.payload == nil | ||
| end | ||
| end | ||
|
|
||
| describe "value/1" do | ||
| test "returns variant when present" do | ||
| result = %FeatureFlagResult{ | ||
| key: "my-flag", | ||
| enabled: true, | ||
| variant: "control", | ||
| payload: nil | ||
| } | ||
|
|
||
| assert FeatureFlagResult.value(result) == "control" | ||
| end | ||
|
|
||
| test "returns variant even when empty string" do | ||
| result = %FeatureFlagResult{ | ||
| key: "my-flag", | ||
| enabled: true, | ||
| variant: "", | ||
| payload: nil | ||
| } | ||
|
|
||
| # Empty string is still a variant, but per implementation nil check | ||
| # empty string is not nil, so it returns the variant | ||
| assert FeatureFlagResult.value(result) == "" | ||
| end | ||
|
|
||
| test "returns true when enabled and no variant" do | ||
| result = %FeatureFlagResult{ | ||
| key: "my-flag", | ||
| enabled: true, | ||
| variant: nil, | ||
| payload: nil | ||
| } | ||
|
|
||
| assert FeatureFlagResult.value(result) == true | ||
| end | ||
|
|
||
| test "returns false when not enabled and no variant" do | ||
| result = %FeatureFlagResult{ | ||
| key: "my-flag", | ||
| enabled: false, | ||
| variant: nil, | ||
| payload: nil | ||
| } | ||
|
|
||
| assert FeatureFlagResult.value(result) == false | ||
| end | ||
|
|
||
| test "variant takes precedence over enabled status" do | ||
| # Edge case: variant present but enabled is false | ||
| result = %FeatureFlagResult{ | ||
| key: "my-flag", | ||
| enabled: false, | ||
| variant: "test-variant", | ||
| payload: nil | ||
| } | ||
|
|
||
| assert FeatureFlagResult.value(result) == "test-variant" | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
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.