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

Commit 62ed09c

Browse files
committed
Merge pull request #12 from launchdarkly/jko/user-rules
Support for user rules
2 parents c296ac7 + 369cbe5 commit 62ed09c

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

src/main/java/com/launchdarkly/client/FeatureRep.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public E evaluate(LDUser user) {
6363
return null;
6464
}
6565
else {
66+
for (Variation<E> variation: variations) {
67+
if (variation.matchUser(user)) {
68+
return variation.value;
69+
}
70+
}
71+
6672
for (Variation<E> variation : variations) {
6773
if (variation.matchTarget(user)) {
6874
return variation.value;

src/main/java/com/launchdarkly/client/Variation.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
class Variation<E> {
1313
E value;
1414
int weight;
15+
TargetRule userTarget;
1516
List<TargetRule> targets;
1617
private final Logger logger = LoggerFactory.getLogger(Variation.class);
1718

@@ -22,11 +23,25 @@ public Variation() {
2223
Variation(Builder<E> b) {
2324
this.value = b.value;
2425
this.weight = b.weight;
26+
this.userTarget = b.userTarget;
2527
this.targets = new ArrayList<TargetRule>(b.targets);
2628
}
2729

30+
public boolean matchUser(LDUser user) {
31+
// If a userTarget rule is present, apply it
32+
if (userTarget != null && userTarget.matchTarget(user)) {
33+
return true;
34+
}
35+
return false;
36+
}
37+
2838
public boolean matchTarget(LDUser user) {
2939
for (TargetRule target: targets) {
40+
// If a userTarget rule is present, nested "key" rules
41+
// are deprecated and should be ignored
42+
if (userTarget != null && target.attribute == "key") {
43+
continue;
44+
}
3045
if (target.matchTarget(user)) {
3146
return true;
3247
}
@@ -37,14 +52,21 @@ public boolean matchTarget(LDUser user) {
3752
static class Builder<E> {
3853
E value;
3954
int weight;
55+
TargetRule userTarget;
4056
List<TargetRule> targets;
4157

4258
Builder(E value, int weight) {
4359
this.value = value;
4460
this.weight = weight;
61+
this.userTarget = new TargetRule("key", "in", new ArrayList<String>());
4562
targets = new ArrayList<TargetRule>();
4663
}
4764

65+
Builder<E> userTarget(TargetRule rule) {
66+
this.userTarget = rule;
67+
return this;
68+
}
69+
4870
Builder<E> target(TargetRule rule) {
4971
targets.add(rule);
5072
return this;
@@ -90,6 +112,31 @@ else if (attribute.equals("country")) {
90112
uValue = user.getCountry().getAlpha2();
91113
}
92114
}
115+
else if (attribute.equals("email")) {
116+
if (user.getEmail() != null) {
117+
uValue = user.getEmail();
118+
}
119+
}
120+
else if (attribute.equals("firstName")) {
121+
if (user.getFirstName() != null ) {
122+
uValue = user.getFirstName();
123+
}
124+
}
125+
else if (attribute.equals("lastName")) {
126+
if (user.getLastName() != null) {
127+
uValue = user.getLastName();
128+
}
129+
}
130+
else if (attribute.equals("avatar")) {
131+
if (user.getAvatar() != null) {
132+
uValue = user.getAvatar();
133+
}
134+
}
135+
else if (attribute.equals("name")) {
136+
if (user.getName() != null) {
137+
uValue = user.getName();
138+
}
139+
}
93140
else { // Custom attribute
94141
JsonElement custom = user.getCustom(attribute);
95142

src/test/java/com/launchdarkly/client/FeatureRepTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ public class FeatureRepTest {
4040
.variation(falseVariation)
4141
.build();
4242

43+
private Variation<Boolean> userRuleVariation = new Variation.Builder<Boolean>(false, 20)
44+
.userTarget(targetUserOn)
45+
.build();
46+
47+
private final FeatureRep<Boolean> userRuleFlag = new FeatureRep.Builder<Boolean>("User rule flag", "user.rule.flag")
48+
.on(true)
49+
.salt("feefifofum")
50+
.variation(trueVariation)
51+
.variation(userRuleVariation)
52+
.build();
53+
54+
@Test
55+
public void testUserRuleFlagForTargetUserOff() {
56+
57+
// The trueVariation tries to enable this rule, but the userVariation (with false value) has a userRule
58+
// that's able to override this. This doesn't represent a real LD response-- we'd never have feature reps
59+
// that sometimes contain user rules and sometimes contain embedded 'key' rules
60+
LDUser user = new LDUser.Builder("targetOn@test.com").build();
61+
Boolean b = userRuleFlag.evaluate(user);
62+
63+
assertEquals(false, b);
64+
}
65+
4366
@Test
4467
public void testFlagForTargetedUserOff() {
4568
LDUser user = new LDUser.Builder("targetOff@test.com").build();

0 commit comments

Comments
 (0)