-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Todd Baert <[email protected]> Co-authored-by: Joris Goovaerts <[email protected]>
- Loading branch information
Showing
12 changed files
with
356 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 = git@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; | ||
|
||
namespace OpenFeature.Providers.Memory | ||
{ | ||
/// <summary> | ||
/// Flag representation for the in-memory provider. | ||
/// </summary> | ||
public class Flag | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Flag representation for the in-memory provider. | ||
/// </summary> | ||
public 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 defaultValue, EvaluationContext evaluationContext) | ||
{ | ||
T value; | ||
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 | ||
{ | ||
string variant = this.ContextEvaluator.Invoke(evaluationContext); | ||
this.Variants.TryGetValue(variant, out value); | ||
if (value == null) | ||
{ | ||
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,126 @@ | ||
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; | ||
|
||
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) | ||
{ | ||
if (flags is null) | ||
throw new ArgumentNullException(nameof(flags)); | ||
this._flags = new Dictionary<string, Flag>(flags); // shallow copy | ||
var @event = new ProviderEventPayload | ||
{ | ||
Type = ProviderEventTypes.ProviderConfigurationChanged, | ||
ProviderName = _metadata.Name, | ||
FlagsChanged = flags.Keys.ToList(), // 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 {flag} not found"); | ||
} | ||
else | ||
{ | ||
if (typeof(Flag<T>).Equals(flag.GetType())) { | ||
return (flag as Flag<T>).Evaluate(flagKey, defaultValue, context); | ||
} else { | ||
throw new TypeMismatchException($"flag {flag} not found"); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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.