-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: use evaluation context interface (#112)
* POC - use evaluation context interface Signed-off-by: Todd Baert <[email protected]> * make .merge non-static Signed-off-by: Todd Baert <[email protected]> * improve naming Signed-off-by: Todd Baert <[email protected]> * add @OverRide Signed-off-by: Todd Baert <[email protected]> * Update src/main/java/dev/openfeature/sdk/EvaluationContext.java Co-authored-by: Justin Abrahms <[email protected]> Signed-off-by: Todd Baert <[email protected]> * Update src/main/java/dev/openfeature/sdk/MutableContext.java Co-authored-by: Justin Abrahms <[email protected]> Signed-off-by: Todd Baert <[email protected]> * address PR feedback Signed-off-by: Todd Baert <[email protected]> Signed-off-by: Todd Baert <[email protected]> Co-authored-by: Justin Abrahms <[email protected]>
- Loading branch information
1 parent
3788a3b
commit e9732b5
Showing
17 changed files
with
483 additions
and
405 deletions.
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 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 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 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.