-
Notifications
You must be signed in to change notification settings - Fork 21
feat!: Context Values support, getEvaluationResult
#184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 22 commits
14bf7cc
da5b62b
f989a8c
05abbe9
9f25722
8be5116
59583be
c55c339
f72f1ff
e697b61
4dcdaed
7be7e3d
c4750b4
15155e6
bf4dddf
9d4b0d8
afdaec0
6a7e1c3
c742ec3
89f420a
d4e5b1e
d99e9d1
b861a72
4730f9a
bebac37
c3a8811
bef6681
b0ad7d7
d12ef3f
ec6000e
db248dd
c4d35e2
0e827fc
3819cd9
de0362b
b8ae5ab
7764097
7303aeb
c37164b
50b4bec
b4ee7ff
540832f
60d5d9e
e7cfad5
c5821d9
711ff13
27b4b8e
000e96c
211c5bd
8389a90
69f00a4
b5889e8
3a05d8b
ed5e516
23799be
988d59b
2dfcbbc
183cdf9
7d1c948
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| [submodule "src/test/java/com/flagsmith/flagengine/enginetestdata"] | ||
| path = src/test/java/com/flagsmith/flagengine/enginetestdata | ||
| url = [email protected]:Flagsmith/engine-test-data.git | ||
| branch = v1.0.0 | ||
| branch = feat/context-values | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,21 +6,16 @@ | |||||
| import com.flagsmith.exceptions.FlagsmithClientError; | ||||||
| import com.flagsmith.exceptions.FlagsmithRuntimeError; | ||||||
| import com.flagsmith.flagengine.Engine; | ||||||
| import com.flagsmith.flagengine.environments.EnvironmentModel; | ||||||
| import com.flagsmith.flagengine.features.FeatureStateModel; | ||||||
| import com.flagsmith.flagengine.identities.IdentityModel; | ||||||
| import com.flagsmith.flagengine.identities.traits.TraitModel; | ||||||
| import com.flagsmith.flagengine.segments.SegmentEvaluator; | ||||||
| import com.flagsmith.flagengine.segments.SegmentModel; | ||||||
| import com.flagsmith.flagengine.EvaluationContext; | ||||||
| import com.flagsmith.flagengine.EvaluationResult; | ||||||
| import com.flagsmith.interfaces.FlagsmithCache; | ||||||
| import com.flagsmith.interfaces.FlagsmithSdk; | ||||||
| import com.flagsmith.mappers.EngineMappers; | ||||||
| import com.flagsmith.models.BaseFlag; | ||||||
| import com.flagsmith.models.Flags; | ||||||
| import com.flagsmith.models.SdkTraitModel; | ||||||
| import com.flagsmith.models.Segment; | ||||||
| import com.flagsmith.threads.PollingManager; | ||||||
| import com.flagsmith.utils.ModelUtils; | ||||||
| import java.util.ArrayList; | ||||||
| import java.util.HashMap; | ||||||
| import java.util.List; | ||||||
| import java.util.Map; | ||||||
|
|
@@ -39,9 +34,8 @@ public class FlagsmithClient { | |||||
|
|
||||||
| private final FlagsmithLogger logger = new FlagsmithLogger(); | ||||||
| private FlagsmithSdk flagsmithSdk; | ||||||
| private EnvironmentModel environment; | ||||||
| private EvaluationContext evaluationContext; | ||||||
| private PollingManager pollingManager; | ||||||
| private Map<String, IdentityModel> identitiesWithOverridesByIdentifier; | ||||||
|
|
||||||
| private FlagsmithClient() { | ||||||
| } | ||||||
|
|
@@ -55,22 +49,12 @@ public static FlagsmithClient.Builder newBuilder() { | |||||
| */ | ||||||
| public void updateEnvironment() { | ||||||
| try { | ||||||
| EnvironmentModel updatedEnvironment = flagsmithSdk.getEnvironment(); | ||||||
| EvaluationContext updatedEvaluationContext = flagsmithSdk.getEvaluationContext(); | ||||||
|
|
||||||
| // if we didn't get an environment from the API, | ||||||
| // then don't overwrite the copy we already have. | ||||||
| if (updatedEnvironment != null) { | ||||||
| List<IdentityModel> identityOverrides = updatedEnvironment.getIdentityOverrides(); | ||||||
|
|
||||||
| if (identityOverrides != null) { | ||||||
| Map<String, IdentityModel> identitiesWithOverridesByIdentifier = new HashMap<>(); | ||||||
| for (IdentityModel identity : identityOverrides) { | ||||||
| identitiesWithOverridesByIdentifier.put(identity.getIdentifier(), identity); | ||||||
| } | ||||||
| this.identitiesWithOverridesByIdentifier = identitiesWithOverridesByIdentifier; | ||||||
| } | ||||||
|
|
||||||
| this.environment = updatedEnvironment; | ||||||
| if (updatedEvaluationContext != null) { | ||||||
| this.evaluationContext = updatedEvaluationContext; | ||||||
| } else { | ||||||
| logger.error(getEnvironmentUpdateErrorMessage()); | ||||||
| } | ||||||
|
|
@@ -150,13 +134,10 @@ public Flags getIdentityFlags(String identifier, Map<String, Object> traits) | |||||
| public Flags getIdentityFlags(String identifier, Map<String, Object> traits, boolean isTransient) | ||||||
| throws FlagsmithClientError { | ||||||
| if (getShouldUseEnvironmentDocument()) { | ||||||
| return getIdentityFlagsFromDocument( | ||||||
| identifier, | ||||||
| ModelUtils.getTraitModelsFromTraitMap(traits)); | ||||||
| return getIdentityFlagsFromDocument(identifier, traits); | ||||||
| } | ||||||
|
|
||||||
| return getIdentityFlagsFromApi( | ||||||
| identifier, ModelUtils.getSdkTraitModelsFromTraitMap(traits), isTransient); | ||||||
| return getIdentityFlagsFromApi(identifier, traits, isTransient); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -180,20 +161,18 @@ public List<Segment> getIdentitySegments(String identifier) | |||||
| */ | ||||||
| public List<Segment> getIdentitySegments(String identifier, Map<String, Object> traits) | ||||||
| throws FlagsmithClientError { | ||||||
| if (environment == null) { | ||||||
| if (evaluationContext == null) { | ||||||
| throw new FlagsmithClientError("Local evaluation required to obtain identity segments."); | ||||||
| } | ||||||
| IdentityModel identityModel = getIdentityModel( | ||||||
| identifier, | ||||||
| (traits != null | ||||||
| ? ModelUtils.getTraitModelsFromTraitMap(traits) | ||||||
| : new ArrayList<TraitModel>())); | ||||||
| List<SegmentModel> segmentModels = SegmentEvaluator.getIdentitySegments( | ||||||
| environment, identityModel); | ||||||
|
|
||||||
| return segmentModels.stream().map((segmentModel) -> { | ||||||
|
|
||||||
| final EvaluationContext context = EngineMappers.mapContextAndIdentityDataToContext( | ||||||
| evaluationContext, identifier, traits); | ||||||
|
|
||||||
| final EvaluationResult result = Engine.getEvaluationResult(context); | ||||||
|
|
||||||
| return result.getSegments().stream().map((segmentModel) -> { | ||||||
| Segment segment = new Segment(); | ||||||
| segment.setId(segmentModel.getId()); | ||||||
| segment.setId(Integer.valueOf(segmentModel.getKey())); | ||||||
| segment.setName(segmentModel.getName()); | ||||||
|
|
||||||
| return segment; | ||||||
|
|
@@ -212,37 +191,39 @@ public void close() { | |||||
| } | ||||||
|
|
||||||
| private Flags getEnvironmentFlagsFromDocument() throws FlagsmithClientError { | ||||||
|
||||||
| private Flags getEnvironmentFlagsFromDocument() throws FlagsmithClientError { | |
| private Flags getEnvironmentFlagsFromLocalEvaluationContext() throws FlagsmithClientError { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice suggestion, done in 988d59b.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following our discussions, reminder to revert here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was referring to dropping the offline handler of the current scope and leaving it read the environment document and return it (no breaking change on that part for users)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should bump v2.0.0 in engine-test-data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in bebac37 (the branch is based on latest main).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making sure we're in the same page here: ultimately this need to be merged as a version tag, since feat/fix branches are ephemeral. If that's correct, I'll keep this thread unresolved as a reminder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now done.