Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Support for user rules #12

Merged
merged 2 commits into from
Feb 4, 2015
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
6 changes: 6 additions & 0 deletions src/main/java/com/launchdarkly/client/FeatureRep.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public E evaluate(LDUser user) {
return null;
}
else {
for (Variation<E> variation: variations) {
if (variation.matchUser(user)) {
return variation.value;
}
}

for (Variation<E> variation : variations) {
if (variation.matchTarget(user)) {
return variation.value;
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/launchdarkly/client/Variation.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class Variation<E> {
E value;
int weight;
TargetRule userTarget;
List<TargetRule> targets;
private final Logger logger = LoggerFactory.getLogger(Variation.class);

Expand All @@ -22,11 +23,25 @@ public Variation() {
Variation(Builder<E> b) {
this.value = b.value;
this.weight = b.weight;
this.userTarget = b.userTarget;
this.targets = new ArrayList<TargetRule>(b.targets);
}

public boolean matchUser(LDUser user) {
// If a userTarget rule is present, apply it
if (userTarget != null && userTarget.matchTarget(user)) {
return true;
}
return false;
}

public boolean matchTarget(LDUser user) {
for (TargetRule target: targets) {
// If a userTarget rule is present, nested "key" rules
// are deprecated and should be ignored
if (userTarget != null && target.attribute == "key") {
continue;
}
if (target.matchTarget(user)) {
return true;
}
Expand All @@ -37,14 +52,21 @@ public boolean matchTarget(LDUser user) {
static class Builder<E> {
E value;
int weight;
TargetRule userTarget;
List<TargetRule> targets;

Builder(E value, int weight) {
this.value = value;
this.weight = weight;
this.userTarget = new TargetRule("key", "in", new ArrayList<String>());
targets = new ArrayList<TargetRule>();
}

Builder<E> userTarget(TargetRule rule) {
this.userTarget = rule;
return this;
}

Builder<E> target(TargetRule rule) {
targets.add(rule);
return this;
Expand Down Expand Up @@ -90,6 +112,31 @@ else if (attribute.equals("country")) {
uValue = user.getCountry().getAlpha2();
}
}
else if (attribute.equals("email")) {
if (user.getEmail() != null) {
uValue = user.getEmail();
}
}
else if (attribute.equals("firstName")) {
if (user.getFirstName() != null ) {
uValue = user.getFirstName();
}
}
else if (attribute.equals("lastName")) {
if (user.getLastName() != null) {
uValue = user.getLastName();
}
}
else if (attribute.equals("avatar")) {
if (user.getAvatar() != null) {
uValue = user.getAvatar();
}
}
else if (attribute.equals("name")) {
if (user.getName() != null) {
uValue = user.getName();
}
}
else { // Custom attribute
JsonElement custom = user.getCustom(attribute);

Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/launchdarkly/client/FeatureRepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ public class FeatureRepTest {
.variation(falseVariation)
.build();

private Variation<Boolean> userRuleVariation = new Variation.Builder<Boolean>(false, 20)
.userTarget(targetUserOn)
.build();

private final FeatureRep<Boolean> userRuleFlag = new FeatureRep.Builder<Boolean>("User rule flag", "user.rule.flag")
.on(true)
.salt("feefifofum")
.variation(trueVariation)
.variation(userRuleVariation)
.build();

@Test
public void testUserRuleFlagForTargetUserOff() {

// The trueVariation tries to enable this rule, but the userVariation (with false value) has a userRule
// that's able to override this. This doesn't represent a real LD response-- we'd never have feature reps
// that sometimes contain user rules and sometimes contain embedded 'key' rules
LDUser user = new LDUser.Builder("targetOn@test.com").build();
Boolean b = userRuleFlag.evaluate(user);

assertEquals(false, b);
}

@Test
public void testFlagForTargetedUserOff() {
LDUser user = new LDUser.Builder("targetOff@test.com").build();
Expand Down