Skip to content

Commit

Permalink
feat: remove Flagsmith targetingKey attribute
Browse files Browse the repository at this point in the history
Signed-off-by: ghelyar <[email protected]>
  • Loading branch information
ghelyar committed Jul 8, 2024
1 parent 781c1d4 commit fa62d1c
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 53 deletions.
14 changes: 1 addition & 13 deletions src/OpenFeature.Contrib.Providers.Flagsmith/FlagsmithProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,12 @@ public FlagsmithProvider(IFlagsmithProviderConfiguration providerOptions, IFlags

private Task<IFlags> GetFlags(EvaluationContext ctx)
{
string key = null;
if (ctx != null)
{
if (ctx.TargetingKey is string { Length: > 0 } targetingKey)
{
key = targetingKey;
}
else if (ctx.TryGetValue(Configuration.TargetingKey, out var value))
{
key = value?.AsString;
}
}
var key = ctx?.TargetingKey;

return string.IsNullOrEmpty(key)
? _flagsmithClient.GetEnvironmentFlags()
: _flagsmithClient.GetIdentityFlags(key, ctx
.AsDictionary()
.Where(x => x.Key != Configuration.TargetingKey)
.Select(x => new Trait(x.Key, x.Value.AsObject) as ITrait)
.ToList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
/// </summary>
public class FlagsmithProviderConfiguration : IFlagsmithProviderConfiguration
{
/// <summary>
/// Default value for targeting key
/// </summary>
public const string DefaultTargetingKey = "targetingKey";

/// <summary>
/// Key that will be used as identity for Flagsmith requests. Default: "targetingKey"
/// </summary>
public string TargetingKey { get; set; } = DefaultTargetingKey;

/// <inheritdoc/>
public bool UsingBooleanConfigValue { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ namespace OpenFeature.Contrib.Providers.Flagsmith;
/// </summary>
public interface IFlagsmithProviderConfiguration
{
/// <summary>
/// Key that will be used as identity for Flagsmith requests.
/// </summary>
public string TargetingKey { get; }

/// <summary>
/// Determines whether to resolve a feature value as a boolean or use
/// the isFeatureEnabled as the flag itself. These values will be false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task GetValue_ForEnabledFeatureWithEvaluationContext_ReturnCorrectV
.Set("key4", date)
.Set("key5", Structure.Empty)
.Set("key6", 1.0)
.Set(FlagsmithProviderConfiguration.DefaultTargetingKey, "233");
.SetTargetingKey("233");
// Act
var result = await flagsmithProvider.ResolveBooleanValue("example-feature", false, contextBuilder.Build());

Expand Down Expand Up @@ -449,29 +449,5 @@ public async Task GetStructureValue_ForEnabledFeatureWithWrongFormatValue_Throws
// Act and Assert
await Assert.ThrowsAsync<TypeMismatchException>(() => flagsmithProvider.ResolveStructureValue("example-feature", defaultObject));
}

[Theory]
[InlineData("property", "attribute", "property")]
[InlineData(null, "attribute", "attribute")]
[InlineData("", "attribute", "attribute")]
[InlineData("property", null, "property")]
[InlineData("property", "", "property")]
public async Task GetValue_WithTargetingKey_UsesPropertyOverAttribute(string property, string attribute, string expected)
{
// Arrange
var flagsmithClient = Substitute.For<IFlagsmithClient>();
var providerConfig = GetDefaultFlagsmithProviderConfigurationConfiguration();
var flagsmithProvider = new FlagsmithProvider(providerConfig, flagsmithClient);

var contextBuilder = EvaluationContext.Builder()
.SetTargetingKey(property)
.Set(FlagsmithProviderConfiguration.DefaultTargetingKey, attribute);

// Act
await flagsmithProvider.ResolveBooleanValue("example-feature", false, contextBuilder.Build());

// Assert
await flagsmithClient.Received().GetIdentityFlags(expected, Arg.Any<List<ITrait>>());
}
}
}

0 comments on commit fa62d1c

Please sign in to comment.