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

Commit 0f7dcf2

Browse files
committed
Merge pull request #29 from launchdarkly/jko/no-args-ctor
Better support for non-string custom values
2 parents b2bfa6c + 45e664b commit 0f7dcf2

File tree

7 files changed

+94
-81
lines changed

7 files changed

+94
-81
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repositories {
1111

1212
allprojects {
1313
group = 'com.launchdarkly'
14-
version = "0.16.0-SNAPSHOT"
14+
version = "0.17.0-SNAPSHOT"
1515
sourceCompatibility = 1.6
1616
targetCompatibility = 1.6
1717
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private Float paramForUser(LDUser user) {
7878
String hash;
7979

8080
if (user.getKey() != null) {
81-
idHash = user.getKey();
81+
idHash = user.getKey().getAsString();
8282
}
8383
else {
8484
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
class IdentifyEvent extends Event {
66

77
IdentifyEvent(LDUser user) {
8-
super("identify", user.getKey(), user);
8+
super("identify", user.getKey().getAsString(), user);
99
}
1010
}

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

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
* launch a feature to the top 10% of users on a site.
2525
*/
2626
public class LDUser {
27-
private String key;
28-
private String secondary;
29-
private String ip;
30-
private String email;
31-
private String name;
32-
private String avatar;
33-
private String firstName;
34-
private String lastName;
35-
private Boolean anonymous;
36-
37-
private LDCountryCode country;
27+
private JsonPrimitive key;
28+
private JsonPrimitive secondary;
29+
private JsonPrimitive ip;
30+
private JsonPrimitive email;
31+
private JsonPrimitive name;
32+
private JsonPrimitive avatar;
33+
private JsonPrimitive firstName;
34+
private JsonPrimitive lastName;
35+
private JsonPrimitive anonymous;
36+
37+
private JsonPrimitive country;
3838
private Map<String, JsonElement> custom;
3939
private static final Logger logger = LoggerFactory.getLogger(LDUser.class);
4040

@@ -44,16 +44,16 @@ public class LDUser {
4444
}
4545

4646
protected LDUser(Builder builder) {
47-
this.key = builder.key;
48-
this.ip = builder.ip;
49-
this.country = builder.country;
50-
this.secondary = builder.secondary;
51-
this.firstName = builder.firstName;
52-
this.lastName = builder.lastName;
53-
this.email = builder.email;
54-
this.name = builder.name;
55-
this.avatar = builder.avatar;
56-
this.anonymous = builder.anonymous;
47+
this.key = builder.key == null? null : new JsonPrimitive(builder.key);
48+
this.ip = builder.ip == null? null : new JsonPrimitive(builder.ip);
49+
this.country = builder.country == null? null : new JsonPrimitive(builder.country.getAlpha2());
50+
this.secondary = builder.secondary == null ? null : new JsonPrimitive(builder.secondary);
51+
this.firstName = builder.firstName == null ? null : new JsonPrimitive(builder.firstName);
52+
this.lastName = builder.lastName == null ? null : new JsonPrimitive(builder.lastName);
53+
this.email = builder.email == null ? null : new JsonPrimitive(builder.email);
54+
this.name = builder.name == null ? null : new JsonPrimitive(builder.name);
55+
this.avatar = builder.avatar == null ? null : new JsonPrimitive(builder.avatar);
56+
this.anonymous = builder.anonymous == null ? null : new JsonPrimitive(builder.anonymous);
5757
this.custom = new HashMap<String, JsonElement>(builder.custom);
5858
}
5959

@@ -62,31 +62,31 @@ protected LDUser(Builder builder) {
6262
* @param key a {@code String} that uniquely identifies a user
6363
*/
6464
public LDUser(String key) {
65-
this.key = key;
65+
this.key = new JsonPrimitive(key);
6666
this.custom = new HashMap<String, JsonElement>();
6767
}
6868

69-
String getKey() {
69+
JsonPrimitive getKey() {
7070
return key;
7171
}
7272

73-
String getIp() { return ip; }
73+
JsonPrimitive getIp() { return ip; }
7474

75-
LDCountryCode getCountry() { return country; }
75+
JsonPrimitive getCountry() { return country; }
7676

77-
String getSecondary() { return secondary; }
77+
JsonPrimitive getSecondary() { return secondary; }
7878

79-
String getName() { return name; }
79+
JsonPrimitive getName() { return name; }
8080

81-
String getFirstName() { return firstName; }
81+
JsonPrimitive getFirstName() { return firstName; }
8282

83-
String getLastName() { return lastName; }
83+
JsonPrimitive getLastName() { return lastName; }
8484

85-
String getEmail() { return email; }
85+
JsonPrimitive getEmail() { return email; }
8686

87-
String getAvatar() { return avatar; }
87+
JsonPrimitive getAvatar() { return avatar; }
8888

89-
Boolean getAnonymous() { return anonymous; }
89+
JsonPrimitive getAnonymous() { return anonymous; }
9090

9191
JsonElement getCustom(String key) {
9292
return custom.get(key);

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static class Builder<E> {
9292
Builder(E value, int weight) {
9393
this.value = value;
9494
this.weight = weight;
95-
this.userTarget = new TargetRule("key", "in", new ArrayList<Object>());
95+
this.userTarget = new TargetRule("key", "in", new ArrayList<JsonPrimitive>());
9696
targets = new ArrayList<TargetRule>();
9797
}
9898

@@ -115,17 +115,21 @@ Variation<E> build() {
115115
static class TargetRule {
116116
String attribute;
117117
String operator;
118-
List<Object> values;
118+
List<JsonPrimitive> values;
119119

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

122-
TargetRule(String attribute, String operator, List<Object> values) {
122+
public TargetRule() {
123+
124+
}
125+
126+
TargetRule(String attribute, String operator, List<JsonPrimitive> values) {
123127
this.attribute = attribute;
124128
this.operator = operator;
125-
this.values = new ArrayList<Object>(values);
129+
this.values = new ArrayList<JsonPrimitive>(values);
126130
}
127131

128-
TargetRule(String attribute, List<Object> values) {
132+
TargetRule(String attribute, List<JsonPrimitive> values) {
129133
this(attribute, "in", values);
130134
}
131135

@@ -143,7 +147,7 @@ else if (attribute.equals("ip") && user.getIp() != null) {
143147
}
144148
else if (attribute.equals("country")) {
145149
if (user.getCountry() != null) {
146-
uValue = user.getCountry().getAlpha2();
150+
uValue = user.getCountry();
147151
}
148152
}
149153
else if (attribute.equals("email")) {
@@ -187,21 +191,14 @@ else if (attribute.equals("anonymous")) {
187191
logger.error("Invalid custom attribute value in user object: " + elt);
188192
return false;
189193
}
190-
else if (values.contains(elt.getAsString())) {
194+
else if (values.contains(elt.getAsJsonPrimitive())) {
191195
return true;
192196
}
193197
}
194198
return false;
195199
}
196200
else if (custom.isJsonPrimitive()) {
197-
JsonPrimitive prim = custom.getAsJsonPrimitive();
198-
if (prim.isNumber()) {
199-
return values.contains(custom.getAsDouble());
200-
} else if (prim.isBoolean()) {
201-
return values.contains(custom.getAsBoolean());
202-
} else {
203-
return values.contains(custom.getAsString());
204-
}
201+
return values.contains(custom.getAsJsonPrimitive());
205202
}
206203
}
207204
return false;

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

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.launchdarkly.client;
22

3+
import com.google.gson.JsonPrimitive;
4+
import org.glassfish.jersey.server.JSONP;
35
import org.junit.Test;
46

57
import java.util.Arrays;
@@ -8,24 +10,24 @@
810

911
public class FeatureRepTest {
1012

11-
private final Variation.TargetRule targetUserOn = new Variation.TargetRule("key", Collections.<Object>singletonList("targetOn@test.com"));
13+
private final Variation.TargetRule targetUserOn = new Variation.TargetRule("key", Collections.<JsonPrimitive>singletonList(new JsonPrimitive("targetOn@test.com")));
1214

13-
private final Variation.TargetRule targetGroupOn = new Variation.TargetRule("groups", Arrays.<Object>asList("google", "microsoft"));
15+
private final Variation.TargetRule targetGroupOn = new Variation.TargetRule("groups", Arrays.<JsonPrimitive>asList(new JsonPrimitive("google"), new JsonPrimitive("microsoft")));
1416

1517
// GSON will deserialize numbers as decimals
16-
private final Variation.TargetRule targetFavoriteNumberOn = new Variation.TargetRule("favorite_number", Arrays.<Object>asList(42.0));
18+
private final Variation.TargetRule targetFavoriteNumberOn = new Variation.TargetRule("favorite_number", Arrays.<JsonPrimitive>asList(new JsonPrimitive(42)));
1719

18-
private final Variation.TargetRule targetLikesCatsOn = new Variation.TargetRule("likes_cats", Arrays.<Object>asList(true));
20+
private final Variation.TargetRule targetLikesCatsOn = new Variation.TargetRule("likes_cats", Arrays.<JsonPrimitive>asList(new JsonPrimitive(true)));
1921

20-
private final Variation.TargetRule targetUserOff = new Variation.TargetRule("key", Collections.<Object>singletonList("targetOff@test.com"));
22+
private final Variation.TargetRule targetUserOff = new Variation.TargetRule("key", Collections.<JsonPrimitive>singletonList(new JsonPrimitive("targetOff@test.com")));
2123

22-
private final Variation.TargetRule targetGroupOff = new Variation.TargetRule("groups", Arrays.<Object>asList("oracle"));
24+
private final Variation.TargetRule targetGroupOff = new Variation.TargetRule("groups", Arrays.<JsonPrimitive>asList(new JsonPrimitive("oracle")));
2325

24-
private final Variation.TargetRule targetFavoriteNumberOff = new Variation.TargetRule("favorite_number", Arrays.<Object>asList(33.0));
26+
private final Variation.TargetRule targetFavoriteNumberOff = new Variation.TargetRule("favorite_number", Arrays.<JsonPrimitive>asList(new JsonPrimitive(33.0)));
2527

26-
private final Variation.TargetRule targetLikesDogsOff = new Variation.TargetRule("likes_dogs", Arrays.<Object>asList(false));
28+
private final Variation.TargetRule targetLikesDogsOff = new Variation.TargetRule("likes_dogs", Arrays.<JsonPrimitive>asList(new JsonPrimitive(false)));
2729

28-
private final Variation.TargetRule targetAnonymousOn = new Variation.TargetRule("anonymous", Collections.<Object>singletonList(true));
30+
private final Variation.TargetRule targetAnonymousOn = new Variation.TargetRule("anonymous", Collections.<JsonPrimitive>singletonList(new JsonPrimitive(true)));
2931

3032
private final Variation<Boolean> trueVariation = new Variation.Builder<Boolean>(true, 80)
3133
.target(targetUserOn)
@@ -119,16 +121,27 @@ public void testFlagForTargetNumericTestOn() {
119121
assertEquals(true, b);
120122
}
121123

122-
@Test
123-
public void testFlagForTargetBooleanTestOn() {
124-
LDUser user = new LDUser.Builder("targetOther@test.com")
125-
.custom("likes_cats", true)
126-
.build();
124+
@Test
125+
public void testFlagForTargetNumericListTestOn() {
126+
LDUser user = new LDUser.Builder("targetOther@test.com")
127+
.customNumber("favorite_number", Arrays.<Number>asList(42, 32))
128+
.build();
127129

128-
Boolean b = simpleFlag.evaluate(user);
130+
Boolean b = simpleFlag.evaluate(user);
129131

130-
assertEquals(true, b);
131-
}
132+
assertEquals(true, b);
133+
}
134+
135+
@Test
136+
public void testFlagForTargetBooleanTestOn() {
137+
LDUser user = new LDUser.Builder("targetOther@test.com")
138+
.custom("likes_cats", true)
139+
.build();
140+
141+
Boolean b = simpleFlag.evaluate(user);
142+
143+
assertEquals(true, b);
144+
}
132145

133146
@Test
134147
public void testFlagForTargetGroupOff() {
@@ -152,16 +165,16 @@ public void testFlagForTargetNumericTestOff() {
152165
assertEquals(false, b);
153166
}
154167

155-
@Test
156-
public void testFlagForTargetBooleanTestOff() {
157-
LDUser user = new LDUser.Builder("targetOther@test.com")
158-
.custom("likes_dogs", false)
159-
.build();
168+
@Test
169+
public void testFlagForTargetBooleanTestOff() {
170+
LDUser user = new LDUser.Builder("targetOther@test.com")
171+
.custom("likes_dogs", false)
172+
.build();
160173

161-
Boolean b = simpleFlag.evaluate(user);
174+
Boolean b = simpleFlag.evaluate(user);
162175

163-
assertEquals(false, b);
164-
}
176+
assertEquals(false, b);
177+
}
165178

166179
@Test
167180
public void testDisabledFlagAlwaysOff() {

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
11
package com.launchdarkly.client;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.JsonPrimitive;
45
import org.junit.Test;
56

67
public class LDUserTest {
78

9+
private JsonPrimitive us = new JsonPrimitive(LDCountryCode.US.getAlpha2());
10+
811
@Test
912
public void testValidCountryCodeSetsCountry() {
1013
LDUser user = new LDUser.Builder("key").country(LDCountryCode.US).build();
1114

12-
assert(user.getCountry().equals(LDCountryCode.US));
15+
assert(user.getCountry().equals(us));
1316
}
1417

1518

1619
@Test
1720
public void testValidCountryCodeStringSetsCountry() {
1821
LDUser user = new LDUser.Builder("key").country("US").build();
1922

20-
assert(user.getCountry().equals(LDCountryCode.US));
23+
assert(user.getCountry().equals(us));
2124
}
2225

2326
@Test
2427
public void testValidCountryCode3SetsCountry() {
2528
LDUser user = new LDUser.Builder("key").country("USA").build();
2629

27-
assert(user.getCountry().equals(LDCountryCode.US));
30+
assert(user.getCountry().equals(us));
2831
}
2932

3033
@Test
3134
public void testAmbiguousCountryNameSetsCountryWithExactMatch() {
3235
// "United States" is ambiguous: can also match "United States Minor Outlying Islands"
3336
LDUser user = new LDUser.Builder("key").country("United States").build();
34-
assert(user.getCountry().equals(LDCountryCode.US));
37+
assert(user.getCountry().equals(us));
3538
}
3639

3740
@Test
@@ -45,7 +48,7 @@ public void testAmbiguousCountryNameSetsCountryWithPartialMatch() {
4548
@Test
4649
public void testPartialUniqueMatchSetsCountry() {
4750
LDUser user = new LDUser.Builder("key").country("United States Minor").build();
48-
assert(user.getCountry().equals(LDCountryCode.UM));
51+
assert(user.getCountry().equals(new JsonPrimitive(LDCountryCode.UM.getAlpha2())));
4952
}
5053

5154
@Test
@@ -63,6 +66,6 @@ public void testLDUserJsonSerializationContainsCountryAsTwoDigitCode() {
6366

6467
LDUser deserialized = gson.fromJson(jsonStr, LDUser.class);
6568

66-
assert(deserialized.getCountry().equals(LDCountryCode.US));
69+
assert(deserialized.getCountry().equals(us));
6770
}
6871
}

0 commit comments

Comments
 (0)