|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.Immutable; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using OpenFeature.Constant; |
| 7 | +using OpenFeature.Error; |
| 8 | +using OpenFeature.Model; |
| 9 | + |
| 10 | +#nullable enable |
| 11 | +namespace OpenFeature.Providers.Memory |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// The in memory provider. |
| 15 | + /// Useful for testing and demonstration purposes. |
| 16 | + /// </summary> |
| 17 | + /// <seealso href="https://openfeature.dev/specification/appendix-a#in-memory-provider">In Memory Provider specification</seealso> |
| 18 | + public class InMemoryProvider : FeatureProvider |
| 19 | + { |
| 20 | + |
| 21 | + private readonly Metadata _metadata = new Metadata("InMemory"); |
| 22 | + |
| 23 | + private Dictionary<string, Flag> _flags; |
| 24 | + |
| 25 | + /// <inheritdoc/> |
| 26 | + public override Metadata GetMetadata() |
| 27 | + { |
| 28 | + return this._metadata; |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Construct a new InMemoryProvider. |
| 33 | + /// </summary> |
| 34 | + /// <param name="flags">dictionary of Flags</param> |
| 35 | + public InMemoryProvider(IDictionary<string, Flag>? flags = null) |
| 36 | + { |
| 37 | + if (flags == null) |
| 38 | + { |
| 39 | + this._flags = new Dictionary<string, Flag>(); |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + this._flags = new Dictionary<string, Flag>(flags); // shallow copy |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Updating provider flags configuration, replacing all flags. |
| 49 | + /// </summary> |
| 50 | + /// <param name="flags">the flags to use instead of the previous flags.</param> |
| 51 | + public async ValueTask UpdateFlags(IDictionary<string, Flag>? flags = null) |
| 52 | + { |
| 53 | + var changed = this._flags.Keys.ToList(); |
| 54 | + if (flags == null) |
| 55 | + { |
| 56 | + this._flags = new Dictionary<string, Flag>(); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + this._flags = new Dictionary<string, Flag>(flags); // shallow copy |
| 61 | + } |
| 62 | + changed.AddRange(this._flags.Keys.ToList()); |
| 63 | + var @event = new ProviderEventPayload |
| 64 | + { |
| 65 | + Type = ProviderEventTypes.ProviderConfigurationChanged, |
| 66 | + ProviderName = _metadata.Name, |
| 67 | + FlagsChanged = changed, // emit all |
| 68 | + Message = "flags changed", |
| 69 | + }; |
| 70 | + await this.EventChannel.Writer.WriteAsync(@event).ConfigureAwait(false); |
| 71 | + } |
| 72 | + |
| 73 | + /// <inheritdoc/> |
| 74 | + public override Task<ResolutionDetails<bool>> ResolveBooleanValue( |
| 75 | + string flagKey, |
| 76 | + bool defaultValue, |
| 77 | + EvaluationContext? context = null) |
| 78 | + { |
| 79 | + return Task.FromResult(Resolve(flagKey, defaultValue, context)); |
| 80 | + } |
| 81 | + |
| 82 | + /// <inheritdoc/> |
| 83 | + public override Task<ResolutionDetails<string>> ResolveStringValue( |
| 84 | + string flagKey, |
| 85 | + string defaultValue, |
| 86 | + EvaluationContext? context = null) |
| 87 | + { |
| 88 | + return Task.FromResult(Resolve(flagKey, defaultValue, context)); |
| 89 | + } |
| 90 | + |
| 91 | + /// <inheritdoc/> |
| 92 | + public override Task<ResolutionDetails<int>> ResolveIntegerValue( |
| 93 | + string flagKey, |
| 94 | + int defaultValue, |
| 95 | + EvaluationContext? context = null) |
| 96 | + { |
| 97 | + return Task.FromResult(Resolve(flagKey, defaultValue, context)); |
| 98 | + } |
| 99 | + |
| 100 | + /// <inheritdoc/> |
| 101 | + public override Task<ResolutionDetails<double>> ResolveDoubleValue( |
| 102 | + string flagKey, |
| 103 | + double defaultValue, |
| 104 | + EvaluationContext? context = null) |
| 105 | + { |
| 106 | + return Task.FromResult(Resolve(flagKey, defaultValue, context)); |
| 107 | + } |
| 108 | + |
| 109 | + /// <inheritdoc/> |
| 110 | + public override Task<ResolutionDetails<Value>> ResolveStructureValue( |
| 111 | + string flagKey, |
| 112 | + Value defaultValue, |
| 113 | + EvaluationContext? context = null) |
| 114 | + { |
| 115 | + return Task.FromResult(Resolve(flagKey, defaultValue, context)); |
| 116 | + } |
| 117 | + |
| 118 | + private ResolutionDetails<T> Resolve<T>(string flagKey, T defaultValue, EvaluationContext? context) |
| 119 | + { |
| 120 | + if (!this._flags.TryGetValue(flagKey, out var flag)) |
| 121 | + { |
| 122 | + throw new FlagNotFoundException($"flag {flagKey} not found"); |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + // This check returns False if a floating point flag is evaluated as an integer flag, and vice-versa. |
| 127 | + // In a production provider, such behavior is probably not desirable; consider supporting conversion. |
| 128 | + if (typeof(Flag<T>).Equals(flag.GetType())) |
| 129 | + { |
| 130 | + return ((Flag<T>)flag).Evaluate(flagKey, defaultValue, context); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + throw new TypeMismatchException($"flag {flagKey} is not of type ${typeof(T)}"); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments