Skip to content

Commit

Permalink
Fixes #230
Browse files Browse the repository at this point in the history
Signed-off-by: Roelof Blom <[email protected]>
  • Loading branch information
roelofb committed Feb 7, 2024
1 parent cc6c404 commit 28fea57
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/OpenFeature/Model/EvaluationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@ public sealed class EvaluationContext
/// Internal constructor used by the builder.
/// </summary>
/// <param name="content">The content of the context.</param>
internal EvaluationContext(Structure content)
internal EvaluationContext(Structure content) : this(string.Empty, content)

Check warning on line 20 in src/OpenFeature/Model/EvaluationContext.cs

View check run for this annotation

Codecov / codecov/patch

src/OpenFeature/Model/EvaluationContext.cs#L20

Added line #L20 was not covered by tests
{
}

Check warning on line 22 in src/OpenFeature/Model/EvaluationContext.cs

View check run for this annotation

Codecov / codecov/patch

src/OpenFeature/Model/EvaluationContext.cs#L22

Added line #L22 was not covered by tests

/// <summary>
/// Internal constructor used by the builder.
/// </summary>
/// <param name="targetingKey">The targeting key</param>
/// <param name="content">The content of the context.</param>
internal EvaluationContext(string targetingKey, Structure content)
{
this.TargetingKey = targetingKey;
this._structure = content;
}

Expand All @@ -28,6 +38,7 @@ internal EvaluationContext(Structure content)
private EvaluationContext()
{
this._structure = Structure.Empty;
this.TargetingKey = string.Empty;
}

/// <summary>
Expand Down Expand Up @@ -83,6 +94,11 @@ public IImmutableDictionary<string, Value> AsDictionary()
/// </summary>
public int Count => this._structure.Count;

/// <summary>
/// Returns the targeting key for the context.
/// </summary>
public string TargetingKey { get; }

/// <summary>
/// Return an enumerator for all values
/// </summary>
Expand Down
32 changes: 31 additions & 1 deletion src/OpenFeature/Model/EvaluationContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,24 @@ public sealed class EvaluationContextBuilder
{
private readonly StructureBuilder _attributes = Structure.Builder();

internal string TargetingKey { get; private set; }

/// <summary>
/// Internal to only allow direct creation by <see cref="EvaluationContext.Builder()"/>.
/// </summary>
internal EvaluationContextBuilder() { }

/// <summary>
/// Set the targeting key for the context.
/// </summary>
/// <param name="targetingKey">The targeting key</param>
/// <returns>This builder</returns>
public EvaluationContextBuilder SetTargetingKey(string targetingKey)
{
this.TargetingKey = targetingKey;
return this;
}

/// <summary>
/// Set the key to the given <see cref="Value"/>.
/// </summary>
Expand Down Expand Up @@ -125,6 +138,23 @@ public EvaluationContextBuilder Set(string key, DateTime value)
/// <returns>This builder</returns>
public EvaluationContextBuilder Merge(EvaluationContext context)
{
string newTargetingKey = "";

if (TargetingKey != null && TargetingKey.Trim() != string.Empty)
{
newTargetingKey = TargetingKey;
}

if (context.TargetingKey != null && context.TargetingKey.Trim() != string.Empty)
{
newTargetingKey = context.TargetingKey;
}

if (newTargetingKey != null && newTargetingKey.Trim() != string.Empty)
{
this.TargetingKey = newTargetingKey;
}

foreach (var kvp in context)
{
this.Set(kvp.Key, kvp.Value);
Expand All @@ -139,7 +169,7 @@ public EvaluationContextBuilder Merge(EvaluationContext context)
/// <returns>An immutable <see cref="EvaluationContext"/></returns>
public EvaluationContext Build()
{
return new EvaluationContext(this._attributes.Build());
return new EvaluationContext(this.TargetingKey, this._attributes.Build());
}
}
}
37 changes: 36 additions & 1 deletion test/OpenFeature.Tests/OpenFeatureEvaluationContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,42 @@ public void Should_Merge_Two_Contexts()
.Set("key1", "value1");
var contextBuilder2 = new EvaluationContextBuilder()
.Set("key2", "value2");

var context1 = contextBuilder1.Merge(contextBuilder2.Build()).Build();

Assert.Equal(2, context1.Count);
Assert.Equal("value1", context1.GetValue("key1").AsString);
Assert.Equal("value2", context1.GetValue("key2").AsString);
}

[Fact]
public void Should_Change_TargetingKey_From_OverridingContext()
{
var contextBuilder1 = new EvaluationContextBuilder()
.Set("key1", "value1")
.SetTargetingKey("targeting_key");
var contextBuilder2 = new EvaluationContextBuilder()
.Set("key2", "value2")
.SetTargetingKey("overriding_key");

var mergeContext = contextBuilder1.Merge(contextBuilder2.Build()).Build();

Assert.Equal("overriding_key", mergeContext.TargetingKey);
}

[Fact]
public void Should_Retain_TargetingKey_When_OverridingContext_TargetingKey_Value_IsEmpty()
{
var contextBuilder1 = new EvaluationContextBuilder()
.Set("key1", "value1")
.SetTargetingKey("targeting_key");
var contextBuilder2 = new EvaluationContextBuilder()
.Set("key2", "value2");

var mergeContext = contextBuilder1.Merge(contextBuilder2.Build()).Build();

Assert.Equal("targeting_key", mergeContext.TargetingKey);
}

[Fact]
[Specification("3.2.2", "Evaluation context MUST be merged in the order: API (global; lowest precedence) - client - invocation - before hooks (highest precedence), with duplicate values being overwritten.")]
public void Should_Merge_TwoContexts_And_Override_Duplicates_With_RightHand_Context()
Expand Down Expand Up @@ -51,6 +79,8 @@ public void EvaluationContext_Should_All_Types()
var now = fixture.Create<DateTime>();
var structure = fixture.Create<Structure>();
var contextBuilder = new EvaluationContextBuilder()
.SetTargetingKey("targeting_key")
.Set("targeting_key", "userId")
.Set("key1", "value")
.Set("key2", 1)
.Set("key3", true)
Expand All @@ -60,6 +90,11 @@ public void EvaluationContext_Should_All_Types()

var context = contextBuilder.Build();

context.TargetingKey.Should().Be("targeting_key");
var targetingKeyValue = context.GetValue(context.TargetingKey);
targetingKeyValue.IsString.Should().BeTrue();
targetingKeyValue.AsString.Should().Be("userId");

var value1 = context.GetValue("key1");
value1.IsString.Should().BeTrue();
value1.AsString.Should().Be("value");
Expand Down

0 comments on commit 28fea57

Please sign in to comment.