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

Support conversion from an LDUser to a JsonObject #136

Closed
wants to merge 2 commits into from
Closed
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
30 changes: 30 additions & 0 deletions src/main/java/com/launchdarkly/client/LDUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.Streams;
Expand All @@ -14,6 +16,8 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -143,6 +147,32 @@ JsonElement getCustom(String key) {
return null;
}

JsonObject toJsConfig(LDConfig config) {
JsonElement tree = new Gson().toJsonTree(this);

Set<String> privateAttributeSet = new HashSet<>();
privateAttributeSet.addAll(this.privateAttributeNames);
privateAttributeSet.addAll(config.privateAttrNames);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure of the reason for this line. The front end already has a privateAttributeNames configuration option for the client, just like the back end; if you're using that option on the back end, presumably you would be doing the same on the front end. The privateAttributeNames property in the user is meant for attributes that have been marked private for this user.

I mean, doing this won't hurt anything, but I think it is unnecessary and it also conflicts with what is arguably the normal goal of JSON serialization: capturing the state of this particular object.

Copy link
Contributor

@eli-darkly eli-darkly Aug 28, 2018

Choose a reason for hiding this comment

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

Similarly, I think adding the allAttributesPrivate property from the client configuration into the user object is misleading (since the JS client will not actually use the property there). If you want your front-end client to always use the same configuration as the back end without having to specify it in code, I think it would be better to pass those configuration options separately.


List<String> attrs = new ArrayList<>();
attrs.addAll(privateAttributeSet);
Collections.sort(attrs);

JsonArray array = new JsonArray();
for (String element : attrs) {
array.add(new JsonPrimitive(element));
}

JsonObject object = tree.getAsJsonObject();
object.add("privateAttributeNames", array);

if (config.allAttributesPrivate) {
object.addProperty("allAttributesPrivate", true);
}

return object;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/com/launchdarkly/client/LDUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,56 @@ public void testInvalidCountryNameDoesNotSetCountry() {
assert(user.getCountry() == null);
}

@Test
public void testLDUserJsConfig() {
LDConfig config = LDConfig.DEFAULT;
LDUser user = new LDUser.Builder("key")
.privateCustom("private-key", "private-value")
.custom("public-key", "public-value")
.build();

JsonObject object = user.toJsConfig(config);

assertEquals("key", object.get("key").getAsString());

JsonArray privateAttributeNames = object.get("privateAttributeNames").getAsJsonArray();
assertEquals(1, privateAttributeNames.size());
assertEquals("private-key", privateAttributeNames.get(0).getAsString());

assertEquals("private-value", object.get("custom").getAsJsonObject().get("private-key").getAsString());
assertEquals("public-value", object.get("custom").getAsJsonObject().get("public-key").getAsString());
}

@Test
public void testLDUserJsConfigAllAttributesPrivate() {
LDConfig config = new LDConfig.Builder()
.allAttributesPrivate(true)
.build();
LDUser user = new LDUser.Builder("key")
.build();

JsonObject object = user.toJsConfig(config);

assertTrue(object.get("allAttributesPrivate").getAsBoolean());
}

@Test
public void testLDUserJsConfigPrivate() {
LDConfig config = new LDConfig.Builder()
.privateAttributeNames("private-key")
.build();
LDUser user = new LDUser.Builder("key")
.privateCustom("private-key2", "private-value2")
.build();

JsonObject object = user.toJsConfig(config);

JsonArray privateAttributeNames = object.get("privateAttributeNames").getAsJsonArray();
assertEquals(2, privateAttributeNames.size());
assertEquals("private-key", privateAttributeNames.get(0).getAsString());
assertEquals("private-key2", privateAttributeNames.get(1).getAsString());
}

@Test
public void testLDUserJsonSerializationContainsCountryAsTwoDigitCode() {
LDConfig config = LDConfig.DEFAULT;
Expand Down