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
5 changes: 3 additions & 2 deletions .github/workflows/sdk-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ permissions:
jobs:
sdk-spec:
# Keep the ref in `uses:` and `spec-ref` in sync.
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.3.0
uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.7.0
with:
spec-ref: v0.3.0
spec-ref: v0.7.0
language: java
tags: core or local-evaluation
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[submodule "src/test/java/com/flagsmith/flagengine/enginetestdata"]
path = src/test/java/com/flagsmith/flagengine/enginetestdata
url = git@github.com:Flagsmith/engine-test-data.git
branch = v3.7.0
branch = v3.11.0
[submodule "third-party/codspeed-jvm"]
path = third-party/codspeed-jvm
url = https://github.com/CodSpeedHQ/codspeed-jvm.git
Expand Down
12 changes: 12 additions & 0 deletions sdk-spec/Steps.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import com.flagsmith.FlagsmithClient;
import com.flagsmith.config.FlagsmithConfig;
import com.flagsmith.exceptions.FlagsmithClientError;
import com.flagsmith.models.BaseFlag;
import com.flagsmith.models.Flags;
Expand Down Expand Up @@ -35,6 +36,17 @@ static FlagsmithClient createClient(String environmentKey, String apiUrl) {
return FlagsmithClient.newBuilder().setApiKey(environmentKey).withApiUrl(apiUrl).build();
}

// Create the SDK client with local evaluation
static FlagsmithClient createClientWithLocalEvaluation(String environmentKey, String apiUrl) {
return FlagsmithClient.newBuilder()
.setApiKey(environmentKey)
.withConfiguration(FlagsmithConfig.newBuilder()
.baseUri(apiUrl)
.withLocalEvaluation(true)
.build())
.build();
}

// When the application gets the environment flags
static List<SpecFlag> getEnvironmentFlags(FlagsmithClient flagsmith)
throws FlagsmithClientError {
Expand Down
65 changes: 57 additions & 8 deletions src/main/java/com/flagsmith/FlagsmithClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.flagsmith.flagengine.Engine;
import com.flagsmith.flagengine.EvaluationContext;
import com.flagsmith.flagengine.EvaluationResult;
import com.flagsmith.flagengine.SegmentRule;
import com.flagsmith.flagengine.Segments;
import com.flagsmith.interfaces.FlagsmithCache;
import com.flagsmith.interfaces.FlagsmithSdk;
import com.flagsmith.mappers.EngineMappers;
Expand All @@ -30,9 +32,11 @@
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -43,9 +47,17 @@
@Data
public class FlagsmithClient {

private static final List<String> IDENTITY_FREE_PROPERTY_PREFIXES = List.of(
"$.environment.", "$.environment[", "$.flags.", "$.flags[");

private final FlagsmithLogger logger = new FlagsmithLogger();
private FlagsmithSdk flagsmithSdk;
private EvaluationContext evaluationContext;
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
@EqualsAndHashCode.Exclude
@ToString.Exclude
private EvaluationContext environmentEvaluationContext;
private PollingManager pollingManager;
@Getter(AccessLevel.PACKAGE)
@Setter(AccessLevel.NONE)
Expand All @@ -58,6 +70,19 @@ public static FlagsmithClient.Builder newBuilder() {
return new FlagsmithClient.Builder();
}

/**
* Set the evaluation context, and derive the one environment flags evaluate against.
*
* @param evaluationContext the evaluation context
*/
public void setEvaluationContext(EvaluationContext evaluationContext) {
this.environmentEvaluationContext = evaluationContext == null
? null
: new EvaluationContext(evaluationContext)
.withSegments(getIdentityFreeSegments(evaluationContext.getSegments()));
this.evaluationContext = evaluationContext;
}

/**
* Load the environment flags in the environment variable from the API.
*/
Expand All @@ -68,7 +93,7 @@ public void updateEnvironment() {
// if we didn't get an environment from the API,
// then don't overwrite the copy we already have.
if (updatedEvaluationContext != null) {
this.evaluationContext = updatedEvaluationContext;
setEvaluationContext(updatedEvaluationContext);
} else {
logger.error(getEnvironmentUpdateErrorMessage());
}
Expand Down Expand Up @@ -411,24 +436,48 @@ private EventProcessor requireEventProcessor(String action) {
}

private Flags getEnvironmentFlagsFromEvaluationContext() throws FlagsmithClientError {
if (evaluationContext == null) {
final EvaluationContext context = environmentEvaluationContext;
if (context == null) {
if (getConfig().getFlagsmithFlagDefaults() == null) {
throw new FlagsmithClientError("Unable to get flags. No environment present.");
}
return getDefaultFlags();
}

final EvaluationResult result = Engine.getEvaluationResult(
new EvaluationContext(evaluationContext)
.withSegments(null)
);
final EvaluationResult result = Engine.getEvaluationResult(context);

return Flags.fromEvaluationResult(
result,
getConfig().getAnalyticsProcessor(),
getConfig().getFlagsmithFlagDefaults());
}

/*
* The segments that resolve without an identity, i.e. those whose every
* condition reads only the environment or other flags.
*/
private static Segments getIdentityFreeSegments(Segments segments) {
if (segments == null) {
return null;
}
Segments identityFreeSegments = new Segments();
segments.getAdditionalProperties().forEach((segmentKey, segmentContext) -> {
if (isIdentityFree(segmentContext.getRules())) {
identityFreeSegments.setAdditionalProperty(segmentKey, segmentContext);
}
});
return identityFreeSegments;
}

private static boolean isIdentityFree(List<SegmentRule> rules) {
return rules == null || rules.stream().allMatch(rule ->
(rule.getConditions() == null || rule.getConditions().stream().allMatch(condition ->
condition.getProperty() != null
&& IDENTITY_FREE_PROPERTY_PREFIXES.stream()
.anyMatch(condition.getProperty()::startsWith)))
&& isIdentityFree(rule.getRules()));
}

private Flags getIdentityFlagsFromEvaluationContext(
String identifier, Map<String, Object> traits)
throws FlagsmithClientError {
Expand Down Expand Up @@ -748,8 +797,8 @@ public FlagsmithClient build() {
throw new FlagsmithRuntimeError(
"Cannot use both default flag handler and offline handler.");
}
client.evaluationContext = EngineMappers.mapEnvironmentToContext(
configuration.getOfflineHandler().getEnvironment());
client.setEvaluationContext(EngineMappers.mapEnvironmentToContext(
configuration.getOfflineHandler().getEnvironment()));
}

EventProcessor processor = null;
Expand Down
Loading
Loading