-
Notifications
You must be signed in to change notification settings - Fork 21
feat: implement in-memory provider #232
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
16 commits
Select commit
Hold shift + click to select a range
f44ccb3
feat: implement in-memory provider
toddbaert 80471e4
fixup: spec path
toddbaert f5385ad
fixup: format
toddbaert 06281f2
Update src/OpenFeature/Providers/Memory/Flag.cs
toddbaert a6b4a4a
Update src/OpenFeature/Providers/Memory/Flag.cs
toddbaert 4fa6f78
Update src/OpenFeature/Providers/Memory/Flag.cs
toddbaert dc00422
Update src/OpenFeature/Providers/Memory/InMemoryProvider.cs
toddbaert b47b64b
fixup: pr feedback and tests
toddbaert 377eb1b
fixup: more tests
toddbaert 976bcfb
fixup: more tests
toddbaert 8549435
fixup: more tests
toddbaert 02b6e2b
fixup: more tests
toddbaert 2ee2a82
fixup: lint
toddbaert 30a3134
fixup: error message
toddbaert 6c70f15
fixup: add comment about conversion
toddbaert 70b7240
Merge branch 'main' into add-in-memory-provider
toddbaert 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
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 |
---|---|---|
@@ -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 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
File renamed without changes.
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,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 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,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; | ||
|
||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#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 | ||
askpt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
|
||
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())) | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 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
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.