Skip to content
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

feat: dynamic config struct #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/statsig.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ defmodule Statsig do
result = Statsig.Evaluator.eval(user, config, :config)
log_exposures(user, result.exposures, :config)

# TODO - could probably hand back a Result struct
case result do
%{reason: :not_found} -> {:error, :not_found}
# TODO - this be {:ok, result}
_ -> %{rule_id: Map.get(result.rule, "id"), value: result.value}
_ -> {:ok, Statsig.DynamicConfig.new(
config,
result.value,
Map.get(result.rule, "id"),
Map.get(result.rule, "groupName"),
Map.get(result.rule, "idType"),
result.exposures
)}
end
end

Expand Down
36 changes: 36 additions & 0 deletions lib/statsig/dynamic_config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Statsig.DynamicConfig do
@type secondary_exposure :: %{
gate: String.t(),
gateValue: String.t(),
ruleID: String.t()
}

@type t :: %__MODULE__{
name: String.t(),
value: map(),
rule_id: String.t(),
group_name: String.t() | nil,
id_type: String.t() | nil,
secondary_exposures: [secondary_exposure()]
}

defstruct [
:name,
:value,
:rule_id,
:group_name,
:id_type,
secondary_exposures: []
]

def new(config_name, value \\ %{}, rule_id \\ "", group_name \\ nil, id_type \\ nil, secondary_exposures \\ []) do
Copy link

Choose a reason for hiding this comment

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

there are too many default params here.

%__MODULE__{
name: config_name || "",
value: value || %{},
rule_id: rule_id,
group_name: group_name,
id_type: id_type,
secondary_exposures: secondary_exposures || []
}
end
end
2 changes: 1 addition & 1 deletion lib/statsig/evaluator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ defmodule Statsig.Evaluator do
result
| result: final_result,
value: Map.get(rule, "returnValue"),
reason: :rule_match
reason: :rule_match,
},
fn r, final ->
%Result{final | exposures: r.exposures ++ final.exposures}
Expand Down