Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Merged
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
14 changes: 13 additions & 1 deletion src/main/java/com/launchdarkly/client/LDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,25 @@ private void sendFlagRequestEvent(String featureKey, LDUser user, boolean value)
/**
* Calculates the value of a feature flag for a given user.
*
*
* @param featureKey the unique featureKey for the feature flag
* @param user the end user requesting the flag
* @param defaultValue the default value of the flag
* @return whether or not the flag should be enabled, or {@code defaultValue} if the flag is disabled in the LaunchDarkly control panel
* @deprecated As of version 0.7.0, renamed to {@link #toggle(String, LDUser, boolean)}
*/
public boolean getFlag(String featureKey, LDUser user, boolean defaultValue) {
return toggle(featureKey, user, defaultValue);
}

/**
* Calculates the value of a feature flag for a given user.
*
* @param featureKey the unique featureKey for the feature flag
* @param user the end user requesting the flag
* @param defaultValue the default value of the flag
* @return whether or not the flag should be enabled, or {@code defaultValue} if the flag is disabled in the LaunchDarkly control panel
*/
public boolean toggle(String featureKey, LDUser user, boolean defaultValue) {
if (this.offline) {
return defaultValue;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/launchdarkly/client/LDUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class LDUser {
private String avatar;
private String firstName;
private String lastName;
private boolean anonymous;

private LDCountryCode country;
private Map<String, JsonElement> custom;
Expand All @@ -52,6 +53,7 @@ protected LDUser(Builder builder) {
this.email = builder.email;
this.name = builder.name;
this.avatar = builder.avatar;
this.anonymous = builder.anonymous;
this.custom = new HashMap<String, JsonElement>(builder.custom);
}

Expand Down Expand Up @@ -84,6 +86,8 @@ String getKey() {

String getAvatar() { return avatar; }

boolean getAnonymous() { return anonymous; }

JsonElement getCustom(String key) {
return custom.get(key);
}
Expand All @@ -110,6 +114,7 @@ public static class Builder {
private String email;
private String name;
private String avatar;
private boolean anonymous;
private LDCountryCode country;
private Map<String, JsonElement> custom;

Expand Down Expand Up @@ -193,6 +198,16 @@ public Builder firstName(String firstName) {
return this;
}

/**
* Sets whether this user is anonymous
* @param anonymous whether the user is anonymous
* @return the builder
*/
public Builder anonymous(boolean anonymous) {
this.anonymous = anonymous;
return this;
}

/**
* Sets the user's last name
* @param lastName the user's last name
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/launchdarkly/client/Variation.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static class Builder<E> {
Builder(E value, int weight) {
this.value = value;
this.weight = weight;
this.userTarget = new TargetRule("key", "in", new ArrayList<String>());
this.userTarget = new TargetRule("key", "in", new ArrayList<Object>());
targets = new ArrayList<TargetRule>();
}

Expand All @@ -81,22 +81,22 @@ Variation<E> build() {
static class TargetRule {
String attribute;
String operator;
List<String> values;
List<Object> values;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looks like a bug-- this wouldn't have worked with non-string target rule values


private final Logger logger = LoggerFactory.getLogger(TargetRule.class);

TargetRule(String attribute, String operator, List<String> values) {
TargetRule(String attribute, String operator, List<Object> values) {
this.attribute = attribute;
this.operator = operator;
this.values = new ArrayList<String>(values);
this.values = new ArrayList<Object>(values);
}

TargetRule(String attribute, List<String> values) {
TargetRule(String attribute, List<Object> values) {
this(attribute, "in", values);
}

public boolean matchTarget(LDUser user) {
String uValue = null;
Object uValue = null;
if (attribute.equals("key")) {
if (user.getKey() != null) {
uValue = user.getKey();
Expand Down Expand Up @@ -137,6 +137,9 @@ else if (attribute.equals("name")) {
uValue = user.getName();
}
}
else if (attribute.equals("anonymous")) {
uValue = user.getAnonymous();
}
else { // Custom attribute
JsonElement custom = user.getCustom(attribute);

Expand Down
19 changes: 15 additions & 4 deletions src/test/java/com/launchdarkly/client/FeatureRepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@

public class FeatureRepTest {

private final Variation.TargetRule targetUserOn = new Variation.TargetRule("key", Collections.singletonList("targetOn@test.com"));
private final Variation.TargetRule targetUserOn = new Variation.TargetRule("key", Collections.<Object>singletonList("targetOn@test.com"));

private final Variation.TargetRule targetGroupOn = new Variation.TargetRule("groups", Arrays.asList("google", "microsoft"));
private final Variation.TargetRule targetGroupOn = new Variation.TargetRule("groups", Arrays.<Object>asList("google", "microsoft"));

private final Variation.TargetRule targetUserOff = new Variation.TargetRule("key", Collections.singletonList("targetOff@test.com"));
private final Variation.TargetRule targetUserOff = new Variation.TargetRule("key", Collections.<Object>singletonList("targetOff@test.com"));

private final Variation.TargetRule targetGroupOff = new Variation.TargetRule("groups", Arrays.asList("oracle"));
private final Variation.TargetRule targetGroupOff = new Variation.TargetRule("groups", Arrays.<Object>asList("oracle"));

private final Variation.TargetRule targetAnonymousOn = new Variation.TargetRule("anonymous", Collections.<Object>singletonList(true));

private final Variation<Boolean> trueVariation = new Variation.Builder<Boolean>(true, 80)
.target(targetUserOn)
.target(targetGroupOn)
.target(targetAnonymousOn)
.build();

private final Variation<Boolean> falseVariation = new Variation.Builder<Boolean>(false, 20)
Expand Down Expand Up @@ -121,4 +124,12 @@ public void testFlagWithCustomAttributeWorksWithLDUserDefaultCtor() {
assertNotNull(b);
}

@Test
public void testFlagWithAnonymousOn() {
LDUser user = new LDUser.Builder("targetOff@test.com").anonymous(true).build();

Boolean b = simpleFlag.evaluate(user);
assertEquals(true, b);
}

}
1 change: 0 additions & 1 deletion src/test/java/com/launchdarkly/client/LDUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,4 @@ public void testLDUserJsonSerializationContainsCountryAsTwoDigitCode() {

assert(deserialized.getCountry().equals(LDCountryCode.US));
}

}