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

Commit 1d4084b

Browse files
committed
add custom attribute overloads for arbitrary JSON value
1 parent 42a5a37 commit 1d4084b

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -608,11 +608,7 @@ public Builder privateEmail(String email) {
608608
* @return the builder
609609
*/
610610
public Builder custom(String k, String v) {
611-
checkCustomAttribute(k);
612-
if (k != null && v != null) {
613-
custom.put(k, new JsonPrimitive(v));
614-
}
615-
return this;
611+
return custom(k, v == null ? null : new JsonPrimitive(v));
616612
}
617613

618614
/**
@@ -625,11 +621,7 @@ public Builder custom(String k, String v) {
625621
* @return the builder
626622
*/
627623
public Builder custom(String k, Number n) {
628-
checkCustomAttribute(k);
629-
if (k != null && n != null) {
630-
custom.put(k, new JsonPrimitive(n));
631-
}
632-
return this;
624+
return custom(k, n == null ? null : new JsonPrimitive(n));
633625
}
634626

635627
/**
@@ -642,9 +634,22 @@ public Builder custom(String k, Number n) {
642634
* @return the builder
643635
*/
644636
public Builder custom(String k, Boolean b) {
637+
return custom(k, b == null ? null : new JsonPrimitive(b));
638+
}
639+
640+
/**
641+
* Add a custom attribute whose value can be any JSON type. When set to one of the
642+
* <a href="http://docs.launchdarkly.com/docs/targeting-users#targeting-based-on-user-attributes">built-in
643+
* user attribute keys</a>, this custom attribute will be ignored.
644+
*
645+
* @param k the key for the custom attribute
646+
* @param v the value for the custom attribute
647+
* @return the builder
648+
*/
649+
public Builder custom(String k, JsonElement v) {
645650
checkCustomAttribute(k);
646-
if (k != null && b != null) {
647-
custom.put(k, new JsonPrimitive(b));
651+
if (k != null && v != null) {
652+
custom.put(k, v);
648653
}
649654
return this;
650655
}
@@ -757,6 +762,21 @@ public Builder privateCustom(String k, Boolean b) {
757762
return custom(k, b);
758763
}
759764

765+
/**
766+
* Add a custom attribute of any JSON type, that will not be sent back to LaunchDarkly.
767+
* When set to one of the
768+
* <a href="http://docs.launchdarkly.com/docs/targeting-users#targeting-based-on-user-attributes">built-in
769+
* user attribute keys</a>, this custom attribute will be ignored.
770+
*
771+
* @param k the key for the custom attribute
772+
* @param v the value for the custom attribute
773+
* @return the builder
774+
*/
775+
public Builder privateCustom(String k, JsonElement v) {
776+
privateAttrNames.add(k);
777+
return custom(k, v);
778+
}
779+
760780
/**
761781
* Add a list of {@link java.lang.String}-valued custom attributes. When set to one of the
762782
* <a href="http://docs.launchdarkly.com/docs/targeting-users#targeting-based-on-user-attributes">

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.launchdarkly.client.TestUtil.js;
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertNull;
22+
import static org.junit.Assert.assertTrue;
2223

2324
public class LDUserTest {
2425

@@ -241,6 +242,25 @@ public void getValueReturnsNullIfNotFound() {
241242
assertNull(user.getValueForEvaluation("height"));
242243
}
243244

245+
@Test
246+
public void canAddCustomAttrWithJsonValue() {
247+
JsonElement value = new JsonPrimitive("x");
248+
LDUser user = new LDUser.Builder("key")
249+
.custom("foo", value)
250+
.build();
251+
assertEquals(value, user.getCustom("foo"));
252+
}
253+
254+
@Test
255+
public void canAddPrivateCustomAttrWithJsonValue() {
256+
JsonElement value = new JsonPrimitive("x");
257+
LDUser user = new LDUser.Builder("key")
258+
.privateCustom("foo", value)
259+
.build();
260+
assertEquals(value, user.getCustom("foo"));
261+
assertTrue(user.privateAttributeNames.contains("foo"));
262+
}
263+
244264
@Test
245265
public void canAddCustomAttrWithListOfStrings() {
246266
LDUser user = new LDUser.Builder("key")

0 commit comments

Comments
 (0)