Skip to content

Commit 0c67ceb

Browse files
DVC-7342 add setClientCustomData() (#58)
* adding new setClientCustomData() function call for the WASM * Update to latest WASM version
1 parent 9929be6 commit 0c67ceb

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ repositories {
8686
sourceCompatibility = JavaVersion.VERSION_1_8
8787

8888
def wasmResourcePath = "$projectDir/src/main/resources"
89-
def wasmVersion = "1.3.3"
89+
def wasmVersion = "1.6.7"
9090
def wasmUrl = "https://unpkg.com/@devcycle/bucketing-assembly-script@$wasmVersion/build/bucketing-lib.release.wasm"
9191
task downloadDVCBucketingWASM(type: Download) {
9292
src wasmUrl

src/main/java/com/devcycle/sdk/server/local/api/DVCLocalClient.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.devcycle.sdk.server.local.model.BucketedUserConfig;
1313
import com.devcycle.sdk.server.local.model.DVCLocalOptions;
1414
import com.fasterxml.jackson.core.JsonProcessingException;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
1516

1617
public final class DVCLocalClient {
1718

@@ -193,6 +194,24 @@ public void track(User user, Event event) {
193194
}
194195
}
195196

197+
public void setClientCustomData(Map<String,Object> customData) {
198+
if (!isInitialized || !configManager.isConfigInitialized())
199+
{
200+
System.out.println("SetClientCustomData called before DVCClient has initialized");
201+
return;
202+
}
203+
204+
if (customData != null && !customData.isEmpty()) {
205+
try {
206+
ObjectMapper mapper = new ObjectMapper();
207+
String customDataJSON = mapper.writeValueAsString(customData);
208+
localBucketing.setClientCustomData(this.sdkKey, customDataJSON);
209+
} catch(Exception e) {
210+
System.out.printf("Failed to set custom data: %s%n", e.getMessage());
211+
}
212+
}
213+
}
214+
196215
/**
197216
* Gracefully close the client
198217
*

src/main/java/com/devcycle/sdk/server/local/bucketing/LocalBucketing.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ public void setPlatformData(String platformData) {
147147
fn.accept(platformDataAddress);
148148
}
149149

150+
public void setClientCustomData(String sdkKey, String customData) {
151+
unpinAll();
152+
int sdkKeyAddress = getSDKKeyAddress(sdkKey);
153+
int customDataAddress = newWasmString(customData);
154+
155+
Func setCustomClientDataPtr = linker.get(store, "", "setClientCustomData").get().func();
156+
WasmFunctions.Consumer2<Integer, Integer> fn = WasmFunctions.consumer(store, setCustomClientDataPtr, I32, I32);
157+
fn.accept(sdkKeyAddress, customDataAddress);
158+
}
159+
150160
public BucketedUserConfig generateBucketedConfig(String sdkKey, User user) throws JsonProcessingException {
151161
unpinAll();
152162
String userString = OBJECT_MAPPER.writeValueAsString(user);
65.7 KB
Binary file not shown.

src/test/java/com/devcycle/sdk/server/local/DVCLocalClientTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.devcycle.sdk.server.local;
22

3+
import java.util.HashMap;
34
import java.util.Map;
45
import java.util.UUID;
56

@@ -78,6 +79,16 @@ public void allVariablesTest() {
7879
Assert.assertEquals(variables.size(), 2);
7980
}
8081

82+
@Test
83+
public void setClientCustomDataWithBadMap(){
84+
// should be a no-op
85+
client.setClientCustomData(null);
86+
87+
// should be a no-op
88+
Map<String, Object> testData = new HashMap();
89+
client.setClientCustomData(testData);
90+
}
91+
8192
private User getUser() {
8293
return User.builder()
8394
.userId("j_test")

src/test/java/com/devcycle/sdk/server/local/LocalBucketingTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.devcycle.sdk.server.local;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
35
import java.util.UUID;
46

57
import org.junit.Assert;
@@ -34,6 +36,21 @@ public void setup(){
3436
localBucketing.storeConfig(apiKey, testConfigString);
3537
}
3638

39+
@Test
40+
public void testSetClientCustomData() {
41+
Map<String,Object> testData = new HashMap();
42+
testData.put("stringProp", "test");
43+
testData.put("intProp", 1);
44+
testData.put("booleanProp", true);
45+
46+
try {
47+
String customData = mapper.writeValueAsString(testData);
48+
localBucketing.setClientCustomData(apiKey, customData);
49+
} catch (JsonProcessingException e) {
50+
throw new RuntimeException(e);
51+
}
52+
}
53+
3754
@Test
3855
public void testEventQueue() throws JsonProcessingException {
3956
Event event = Event.builder().type("test").target("target").build();

0 commit comments

Comments
 (0)