Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ static final class FeatureFlag implements VersionedData, JsonHelpers.PostProcess
private Integer offVariation; //optional
private List<LDValue> variations;
private boolean clientSide;
private ClientSideAvailability clientSideAvailability; // optional
private boolean trackEvents;
private boolean trackEventsFallthrough;
private Long debugEventsUntilDate;
Expand All @@ -145,6 +146,30 @@ static final class FeatureFlag implements VersionedData, JsonHelpers.PostProcess
private Migration migration;
private boolean excludeFromSummaries;

/**
* Tells which kinds of client-side SDK can use the flag. The flag has this object only if its JSON
* has a "clientSideAvailability" object.
*/
static final class ClientSideAvailability {
private boolean usingMobileKey;
private boolean usingEnvironmentId;

ClientSideAvailability() {}

ClientSideAvailability(boolean usingMobileKey, boolean usingEnvironmentId) {
this.usingMobileKey = usingMobileKey;
this.usingEnvironmentId = usingEnvironmentId;
}

boolean isUsingMobileKey() {
return usingMobileKey;
}

boolean isUsingEnvironmentId() {
return usingEnvironmentId;
}
}

/**
* Container for migration specific flag data.
*/
Expand All @@ -168,8 +193,9 @@ public Long getCheckRatio() {

FeatureFlag(String key, int version, boolean on, List<Prerequisite> prerequisites, String salt, List<Target> targets,
List<Target> contextTargets, List<Rule> rules, VariationOrRollout fallthrough, Integer offVariation,
List<LDValue> variations, boolean clientSide, boolean trackEvents, boolean trackEventsFallthrough,
Long debugEventsUntilDate, boolean deleted, Long samplingRatio, Migration migration, boolean excludeFromSummaries) {
List<LDValue> variations, boolean clientSide, ClientSideAvailability clientSideAvailability,
boolean trackEvents, boolean trackEventsFallthrough, Long debugEventsUntilDate, boolean deleted,
Long samplingRatio, Migration migration, boolean excludeFromSummaries) {
this.key = key;
this.version = version;
this.on = on;
Expand All @@ -181,7 +207,9 @@ public Long getCheckRatio() {
this.fallthrough = fallthrough;
this.offVariation = offVariation;
this.variations = variations;
this.clientSide = clientSide;
// If the flag has a "clientSideAvailability" object, "clientSide" has the value of "usingEnvironmentId".
this.clientSide = clientSideAvailability == null ? clientSide : clientSideAvailability.isUsingEnvironmentId();
this.clientSideAvailability = clientSideAvailability;
this.trackEvents = trackEvents;
this.trackEventsFallthrough = trackEventsFallthrough;
this.debugEventsUntilDate = debugEventsUntilDate;
Expand Down Expand Up @@ -259,6 +287,11 @@ boolean isClientSide() {
return clientSide;
}

// Returns null if the JSON for the flag has no "clientSideAvailability" object.
ClientSideAvailability getClientSideAvailability() {
return clientSideAvailability;
}

Long getSamplingRatio() { return samplingRatio; }

Migration getMigration() { return migration; }
Expand All @@ -268,6 +301,12 @@ boolean isExcludeFromSummaries() {
}

public void afterDeserialized() {
// The "clientSideAvailability" object is in the newer schema. If the flag has the object, set
// "clientSide" to the value of "usingEnvironmentId". The SDK then writes the two properties
// with the same value, for SDKs that read only "clientSide".
if (clientSideAvailability != null) {
clientSide = clientSideAvailability.isUsingEnvironmentId();
}
DataModelPreprocessing.preprocessFlag(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class DataModelPreprocessingTest {

private FeatureFlag flagFromClause(Clause c) {
return new FeatureFlag("key", 0, false, null, null, null, null, rulesFromClause(c),
null, null, null, false, false, false, null, false, null, null, false);
null, null, null, false, null, false, false, null, false, null, null, false);
}

private List<Rule> rulesFromClause(Clause c) {
Expand All @@ -54,7 +54,7 @@ public void preprocessFlagAddsPrecomputedOffResult() {
ImmutableList.of(), null,
0,
ImmutableList.of(aValue, bValue),
false, false, false, null, false, null, null, false);
false, null, false, false, null, false, null, null, false);

f.afterDeserialized();

Expand All @@ -69,7 +69,7 @@ public void preprocessFlagAddsPrecomputedOffResultForNullOffVariation() {
ImmutableList.of(), null,
null,
ImmutableList.of(aValue, bValue),
false, false, false, null, false, null, null, false);
false, null, false, false, null, false, null, null, false);

f.afterDeserialized();

Expand All @@ -83,7 +83,7 @@ public void preprocessFlagAddsPrecomputedFallthroughResults() {
FeatureFlag f = new FeatureFlag("key", 0, false, null, null, null, null,
ImmutableList.of(), null, 0,
ImmutableList.of(aValue, bValue),
false, false, false, null, false, null, null, false);
false, null, false, false, null, false, null, null, false);

f.afterDeserialized();

Expand All @@ -109,7 +109,7 @@ public void preprocessFlagAddsPrecomputedTargetMatchResults() {
ImmutableList.of(new Target(null, ImmutableSet.of(), 1)),
null, ImmutableList.of(), null, 0,
ImmutableList.of(aValue, bValue),
false, false, false, null, false, null, null, false);
false, null, false, false, null, false, null, null, false);

f.afterDeserialized();

Expand All @@ -126,7 +126,7 @@ public void preprocessFlagAddsPrecomputedPrerequisiteFailedResults() {
null, null, null,
ImmutableList.of(), null, 0,
ImmutableList.of(aValue, bValue),
false, false, false, null, false, null, null, false);
false, null, false, false, null, false, null, null, false);

f.afterDeserialized();

Expand All @@ -150,7 +150,7 @@ public void preprocessFlagAddsPrecomputedResultsToFlagRulesWithRollout() {
ImmutableList.of(new Rule("ruleid0", ImmutableList.of(), null, rollout, false)),
null, null,
ImmutableList.of(aValue, bValue),
false, false, false, null, false, null, null, false);
false, null, false, false, null, false, null, null, false);

f.afterDeserialized();

Expand All @@ -177,7 +177,7 @@ public void preprocessFlagAddsPrecomputedResultsToFlagRulesWithJustVariation() {
ImmutableList.of(new Rule("ruleid0", ImmutableList.of(), 0, null, false)),
null, null,
ImmutableList.of(aValue, bValue),
false, false, false, null, false, null, null, false);
false, null, false, false, null, false, null, null, false);

f.afterDeserialized();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,101 @@ public void explicitNullsForOptionalStringAndBooleanPropertiesAreTreatedAsOmitte
});
}

@Test
public void flagWithClientSideAvailabilityIsDeserializedAndSerialized() {
boolean[] values = { false, true };
for (boolean usingMobileKey: values) {
for (boolean usingEnvironmentId: values) {
LDValue flagJson = baseBuilder("flag-key")
.put("clientSide", usingEnvironmentId)
.put("clientSideAvailability", LDValue.buildObject()
.put("usingMobileKey", usingMobileKey)
.put("usingEnvironmentId", usingEnvironmentId)
.build())
.build();
assertFlagFromJson(flagJson, flag -> {
assertNotNull(flag.getClientSideAvailability());
assertEquals(usingMobileKey, flag.getClientSideAvailability().isUsingMobileKey());
assertEquals(usingEnvironmentId, flag.getClientSideAvailability().isUsingEnvironmentId());
assertEquals(usingEnvironmentId, flag.isClientSide());

LDValue json1 = LDValue.parse(FEATURES.serialize(new ItemDescriptor(flag.getVersion(), flag)));
assertJsonEquals(flagJson.get("clientSideAvailability").toJsonString(),
json1.get("clientSideAvailability").toJsonString());
assertEquals(LDValue.of(usingEnvironmentId), json1.get("clientSide"));
});
}
}
}

@Test
public void flagWithClientSideAvailabilityTakesClientSideFromUsingEnvironmentId() {
LDValue flagJson = baseBuilder("flag-key")
.put("clientSide", true)
.put("clientSideAvailability", LDValue.buildObject()
.put("usingMobileKey", true)
.put("usingEnvironmentId", false)
.build())
.build();
assertFlagFromJson(flagJson, flag -> {
// The object has precedence over the "clientSide" value that the flag JSON has.
assertFalse(flag.isClientSide());

LDValue json1 = LDValue.parse(FEATURES.serialize(new ItemDescriptor(flag.getVersion(), flag)));
assertEquals(LDValue.of(false), json1.get("clientSide"));
assertEquals(LDValue.of(false), json1.get("clientSideAvailability").get("usingEnvironmentId"));
});
}

@Test
public void flagWithClientSideAvailabilityMissingPropertyReadsItAsFalse() {
LDValue flagJson = baseBuilder("flag-key")
.put("clientSide", true)
.put("clientSideAvailability", LDValue.buildObject()
.put("usingEnvironmentId", true)
.build())
.build();
assertFlagFromJson(flagJson, flag -> {
assertNotNull(flag.getClientSideAvailability());
assertFalse(flag.getClientSideAvailability().isUsingMobileKey());
assertTrue(flag.getClientSideAvailability().isUsingEnvironmentId());

LDValue json1 = LDValue.parse(FEATURES.serialize(new ItemDescriptor(flag.getVersion(), flag)));
assertJsonEquals("{\"usingMobileKey\":false,\"usingEnvironmentId\":true}",
json1.get("clientSideAvailability").toJsonString());
assertEquals(LDValue.of(true), json1.get("clientSide"));
});
}

@Test
public void flagWithoutClientSideAvailabilityIsSerializedWithClientSideOnly() {
boolean[] values = { false, true };
for (boolean clientSide: values) {
LDValue absentJson = baseBuilder("flag-key").put("clientSide", clientSide).build();
LDValue nullJson = baseBuilder("flag-key").put("clientSide", clientSide)
.put("clientSideAvailability", LDValue.ofNull()).build();
for (LDValue flagJson: new LDValue[] { absentJson, nullJson }) {
assertFlagFromJson(flagJson, flag -> {
assertNull(flag.getClientSideAvailability());
assertEquals(clientSide, flag.isClientSide());

String json1 = FEATURES.serialize(new ItemDescriptor(flag.getVersion(), flag));
assertFalse(json1.contains("clientSideAvailability"));
assertTrue(json1.contains("\"clientSide\":" + clientSide));
});
}
}
}

@Test
public void flagWithClientSideAvailabilityFromBuilderIsSerialized() {
FeatureFlag flag = flagBuilder("flag-key").clientSideAvailability(false, true).build();
LDValue json = LDValue.parse(serialize(flag));
assertJsonEquals("{\"usingMobileKey\":false,\"usingEnvironmentId\":true}",
json.get("clientSideAvailability").toJsonString());
assertEquals(LDValue.of(true), json.get("clientSide"));
}

@Test
public void parsingFullDataSetEmptyObject() throws Exception {
String json = "{}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private MigrationOpTracker trackerWithFlag(EvaluationDetail<String> detail, LDCo
"test-key",
new DataModel.FeatureFlag("flag", 2, true, null,
"salt", null, null, null, null, 0, null,
false, false, false, null, false,
false, null, false, false, null, false,
5l, null, false),
detail,
MigrationStage.OFF,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public static class FlagBuilder {
private Integer offVariation;
private List<LDValue> variations = new ArrayList<>();
private boolean clientSide;
private FeatureFlag.ClientSideAvailability clientSideAvailability;
private boolean trackEvents;
private boolean trackEventsFallthrough;
private Long debugEventsUntilDate;
Expand Down Expand Up @@ -173,6 +174,7 @@ private FlagBuilder(DataModel.FeatureFlag f) {
this.offVariation = f.getOffVariation();
this.variations = f.getVariations();
this.clientSide = f.isClientSide();
this.clientSideAvailability = f.getClientSideAvailability();
this.trackEvents = f.isTrackEvents();
this.trackEventsFallthrough = f.isTrackEventsFallthrough();
this.debugEventsUntilDate = f.getDebugEventsUntilDate();
Expand Down Expand Up @@ -297,6 +299,11 @@ public FlagBuilder clientSide(boolean clientSide) {
return this;
}

public FlagBuilder clientSideAvailability(boolean usingMobileKey, boolean usingEnvironmentId) {
this.clientSideAvailability = new FeatureFlag.ClientSideAvailability(usingMobileKey, usingEnvironmentId);
return this;
}

public FlagBuilder trackEvents(boolean trackEvents) {
this.trackEvents = trackEvents;
return this;
Expand Down Expand Up @@ -335,7 +342,7 @@ public FlagBuilder migration(FeatureFlag.Migration migration) {
public FeatureFlag build() {
FeatureFlag flag = new DataModel.FeatureFlag(key, version, on, prerequisites, salt, targets,
contextTargets, rules, fallthrough, offVariation, variations,
clientSide, trackEvents, trackEventsFallthrough, debugEventsUntilDate, deleted,
clientSide, clientSideAvailability, trackEvents, trackEventsFallthrough, debugEventsUntilDate, deleted,
samplingRatio, migration, excludeFromSummaries);
if (!disablePreprocessing) {
flag.afterDeserialized();
Expand Down