Skip to content

feat(decide): clone user-context before calling optimizely decide #417

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

Merged
merged 2 commits into from
Jan 11, 2021
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
@@ -1,6 +1,6 @@
/**
*
* Copyright 2020, Optimizely and contributors
* Copyright 2020-2021, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,13 +61,17 @@ public String getUserId() {
}

public Map<String, Object> getAttributes() {
return new HashMap<String, Object>(attributes);
return attributes;
}

public Optimizely getOptimizely() {
return optimizely;
}

public OptimizelyUserContext copy() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about implementing CloneableInterface

return new OptimizelyUserContext(optimizely, userId, attributes);
}

/**
* Set an attribute for a given key.
*
Expand All @@ -89,7 +93,7 @@ public void setAttribute(@Nonnull String key, @Nullable Object value) {
*/
public OptimizelyDecision decide(@Nonnull String key,
@Nonnull List<OptimizelyDecideOption> options) {
return optimizely.decide(this, key, options);
return optimizely.decide(copy(), key, options);
}

/**
Expand All @@ -114,7 +118,7 @@ public OptimizelyDecision decide(@Nonnull String key) {
*/
public Map<String, OptimizelyDecision> decideForKeys(@Nonnull List<String> keys,
@Nonnull List<OptimizelyDecideOption> options) {
return optimizely.decideForKeys(this, keys, options);
return optimizely.decideForKeys(copy(), keys, options);
}

/**
Expand All @@ -134,7 +138,7 @@ public Map<String, OptimizelyDecision> decideForKeys(@Nonnull List<String> keys)
* @return All decision results mapped by flag keys.
*/
public Map<String, OptimizelyDecision> decideAll(@Nonnull List<OptimizelyDecideOption> options) {
return optimizely.decideAll(this, options);
return optimizely.decideAll(copy(), options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2020, Optimizely and contributors
* Copyright 2020-2021, Optimizely and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,23 +26,44 @@
import java.util.List;

public class OptimizelyDecision {
/**
* The variation key of the decision. This value will be null when decision making fails.
*/
@Nullable
private final String variationKey;

/**
* The boolean value indicating if the flag is enabled or not.
*/
private final boolean enabled;

/**
* The collection of variables associated with the decision.
*/
@Nonnull
private final OptimizelyJSON variables;

/**
* The rule key of the decision.
*/
@Nullable
private final String ruleKey;

/**
* The flag key for which the decision has been made for.
*/
@Nonnull
private final String flagKey;

/**
* A copy of the user context for which the decision has been made for.
*/
@Nonnull
private final OptimizelyUserContext userContext;

/**
* An array of error/info messages describing why the decision has been made.
*/
@Nonnull
private List<String> reasons;

Expand Down