forked from open-feature/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement in-memory provider (open-feature#232)
Implements in-memory provider as per spec, updates gherkin to use spec version, removes flagd deps. Signed-off-by: Todd Baert <[email protected]> Co-authored-by: Joris Goovaerts <[email protected]> Co-authored-by: André Silva <[email protected]> Signed-off-by: Artyom Tonoyan <[email protected]>
- Loading branch information
1 parent
d2c895b
commit 0449d1c
Showing
12 changed files
with
554 additions
and
29 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[submodule "test-harness"] | ||
path = test-harness | ||
url = https://github.com/open-feature/test-harness.git | ||
[submodule "spec"] | ||
path = spec | ||
url = https://github.com/open-feature/spec.git |
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
File renamed without changes.
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,78 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using OpenFeature.Constant; | ||
using OpenFeature.Error; | ||
using OpenFeature.Model; | ||
|
||
#nullable enable | ||
namespace OpenFeature.Providers.Memory | ||
{ | ||
/// <summary> | ||
/// Flag representation for the in-memory provider. | ||
/// </summary> | ||
public interface Flag | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Flag representation for the in-memory provider. | ||
/// </summary> | ||
public sealed class Flag<T> : Flag | ||
{ | ||
private Dictionary<string, T> Variants; | ||
private string DefaultVariant; | ||
private Func<EvaluationContext, string>? ContextEvaluator; | ||
|
||
/// <summary> | ||
/// Flag representation for the in-memory provider. | ||
/// </summary> | ||
/// <param name="variants">dictionary of variants and their corresponding values</param> | ||
/// <param name="defaultVariant">default variant (should match 1 key in variants dictionary)</param> | ||
/// <param name="contextEvaluator">optional context-sensitive evaluation function</param> | ||
public Flag(Dictionary<string, T> variants, string defaultVariant, Func<EvaluationContext, string>? contextEvaluator = null) | ||
{ | ||
this.Variants = variants; | ||
this.DefaultVariant = defaultVariant; | ||
this.ContextEvaluator = contextEvaluator; | ||
} | ||
|
||
internal ResolutionDetails<T> Evaluate(string flagKey, T _, EvaluationContext? evaluationContext) | ||
{ | ||
T? value = default; | ||
if (this.ContextEvaluator == null) | ||
{ | ||
if (this.Variants.TryGetValue(this.DefaultVariant, out value)) | ||
{ | ||
return new ResolutionDetails<T>( | ||
flagKey, | ||
value, | ||
variant: this.DefaultVariant, | ||
reason: Reason.Static | ||
); | ||
} | ||
else | ||
{ | ||
throw new GeneralException($"variant {this.DefaultVariant} not found"); | ||
} | ||
} | ||
else | ||
{ | ||
var variant = this.ContextEvaluator.Invoke(evaluationContext ?? EvaluationContext.Empty); | ||
if (!this.Variants.TryGetValue(variant, out value)) | ||
{ | ||
throw new GeneralException($"variant {variant} not found"); | ||
} | ||
else | ||
{ | ||
return new ResolutionDetails<T>( | ||
flagKey, | ||
value, | ||
variant: variant, | ||
reason: Reason.TargetingMatch | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,139 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using OpenFeature.Constant; | ||
using OpenFeature.Error; | ||
using OpenFeature.Model; | ||
|
||
#nullable enable | ||
namespace OpenFeature.Providers.Memory | ||
{ | ||
/// <summary> | ||
/// The in memory provider. | ||
/// Useful for testing and demonstration purposes. | ||
/// </summary> | ||
/// <seealso href="https://openfeature.dev/specification/appendix-a#in-memory-provider">In Memory Provider specification</seealso> | ||
public class InMemoryProvider : FeatureProvider | ||
{ | ||
|
||
private readonly Metadata _metadata = new Metadata("InMemory"); | ||
|
||
private Dictionary<string, Flag> _flags; | ||
|
||
/// <inheritdoc/> | ||
public override Metadata GetMetadata() | ||
{ | ||
return this._metadata; | ||
} | ||
|
||
/// <summary> | ||
/// Construct a new InMemoryProvider. | ||
/// </summary> | ||
/// <param name="flags">dictionary of Flags</param> | ||
public InMemoryProvider(IDictionary<string, Flag>? flags = null) | ||
{ | ||
if (flags == null) | ||
{ | ||
this._flags = new Dictionary<string, Flag>(); | ||
} | ||
else | ||
{ | ||
this._flags = new Dictionary<string, Flag>(flags); // shallow copy | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Updating provider flags configuration, replacing all flags. | ||
/// </summary> | ||
/// <param name="flags">the flags to use instead of the previous flags.</param> | ||
public async ValueTask UpdateFlags(IDictionary<string, Flag>? flags = null) | ||
{ | ||
var changed = this._flags.Keys.ToList(); | ||
if (flags == null) | ||
{ | ||
this._flags = new Dictionary<string, Flag>(); | ||
} | ||
else | ||
{ | ||
this._flags = new Dictionary<string, Flag>(flags); // shallow copy | ||
} | ||
changed.AddRange(this._flags.Keys.ToList()); | ||
var @event = new ProviderEventPayload | ||
{ | ||
Type = ProviderEventTypes.ProviderConfigurationChanged, | ||
ProviderName = _metadata.Name, | ||
FlagsChanged = changed, // emit all | ||
Message = "flags changed", | ||
}; | ||
await this.EventChannel.Writer.WriteAsync(@event).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override Task<ResolutionDetails<bool>> ResolveBooleanValue( | ||
string flagKey, | ||
bool defaultValue, | ||
EvaluationContext? context = null) | ||
{ | ||
return Task.FromResult(Resolve(flagKey, defaultValue, context)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override Task<ResolutionDetails<string>> ResolveStringValue( | ||
string flagKey, | ||
string defaultValue, | ||
EvaluationContext? context = null) | ||
{ | ||
return Task.FromResult(Resolve(flagKey, defaultValue, context)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override Task<ResolutionDetails<int>> ResolveIntegerValue( | ||
string flagKey, | ||
int defaultValue, | ||
EvaluationContext? context = null) | ||
{ | ||
return Task.FromResult(Resolve(flagKey, defaultValue, context)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override Task<ResolutionDetails<double>> ResolveDoubleValue( | ||
string flagKey, | ||
double defaultValue, | ||
EvaluationContext? context = null) | ||
{ | ||
return Task.FromResult(Resolve(flagKey, defaultValue, context)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override Task<ResolutionDetails<Value>> ResolveStructureValue( | ||
string flagKey, | ||
Value defaultValue, | ||
EvaluationContext? context = null) | ||
{ | ||
return Task.FromResult(Resolve(flagKey, defaultValue, context)); | ||
} | ||
|
||
private ResolutionDetails<T> Resolve<T>(string flagKey, T defaultValue, EvaluationContext? context) | ||
{ | ||
if (!this._flags.TryGetValue(flagKey, out var flag)) | ||
{ | ||
throw new FlagNotFoundException($"flag {flagKey} not found"); | ||
} | ||
else | ||
{ | ||
// This check returns False if a floating point flag is evaluated as an integer flag, and vice-versa. | ||
// In a production provider, such behavior is probably not desirable; consider supporting conversion. | ||
if (typeof(Flag<T>).Equals(flag.GetType())) | ||
{ | ||
return ((Flag<T>)flag).Evaluate(flagKey, defaultValue, context); | ||
} | ||
else | ||
{ | ||
throw new TypeMismatchException($"flag {flagKey} is not of type ${typeof(T)}"); | ||
} | ||
} | ||
} | ||
} | ||
} |
Submodule test-harness
deleted from
01c4a4
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
Oops, something went wrong.