-
Notifications
You must be signed in to change notification settings - Fork 2
feat : java-sdk main implementation #53
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 1 commit
ff5a967
2bf5ea1
3c9da80
fd70397
8d7b073
dcf8479
d6c661b
d615f7a
6094fa3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package org.lightswitch.sdk.model; | ||
|
|
||
| import org.lightswitch.sdk.user.LightSwitchUser; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| public class FeatureFlag { | ||
|
|
||
| private final String key; | ||
| private final Object defaultValue; | ||
| private final Map<Map<String, String>, Object> filters; // filtering rules | ||
|
|
||
| public FeatureFlag(String key, Object defaultValue, Map<Map<String, String>, Object> filters) { | ||
| this.key = key; | ||
| this.defaultValue = defaultValue; | ||
|
Contributor
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. Doesn't defaultValue also require Null check? |
||
| this.filters = filters != null ? filters : Collections.emptyMap(); | ||
| } | ||
|
|
||
| public String getKey() { | ||
| return key; | ||
| } | ||
|
|
||
| public Object getDefaultValue() { | ||
| return defaultValue; | ||
| } | ||
|
|
||
| public Map<Map<String, String>, Object> getConditions() { | ||
| return Collections.unmodifiableMap(filters); | ||
| } | ||
|
|
||
| /** | ||
| * Find and return appropriate flag values based on user properties | ||
| */ | ||
| public Object evaluate(LightSwitchUser user) { | ||
| for (Map.Entry<Map<String, String>, Object> entry : filters.entrySet()) { | ||
| Map<String, String> conditions = entry.getKey(); | ||
| boolean match = conditions.entrySet().stream() | ||
| .allMatch(cond -> user.getAttributes().getOrDefault(cond.getKey(), "").equals(cond.getValue())); | ||
|
|
||
| if (match) { | ||
| return entry.getValue(); | ||
| } | ||
| } | ||
| return defaultValue; | ||
|
Comment on lines
+36
to
+45
Contributor
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. If we simplify the logic using Streams, it might be more readable than using a for loop and manually iterating. |
||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "FeatureFlag{" + | ||
| "key='" + key + '\'' + | ||
| ", defaultValue=" + defaultValue + | ||
| ", filters=" + filters + | ||
| '}'; | ||
| } | ||
| } | ||
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.
Now that the
filtershave been added to flag, it seems like similar modifications should be needed in the backend entity and API spec as well. It would be good to review and align everything before moving forward.