Skip to content

Commit 7a6a1db

Browse files
feat: Support inline context for custom and migration events (#55)
1 parent 84d531d commit 7a6a1db

File tree

9 files changed

+39
-80
lines changed

9 files changed

+39
-80
lines changed

lib/shared/common/CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ If you wish to clean your working directory between builds, you can clean it by
3737
./gradlew clean
3838
```
3939

40-
If you wish to use your generated SDK artifact by another Maven/Gradle project such as [java-server-sdk](https://github.com/launchdarkly/java-server-sdk), you will likely want to publish the artifact to your local Maven repository so that your other project can access it.
40+
If you wish to use your generated SDK artifact by another Maven/Gradle project such as [java-server-sdk](https://github.com/launchdarkly/java-server-sdk), you will likely want to publish the artifact to your local Maven repository so that your other project can access it. You can append `-SNAPSHOT` to the version in [gradle.properties](gradle.properties) and update any dependancies to test the snapshot version.
4141
```
42-
./gradlew publishToMavenLocal
42+
./gradlew publishToMavenLocal -PskipSigning=true
4343
```
4444

4545
### Testing

lib/shared/common/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,6 @@ nexusPublishing {
8888
}
8989

9090
signing {
91+
setRequired({ findProperty("skipSigning") != "true" })
9192
sign(publishing.publications["mavenJava"])
9293
}

lib/shared/internal/CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ If you wish to clean your working directory between builds, you can clean it by
4545
./gradlew clean
4646
```
4747

48-
If you wish to use your generated SDK artifact by another Maven/Gradle project such as [java-server-sdk](https://github.com/launchdarkly/java-server-sdk), you will likely want to publish the artifact to your local Maven repository so that your other project can access it.
48+
If you wish to use your generated SDK artifact by another Maven/Gradle project such as [java-server-sdk](https://github.com/launchdarkly/java-server-sdk), you will likely want to publish the artifact to your local Maven repository so that your other project can access it. You can append `-SNAPSHOT` to the version in [gradle.properties](gradle.properties) and update any dependancies to test the snapshot version.
4949
```
50-
./gradlew publishToMavenLocal
50+
./gradlew publishToMavenLocal -PskipSigning=true
5151
```
5252

5353
### Testing

lib/shared/internal/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,6 @@ nexusPublishing {
8282
}
8383

8484
signing {
85+
setRequired({ findProperty("skipSigning") != "true" })
8586
sign(publishing.publications["mavenJava"])
8687
}

lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/EventOutputFormatter.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private boolean writeOutputEvent(Event event, JsonWriter jw) throws IOException
8989
jw.beginObject();
9090
writeKindAndCreationDate(jw, "custom", event.getCreationDate());
9191
jw.name("key").value(ce.getKey());
92-
writeContextKeys(ce.getContext(), jw);
92+
writeContext(ce.getContext(), jw, false);
9393
writeLDValue("data", ce.getData(), jw);
9494
if (ce.getMetricValue() != null) {
9595
jw.name("metricValue");
@@ -104,7 +104,7 @@ private boolean writeOutputEvent(Event event, JsonWriter jw) throws IOException
104104
} else if (event instanceof Event.MigrationOp) {
105105
jw.beginObject();
106106
writeKindAndCreationDate(jw, "migration_op", event.getCreationDate());
107-
writeContextKeys(event.getContext(), jw);
107+
writeContext(event.getContext(), jw, false);
108108

109109
Event.MigrationOp me = (Event.MigrationOp)event;
110110
jw.name("operation").value(me.getOperation());
@@ -296,17 +296,6 @@ private void writeContext(LDContext context, JsonWriter jw, boolean redactAnonym
296296
contextFormatter.write(context, jw, redactAnonymous);
297297
}
298298

299-
private void writeContextKeys(LDContext context, JsonWriter jw) throws IOException {
300-
jw.name("contextKeys").beginObject();
301-
for (int i = 0; i < context.getIndividualContextCount(); i++) {
302-
LDContext c = context.getIndividualContext(i);
303-
if (c != null) {
304-
jw.name(c.getKind().toString()).value(c.getKey());
305-
}
306-
}
307-
jw.endObject();
308-
}
309-
310299
private void writeLDValue(String key, LDValue value, JsonWriter jw) throws IOException {
311300
if (value == null || value.isNull()) {
312301
return;

lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/events/BaseEventTest.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ public static Matcher<JsonTestValue> isIdentifyEvent(Event sourceEvent, LDValue
203203
);
204204
}
205205

206-
public static Matcher<JsonTestValue> isMigrationEvent(Event sourceEvent, LDValue context) {
206+
public static Matcher<JsonTestValue> isMigrationEvent(Event sourceEvent, LDValue inlineContext) {
207207
// Doesn't fully test an event, but makes sure it is a specific event.
208208
return allOf(
209209
jsonProperty("kind", "migration_op"),
210210
jsonProperty("creationDate", (double)sourceEvent.getCreationDate()),
211-
hasContextKeys(sourceEvent)
211+
hasInlineContext(inlineContext)
212212
);
213213
}
214214

@@ -248,28 +248,18 @@ private static Matcher<JsonTestValue> isFeatureOrDebugEvent(Event.FeatureRequest
248248
);
249249
}
250250

251-
public static Matcher<JsonTestValue> isCustomEvent(Event.Custom sourceEvent) {
251+
public static Matcher<JsonTestValue> isCustomEvent(Event.Custom sourceEvent, LDValue inlineContext) {
252252
boolean hasData = sourceEvent.getData() != null && !sourceEvent.getData().isNull();
253253
return allOf(
254254
jsonProperty("kind", "custom"),
255255
jsonProperty("creationDate", (double)sourceEvent.getCreationDate()),
256256
jsonProperty("key", sourceEvent.getKey()),
257-
hasContextKeys(sourceEvent),
257+
hasInlineContext(inlineContext),
258258
jsonProperty("data", hasData ? jsonEqualsValue(sourceEvent.getData()) : jsonUndefined()),
259259
jsonProperty("metricValue", sourceEvent.getMetricValue() == null ? jsonUndefined() : jsonEqualsValue(sourceEvent.getMetricValue()))
260260
);
261261
}
262262

263-
public static Matcher<JsonTestValue> hasContextKeys(Event sourceEvent) {
264-
ObjectBuilder b = LDValue.buildObject();
265-
LDContext c = sourceEvent.getContext();
266-
for (int i = 0; i < c.getIndividualContextCount(); i++) {
267-
LDContext c1 = c.getIndividualContext(i);
268-
b.put(c1.getKind().toString(), c1.getKey());
269-
}
270-
return jsonProperty("contextKeys", jsonEqualsValue(b.build()));
271-
}
272-
273263
public static Matcher<JsonTestValue> hasInlineContext(LDValue inlineContext) {
274264
return allOf(
275265
jsonProperty("context", jsonEqualsValue(inlineContext)),

lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/events/DefaultEventProcessorOutputTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public void customEventIsQueuedWithUser() throws Exception {
399399
}
400400

401401
assertThat(es.getEventsFromLastRequest(), contains(
402-
isCustomEvent(ce)
402+
isCustomEvent(ce, userJson)
403403
));
404404
}
405405

@@ -419,7 +419,7 @@ public void customEventWithNullContextOrInvalidContextDoesNotCauseError() throws
419419
}
420420

421421
assertThat(es.getEventsFromLastRequest(), contains(
422-
isCustomEvent(event3)
422+
isCustomEvent(event3, userJson)
423423
));
424424
}
425425

lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/events/DefaultEventProcessorTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public void eventsAreFlushedAutomatically() throws Exception {
4444
// sendEvent calls, they might be in two
4545
List<JsonTestValue> payload1 = es.getEventsFromLastRequest();
4646
if (payload1.size() == 1) {
47-
assertThat(payload1, contains(isCustomEvent(event1)));
48-
assertThat(es.getEventsFromLastRequest(), contains(isCustomEvent(event2)));
47+
assertThat(payload1, contains(isCustomEvent(event1, userJson)));
48+
assertThat(es.getEventsFromLastRequest(), contains(isCustomEvent(event2, userJson)));
4949
} else {
50-
assertThat(payload1, contains(isCustomEvent(event1), isCustomEvent(event2)));
50+
assertThat(payload1, contains(isCustomEvent(event1, userJson), isCustomEvent(event2, userJson)));
5151
}
5252

5353
Event.Custom event3 = customEvent(user, "event3").build();
5454
ep.sendEvent(event3);
55-
assertThat(es.getEventsFromLastRequest(), contains(isCustomEvent(event3)));
55+
assertThat(es.getEventsFromLastRequest(), contains(isCustomEvent(event3, userJson)));
5656
}
5757

5858
Event.Custom ce = customEvent(user, "eventkey").build();
@@ -62,7 +62,7 @@ public void eventsAreFlushedAutomatically() throws Exception {
6262
}
6363

6464
assertThat(es.getEventsFromLastRequest(), contains(
65-
isCustomEvent(ce)
65+
isCustomEvent(ce, userJson)
6666
));
6767
}
6868

@@ -85,7 +85,7 @@ public void eventsAreNotFlushedWhenNotConnected() throws Exception {
8585
ep.setOffline(false);
8686

8787
List<JsonTestValue> payload1 = es.getEventsFromLastRequest();
88-
assertThat(payload1, contains(isCustomEvent(event1), isCustomEvent(event2)));
88+
assertThat(payload1, contains(isCustomEvent(event1, userJson), isCustomEvent(event2, userJson)));
8989
}
9090
}
9191

lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/events/EventOutputTest.java

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,6 @@ public void allAttributesAreSerialized() throws Exception {
4545
defaultEventsConfig());
4646
}
4747

48-
@Test
49-
public void contextKeysAreSetInsteadOfContextWhenNotInlined() throws Exception {
50-
testContextKeysSerialization(
51-
LDContext.create("userkey"),
52-
LDValue.buildObject().put("user", "userkey").build()
53-
);
54-
55-
testContextKeysSerialization(
56-
LDContext.create(ContextKind.of("kind1"), "key1"),
57-
LDValue.buildObject().put("kind1", "key1").build()
58-
);
59-
60-
testContextKeysSerialization(
61-
LDContext.createMulti(
62-
LDContext.create(ContextKind.of("kind1"), "key1"),
63-
LDContext.create(ContextKind.of("kind2"), "key2")),
64-
LDValue.buildObject().put("kind1", "key1").put("kind2", "key2").build()
65-
);
66-
}
67-
6848
@Test
6949
public void allAttributesPrivateMakesAttributesPrivate() throws Exception {
7050
// We test this behavior in more detail in EventContextFormatterTest, but here we're verifying that the
@@ -265,15 +245,15 @@ public void identifyEventIsSerialized() throws IOException {
265245
@Test
266246
public void customEventIsSerialized() throws IOException {
267247
LDContext context = LDContext.builder("userkey").name("me").build();
268-
LDValue contextKeysJson = LDValue.buildObject().put("user", context.getKey()).build();
248+
LDValue contextJson = LDValue.buildObject().put("kind", "user").put("key", "userkey").put("name", "me").build();
269249
EventOutputFormatter f = new EventOutputFormatter(defaultEventsConfig());
270250

271251
Event.Custom ceWithoutData = customEvent(context, "customkey").build();
272252
LDValue ceJson1 = parseValue("{" +
273253
"\"kind\":\"custom\"," +
274254
"\"creationDate\":100000," +
275255
"\"key\":\"customkey\"," +
276-
"\"contextKeys\":" + contextKeysJson +
256+
"\"context\":" + contextJson +
277257
"}");
278258
assertJsonEquals(ceJson1, getSingleOutputEvent(f, ceWithoutData));
279259

@@ -282,7 +262,7 @@ public void customEventIsSerialized() throws IOException {
282262
"\"kind\":\"custom\"," +
283263
"\"creationDate\":100000," +
284264
"\"key\":\"customkey\"," +
285-
"\"contextKeys\":" + contextKeysJson + "," +
265+
"\"context\":" + contextJson + "," +
286266
"\"data\":\"thing\"" +
287267
"}");
288268
assertJsonEquals(ceJson2, getSingleOutputEvent(f, ceWithData));
@@ -292,7 +272,7 @@ public void customEventIsSerialized() throws IOException {
292272
"\"kind\":\"custom\"," +
293273
"\"creationDate\":100000," +
294274
"\"key\":\"customkey\"," +
295-
"\"contextKeys\":" + contextKeysJson + "," +
275+
"\"context\":" + contextJson + "," +
296276
"\"metricValue\":2.5" +
297277
"}");
298278
assertJsonEquals(ceJson3, getSingleOutputEvent(f, ceWithMetric));
@@ -303,7 +283,7 @@ public void customEventIsSerialized() throws IOException {
303283
"\"kind\":\"custom\"," +
304284
"\"creationDate\":100000," +
305285
"\"key\":\"customkey\"," +
306-
"\"contextKeys\":" + contextKeysJson + "," +
286+
"\"context\":" + contextJson + "," +
307287
"\"data\":\"thing\"," +
308288
"\"metricValue\":2.5" +
309289
"}");
@@ -408,8 +388,10 @@ public void migrationOpEventIsSerialized() throws IOException {
408388
.put("reason", LDValue.buildObject()
409389
.put("kind", "FALLTHROUGH")
410390
.build()).build())
411-
.put("contextKeys", LDValue.buildObject()
412-
.put("user", "user-key")
391+
.put("context", LDValue.buildObject()
392+
.put("kind", "user")
393+
.put("key", "user-key")
394+
.put("name", "me")
413395
.build())
414396
.put("samplingRatio", 2)
415397
.put("measurements", LDValue.buildArray()
@@ -495,8 +477,10 @@ public void migrationOpEventSerializationCanExcludeOptionalItems() throws IOExce
495477
.put("reason", LDValue.buildObject()
496478
.put("kind", "FALLTHROUGH")
497479
.build()).build())
498-
.put("contextKeys", LDValue.buildObject()
499-
.put("user", "user-key")
480+
.put("context", LDValue.buildObject()
481+
.put("kind", "user")
482+
.put("key", "user-key")
483+
.put("name", "me")
500484
.build())
501485
.put("measurements", LDValue.buildArray()
502486
.add(LDValue.buildObject()
@@ -705,25 +689,19 @@ private LDValue getSingleOutputEvent(EventOutputFormatter f, Event event) throws
705689
return parseValue(w.toString()).get(0);
706690
}
707691

708-
private void testContextKeysSerialization(LDContext context, LDValue expectedJsonValue) throws IOException {
709-
EventsConfiguration config = makeEventsConfig(false, null);
710-
EventOutputFormatter f = new EventOutputFormatter(config);
711-
712-
Event.Custom customEvent = customEvent(context, "eventkey").build();
713-
LDValue outputEvent = getSingleOutputEvent(f, customEvent);
714-
assertJsonEquals(expectedJsonValue, outputEvent.get("contextKeys"));
715-
assertJsonEquals(LDValue.ofNull(), outputEvent.get("context"));
716-
}
717-
718692
private void testInlineContextSerialization(LDContext context, LDValue expectedJsonValue, EventsConfiguration baseConfig) throws IOException {
719693
EventsConfiguration config = makeEventsConfig(baseConfig.allAttributesPrivate, baseConfig.privateAttributes);
720694
EventOutputFormatter f = new EventOutputFormatter(config);
721695

722-
Event.FeatureRequest featureEvent = featureEvent(context, FLAG_KEY).build();
723-
LDValue outputEvent = getSingleOutputEvent(f, featureEvent);
696+
Event.Custom customEvent = customEvent(context, FLAG_KEY).build();
697+
LDValue outputEvent = getSingleOutputEvent(f, customEvent);
724698
assertJsonEquals(LDValue.ofNull(), outputEvent.get("contextKeys"));
725699
assertJsonEquals(expectedJsonValue, outputEvent.get("context"));
726700

701+
Event.FeatureRequest featureEvent = featureEvent(context, FLAG_KEY).build();
702+
outputEvent = getSingleOutputEvent(f, featureEvent);
703+
assertJsonEquals(LDValue.ofNull(), outputEvent.get("contextKeys"));
704+
assertJsonEquals(expectedJsonValue, outputEvent.get("context"));
727705

728706
Event.Identify identifyEvent = identifyEvent(context);
729707
outputEvent = getSingleOutputEvent(f, identifyEvent);

0 commit comments

Comments
 (0)