-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Constraints for Feature Flags
- Loading branch information
Showing
4 changed files
with
49 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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,35 @@ | ||
defmodule Unleash.Strategy.Constraint do | ||
@moduledoc """ | ||
Module that is used to verify | ||
[constraints](https://www.unleash-hosted.com/docs/strategy-constraints/) are | ||
met. | ||
These constraints allow for very complex and specifc strategies to be | ||
enacted by allowing users to specify context values to include or exclude. | ||
""" | ||
|
||
def verify_all(constraints, context) do | ||
Enum.all?(constraints, &verify(&1, context)) | ||
end | ||
|
||
defp verify(%{"contextName" => name, "operator" => op, "values" => values}, context) do | ||
context | ||
|> find_value(name) | ||
|> check(op, values) | ||
end | ||
|
||
defp verify(%{}, _context), do: false | ||
|
||
defp check(value, "IN", values), do: value in values | ||
defp check(value, "NOT_IN", values), do: value not in values | ||
|
||
defp find_value(nil, _name), do: nil | ||
|
||
defp find_value(ctx, name) do | ||
Map.get( | ||
ctx, | ||
String.to_atom(Recase.to_snake(name)), | ||
find_value(Map.get(ctx, :properties), name) | ||
) | ||
end | ||
end |
This file contains 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 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