-
Notifications
You must be signed in to change notification settings - Fork 46
feat!: use evaluation context interface #112
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
92b1473
POC - use evaluation context interface
toddbaert e70cd6f
make .merge non-static
toddbaert 097fc86
improve naming
toddbaert d559fbb
add @override
toddbaert ac639b6
Update src/main/java/dev/openfeature/sdk/EvaluationContext.java
toddbaert de84a57
Update src/main/java/dev/openfeature/sdk/MutableContext.java
toddbaert 700cf54
address PR feedback
toddbaert ab49d2b
Merge branch 'main' into poc/eval-context-interface
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 11 additions & 120 deletions
131
src/main/java/dev/openfeature/sdk/EvaluationContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,21 @@ | ||
package dev.openfeature.sdk; | ||
|
||
import java.time.Instant; | ||
import java.util.List; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import lombok.experimental.Delegate; | ||
|
||
@ToString | ||
/** | ||
* The EvaluationContext is a container for arbitrary contextual data | ||
* that can be used as a basis for dynamic evaluation. | ||
*/ | ||
@SuppressWarnings("PMD.BeanMembersShouldSerialize") | ||
public class EvaluationContext { | ||
|
||
@Setter @Getter private String targetingKey; | ||
@Delegate(excludes = HideDelegateAddMethods.class) private final Structure structure = new Structure(); | ||
|
||
public EvaluationContext() { | ||
super(); | ||
this.targetingKey = ""; | ||
} | ||
|
||
public EvaluationContext(String targetingKey) { | ||
this(); | ||
this.targetingKey = targetingKey; | ||
} | ||
public interface EvaluationContext extends Structure { | ||
String getTargetingKey(); | ||
|
||
void setTargetingKey(String targetingKey); | ||
|
||
/** | ||
* Merges two EvaluationContext objects with the second overriding the first in | ||
* Merges this EvaluationContext object with the second overriding the this in | ||
* case of conflict. | ||
* | ||
* @param ctx1 base context | ||
* @param ctx2 overriding context | ||
* @param overridingContext overriding context | ||
* @return resulting merged context | ||
*/ | ||
public static EvaluationContext merge(EvaluationContext ctx1, EvaluationContext ctx2) { | ||
EvaluationContext ec = new EvaluationContext(); | ||
if (ctx1 == null) { | ||
return ctx2; | ||
} else if (ctx2 == null) { | ||
return ctx1; | ||
} | ||
|
||
ec.structure.attributes.putAll(ctx1.structure.attributes); | ||
ec.structure.attributes.putAll(ctx2.structure.attributes); | ||
|
||
if (ctx1.getTargetingKey() != null && !ctx1.getTargetingKey().trim().equals("")) { | ||
ec.setTargetingKey(ctx1.getTargetingKey()); | ||
} | ||
|
||
if (ctx2.getTargetingKey() != null && !ctx2.getTargetingKey().trim().equals("")) { | ||
ec.setTargetingKey(ctx2.getTargetingKey()); | ||
} | ||
|
||
return ec; | ||
} | ||
|
||
// override @Delegate methods so that we can use "add" methods and still return EvaluationContext, not Structure | ||
public EvaluationContext add(String key, Boolean value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public EvaluationContext add(String key, String value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public EvaluationContext add(String key, Integer value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public EvaluationContext add(String key, Double value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public EvaluationContext add(String key, Instant value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public EvaluationContext add(String key, Structure value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public EvaluationContext add(String key, List<Value> value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
/** | ||
* Hidden class to tell Lombok not to copy these methods over via delegation. | ||
*/ | ||
private static class HideDelegateAddMethods { | ||
public Structure add(String ignoredKey, Boolean ignoredValue) { | ||
return null; | ||
} | ||
|
||
public Structure add(String ignoredKey, Double ignoredValue) { | ||
return null; | ||
} | ||
|
||
public Structure add(String ignoredKey, String ignoredValue) { | ||
return null; | ||
} | ||
|
||
public Structure add(String ignoredKey, Value ignoredValue) { | ||
return null; | ||
} | ||
|
||
public Structure add(String ignoredKey, Integer ignoredValue) { | ||
return null; | ||
} | ||
|
||
public Structure add(String ignoredKey, List<Value> ignoredValue) { | ||
return null; | ||
} | ||
|
||
public Structure add(String ignoredKey, Structure ignoredValue) { | ||
return null; | ||
} | ||
|
||
public Structure add(String ignoredKey, Instant ignoredValue) { | ||
return null; | ||
} | ||
} | ||
EvaluationContext merge(EvaluationContext overridingContext); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package dev.openfeature.sdk; | ||
|
||
import java.time.Instant; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import lombok.experimental.Delegate; | ||
|
||
/** | ||
* The EvaluationContext is a container for arbitrary contextual data | ||
* that can be used as a basis for dynamic evaluation. | ||
* The MutableContext is an EvaluationContext implementation which is not threadsafe, and whose attributes can | ||
* be modified after instantiation. | ||
*/ | ||
@ToString | ||
@SuppressWarnings("PMD.BeanMembersShouldSerialize") | ||
public class MutableContext implements EvaluationContext { | ||
|
||
@Setter() @Getter private String targetingKey; | ||
@Delegate(excludes = HideDelegateAddMethods.class) private final MutableStructure structure; | ||
|
||
public MutableContext() { | ||
this.structure = new MutableStructure(); | ||
this.targetingKey = ""; | ||
} | ||
|
||
public MutableContext(String targetingKey) { | ||
this(); | ||
this.targetingKey = targetingKey; | ||
} | ||
|
||
public MutableContext(Map<String, Value> attributes) { | ||
this.structure = new MutableStructure(attributes); | ||
this.targetingKey = ""; | ||
} | ||
|
||
public MutableContext(String targetingKey, Map<String, Value> attributes) { | ||
this(attributes); | ||
this.targetingKey = targetingKey; | ||
} | ||
|
||
// override @Delegate methods so that we can use "add" methods and still return MutableContext, not Structure | ||
public MutableContext add(String key, Boolean value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public MutableContext add(String key, String value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public MutableContext add(String key, Integer value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public MutableContext add(String key, Double value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public MutableContext add(String key, Instant value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public MutableContext add(String key, Structure value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
public MutableContext add(String key, List<Value> value) { | ||
this.structure.add(key, value); | ||
return this; | ||
} | ||
|
||
/** | ||
* Merges this EvaluationContext objects with the second overriding the this in | ||
* case of conflict. | ||
* | ||
* @param overridingContext overriding context | ||
* @return resulting merged context | ||
*/ | ||
@Override | ||
public EvaluationContext merge(EvaluationContext overridingContext) { | ||
if (overridingContext == null) { | ||
return new MutableContext(this.asMap()); | ||
} | ||
|
||
Map<String, Value> merged = new HashMap<String, Value>(); | ||
|
||
merged.putAll(this.asMap()); | ||
merged.putAll(overridingContext.asMap()); | ||
EvaluationContext ec = new MutableContext(merged); | ||
|
||
if (this.getTargetingKey() != null && !this.getTargetingKey().trim().equals("")) { | ||
ec.setTargetingKey(this.getTargetingKey()); | ||
} | ||
|
||
if (overridingContext.getTargetingKey() != null && !overridingContext.getTargetingKey().trim().equals("")) { | ||
ec.setTargetingKey(overridingContext.getTargetingKey()); | ||
} | ||
|
||
return ec; | ||
} | ||
|
||
/** | ||
* Hidden class to tell Lombok not to copy these methods over via delegation. | ||
*/ | ||
private static class HideDelegateAddMethods { | ||
public MutableStructure add(String ignoredKey, Boolean ignoredValue) { | ||
return null; | ||
} | ||
|
||
public MutableStructure add(String ignoredKey, Double ignoredValue) { | ||
return null; | ||
} | ||
|
||
public MutableStructure add(String ignoredKey, String ignoredValue) { | ||
return null; | ||
} | ||
|
||
public MutableStructure add(String ignoredKey, Value ignoredValue) { | ||
return null; | ||
} | ||
|
||
public MutableStructure add(String ignoredKey, Integer ignoredValue) { | ||
return null; | ||
} | ||
|
||
public MutableStructure add(String ignoredKey, List<Value> ignoredValue) { | ||
return null; | ||
} | ||
|
||
public MutableStructure add(String ignoredKey, MutableStructure ignoredValue) { | ||
return null; | ||
} | ||
|
||
public MutableStructure add(String ignoredKey, Instant ignoredValue) { | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.