-
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 all commits
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
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package org.lightswitch.sdk.client; | ||
|
|
||
| import org.lightswitch.sdk.user.LightSwitchUser; | ||
|
|
||
| public interface LightSwitchClient { | ||
|
|
||
| boolean getBooleanFlag(String key, boolean defaultValue, LightSwitchUser user); | ||
|
|
||
| int getIntFlag(String key, int defaultValue, LightSwitchUser user); | ||
|
|
||
| String getStringFlag(String key, String defaultValue, LightSwitchUser user); | ||
|
|
||
| void destroy(); | ||
|
|
||
| boolean isEnabled(String key, LightSwitchUser user); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package org.lightswitch.sdk.client; | ||
|
|
||
| import org.lightswitch.sdk.exception.LightSwitchCastException; | ||
| import org.lightswitch.sdk.model.FeatureFlag; | ||
| import org.lightswitch.sdk.user.LightSwitchUser; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class LightSwitchClientImpl implements LightSwitchClient { | ||
|
|
||
| // TODO develop FeatureFlagRepository, httpClient, SSEClient | ||
| private final Map<String, FeatureFlag> featureFlags; // Mock | ||
|
|
||
| public LightSwitchClientImpl() { | ||
| // Mock data | ||
| this.featureFlags = new HashMap<>(); | ||
|
|
||
| featureFlags.put("new-dashboard", new FeatureFlag( | ||
| "new-dashboard", | ||
| false, // default | ||
| Map.of( | ||
| Map.of("region", "US"), true // active "US" user | ||
| ) | ||
| )); | ||
|
|
||
| featureFlags.put("beta-feature", new FeatureFlag( | ||
| "beta-feature", | ||
| false, | ||
| Map.of( | ||
| Map.of("plan", "premium"), true // active "premium" user | ||
| ) | ||
| )); | ||
|
|
||
| featureFlags.put("discount", new FeatureFlag( | ||
| "discount", | ||
| 0, // default sale rates : 0% | ||
| Map.of( | ||
| Map.of("region", "US"), 100, // US user 100% sale | ||
| Map.of("region", "EU"), 50 // EU user 50% sale | ||
| ) | ||
| )); | ||
| } | ||
|
|
||
| private FeatureFlag getMockFlag(String key) { | ||
| return featureFlags.get(key); | ||
| } | ||
|
|
||
| private <T> T castValue(String key, Object value, Class<T> expectedType) { | ||
| if (!expectedType.isInstance(value)) { | ||
| throw new LightSwitchCastException(key, value, expectedType); | ||
| } | ||
| return expectedType.cast(value); | ||
| } | ||
|
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. How about move all private methods to the bottom? |
||
|
|
||
| @Override | ||
| public boolean getBooleanFlag(String key, boolean defaultValue, LightSwitchUser user) { | ||
| FeatureFlag flag = getMockFlag(key); | ||
| if (flag == null) return defaultValue; | ||
|
|
||
| Object value = flag.evaluate(user); | ||
| return castValue(key, value, Boolean.class); | ||
|
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. Since 58~62 lines are duplicated to other methods too, it would be better to extract private method for these. |
||
| } | ||
|
|
||
| @Override | ||
| public int getIntFlag(String key, int defaultValue, LightSwitchUser user) { | ||
| FeatureFlag flag = getMockFlag(key); | ||
| if (flag == null) return defaultValue; | ||
|
|
||
| Object value = flag.evaluate(user); | ||
| return castValue(key, value, Integer.class); | ||
| } | ||
|
|
||
| @Override | ||
| public String getStringFlag(String key, String defaultValue, LightSwitchUser user) { | ||
| FeatureFlag flag = getMockFlag(key); | ||
| if (flag == null) return defaultValue; | ||
|
|
||
| Object value = flag.evaluate(user); | ||
| return castValue(key, value, String.class); | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() { | ||
| featureFlags.clear(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled(String key, LightSwitchUser user) { | ||
| return getBooleanFlag(key, false, user); | ||
| } | ||
| } | ||
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.
I'm curious about why the flag restricts the number type to
Int. It seems like there could be many cases where storing aLongwould be necessary.