-
Notifications
You must be signed in to change notification settings - Fork 27
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
feat: Update in-process resolver to support flag metadata #305 #309
Merged
toddbaert
merged 10 commits into
open-feature:main
from
chrfwow:Update-in-process-resolver-to-support-flag-metadata-#305
Jan 27, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
79062cb
Update in-process resolver to support flag metadata #305
chrfwow 934e580
fixup! Update in-process resolver to support flag metadata #305
chrfwow 0ff89cd
fixup! Update in-process resolver to support flag metadata #305
chrfwow 8ec5889
fixup! Update in-process resolver to support flag metadata #305
chrfwow 55609f5
fixup! Update in-process resolver to support flag metadata #305
chrfwow 6b5210d
fixup! Update in-process resolver to support flag metadata #305
chrfwow 4709b8f
fixup! Update in-process resolver to support flag metadata #305
chrfwow 0a13cdd
fixup! Update in-process resolver to support flag metadata #305
chrfwow 6176c1f
fixup! Update in-process resolver to support flag metadata #305
chrfwow d81d043
fixup! Update in-process resolver to support flag metadata #305
chrfwow 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 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 |
---|---|---|
|
@@ -2,38 +2,32 @@ | |
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using JsonLogic.Net; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using OpenFeature.Constant; | ||
using OpenFeature.Contrib.Providers.Flagd.Resolver.InProcess.CustomEvaluators; | ||
using OpenFeature.Error; | ||
using OpenFeature.Model; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace OpenFeature.Contrib.Providers.Flagd.Resolver.InProcess | ||
{ | ||
|
||
internal class FlagConfiguration | ||
{ | ||
[JsonProperty("state")] | ||
internal string State { get; set; } | ||
[JsonProperty("defaultVariant")] | ||
internal string DefaultVariant { get; set; } | ||
[JsonProperty("variants")] | ||
internal Dictionary<string, object> Variants { get; set; } | ||
[JsonProperty("targeting")] | ||
internal object Targeting { get; set; } | ||
[JsonProperty("source")] | ||
internal string Source { get; set; } | ||
[JsonProperty("state")] internal string State { get; set; } | ||
[JsonProperty("defaultVariant")] internal string DefaultVariant { get; set; } | ||
[JsonProperty("variants")] internal Dictionary<string, object> Variants { get; set; } | ||
[JsonProperty("targeting")] internal object Targeting { get; set; } | ||
[JsonProperty("source")] internal string Source { get; set; } | ||
[JsonProperty("metadata")] internal Dictionary<string, object> Metadata { get; set; } | ||
} | ||
|
||
internal class FlagSyncData | ||
{ | ||
[JsonProperty("flags")] | ||
internal Dictionary<string, FlagConfiguration> Flags { get; set; } | ||
[JsonProperty("$evaluators")] | ||
internal Dictionary<string, object> Evaluators { get; set; } | ||
[JsonProperty("flags")] internal Dictionary<string, FlagConfiguration> Flags { get; set; } | ||
[JsonProperty("$evaluators")] internal Dictionary<string, object> Evaluators { get; set; } | ||
[JsonProperty("metadata")] internal Dictionary<string, object> Metadata { get; set; } | ||
} | ||
|
||
internal class FlagConfigurationSync | ||
|
@@ -53,6 +47,7 @@ internal enum FlagConfigurationUpdateType | |
internal class JsonEvaluator | ||
{ | ||
private Dictionary<string, FlagConfiguration> _flags = new Dictionary<string, FlagConfiguration>(); | ||
private Dictionary<string, object> _flagSetMetadata = new Dictionary<string, object>(); | ||
|
||
private string _selector; | ||
|
||
|
@@ -88,7 +83,57 @@ internal FlagSyncData Parse(string flagConfigurations) | |
}); | ||
} | ||
|
||
return JsonConvert.DeserializeObject<FlagSyncData>(transformed); | ||
|
||
var data = JsonConvert.DeserializeObject<FlagSyncData>(transformed); | ||
if (data.Metadata == null) | ||
{ | ||
data.Metadata = new Dictionary<string, object>(); | ||
} | ||
else | ||
{ | ||
foreach (var key in new List<string>(data.Metadata.Keys)) | ||
{ | ||
var value = data.Metadata[key]; | ||
if (value is long longValue) | ||
{ | ||
value = data.Metadata[key] = (int)longValue; | ||
} | ||
|
||
VerifyMetadataValue(key, value); | ||
} | ||
} | ||
|
||
foreach (var flagConfig in data.Flags) | ||
{ | ||
if (flagConfig.Value.Metadata == null) | ||
{ | ||
continue; | ||
} | ||
|
||
foreach (var key in new List<string>(flagConfig.Value.Metadata.Keys)) | ||
{ | ||
var value = flagConfig.Value.Metadata[key]; | ||
if (value is long longValue) | ||
{ | ||
value = flagConfig.Value.Metadata[key] = (int)longValue; | ||
} | ||
|
||
VerifyMetadataValue(key, value); | ||
chrfwow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
return data; | ||
} | ||
|
||
private static void VerifyMetadataValue(string key, object value) | ||
{ | ||
if (value is int || value is double || value is string || value is bool) | ||
{ | ||
return; | ||
} | ||
|
||
throw new ParseErrorException("Metadata entry for key " + key + " and value " + value + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the proper exception type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya I think it's the best one. |
||
" is of unknown type"); | ||
} | ||
|
||
internal void Sync(FlagConfigurationUpdateType updateType, string flagConfigurations) | ||
|
@@ -99,71 +144,100 @@ internal void Sync(FlagConfigurationUpdateType updateType, string flagConfigurat | |
{ | ||
case FlagConfigurationUpdateType.ALL: | ||
_flags = flagConfigsMap.Flags; | ||
_flagSetMetadata = flagConfigsMap.Metadata; | ||
|
||
break; | ||
case FlagConfigurationUpdateType.ADD: | ||
case FlagConfigurationUpdateType.UPDATE: | ||
foreach (var keyAndValue in flagConfigsMap.Flags) | ||
{ | ||
_flags[keyAndValue.Key] = keyAndValue.Value; | ||
} | ||
break; | ||
case FlagConfigurationUpdateType.UPDATE: | ||
foreach (var keyAndValue in flagConfigsMap.Flags) | ||
|
||
foreach (var metadata in flagConfigsMap.Metadata) | ||
{ | ||
_flags[keyAndValue.Key] = keyAndValue.Value; | ||
_flagSetMetadata[metadata.Key] = metadata.Value; | ||
} | ||
|
||
break; | ||
case FlagConfigurationUpdateType.DELETE: | ||
foreach (var keyAndValue in flagConfigsMap.Flags) | ||
{ | ||
_flags.Remove(keyAndValue.Key); | ||
} | ||
break; | ||
|
||
foreach (var keyValuePair in flagConfigsMap.Metadata) | ||
{ | ||
_flagSetMetadata.Remove(keyValuePair.Key); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
public ResolutionDetails<bool> ResolveBooleanValueAsync(string flagKey, bool defaultValue, EvaluationContext context = null) | ||
public ResolutionDetails<bool> ResolveBooleanValueAsync(string flagKey, bool defaultValue, | ||
EvaluationContext context = null) | ||
{ | ||
return ResolveValue(flagKey, defaultValue, context); | ||
} | ||
|
||
public ResolutionDetails<string> ResolveStringValueAsync(string flagKey, string defaultValue, EvaluationContext context = null) | ||
public ResolutionDetails<string> ResolveStringValueAsync(string flagKey, string defaultValue, | ||
EvaluationContext context = null) | ||
{ | ||
return ResolveValue(flagKey, defaultValue, context); | ||
} | ||
|
||
public ResolutionDetails<int> ResolveIntegerValueAsync(string flagKey, int defaultValue, EvaluationContext context = null) | ||
public ResolutionDetails<int> ResolveIntegerValueAsync(string flagKey, int defaultValue, | ||
EvaluationContext context = null) | ||
{ | ||
return ResolveValue(flagKey, defaultValue, context); | ||
} | ||
|
||
public ResolutionDetails<double> ResolveDoubleValueAsync(string flagKey, double defaultValue, EvaluationContext context = null) | ||
public ResolutionDetails<double> ResolveDoubleValueAsync(string flagKey, double defaultValue, | ||
EvaluationContext context = null) | ||
{ | ||
return ResolveValue(flagKey, defaultValue, context); | ||
} | ||
|
||
public ResolutionDetails<Value> ResolveStructureValueAsync(string flagKey, Value defaultValue, EvaluationContext context = null) | ||
public ResolutionDetails<Value> ResolveStructureValueAsync(string flagKey, Value defaultValue, | ||
EvaluationContext context = null) | ||
{ | ||
return ResolveValue(flagKey, defaultValue, context); | ||
} | ||
|
||
private ResolutionDetails<T> ResolveValue<T>(string flagKey, T defaultValue, EvaluationContext context = null) | ||
private ResolutionDetails<T> ResolveValue<T>(string flagKey, T defaultValue, | ||
EvaluationContext context = null) | ||
{ | ||
// check if we find the flag key | ||
var reason = Reason.Static; | ||
if (_flags.TryGetValue(flagKey, out var flagConfiguration)) | ||
{ | ||
if ("DISABLED" == flagConfiguration.State) | ||
{ | ||
throw new FeatureProviderException(ErrorType.FlagNotFound, "FLAG_NOT_FOUND: flag '" + flagKey + "' is disabled"); | ||
throw new FeatureProviderException(ErrorType.FlagNotFound, | ||
"FLAG_NOT_FOUND: flag '" + flagKey + "' is disabled"); | ||
} | ||
|
||
Dictionary<string, object> combinedMetadata = new Dictionary<string, object>(_flagSetMetadata); | ||
if (flagConfiguration.Metadata != null) | ||
{ | ||
foreach (var metadataEntry in flagConfiguration.Metadata) | ||
{ | ||
combinedMetadata[metadataEntry.Key] = metadataEntry.Value; | ||
} | ||
} | ||
|
||
var flagMetadata = new ImmutableMetadata(combinedMetadata); | ||
var variant = flagConfiguration.DefaultVariant; | ||
if (flagConfiguration.Targeting != null && !String.IsNullOrEmpty(flagConfiguration.Targeting.ToString()) && flagConfiguration.Targeting.ToString() != "{}") | ||
if (flagConfiguration.Targeting != null && | ||
!String.IsNullOrEmpty(flagConfiguration.Targeting.ToString()) && | ||
flagConfiguration.Targeting.ToString() != "{}") | ||
{ | ||
reason = Reason.TargetingMatch; | ||
var flagdProperties = new Dictionary<string, Value>(); | ||
flagdProperties.Add(FlagdProperties.FlagKeyKey, new Value(flagKey)); | ||
flagdProperties.Add(FlagdProperties.TimestampKey, new Value(DateTimeOffset.UtcNow.ToUnixTimeSeconds())); | ||
flagdProperties.Add(FlagdProperties.TimestampKey, | ||
new Value(DateTimeOffset.UtcNow.ToUnixTimeSeconds())); | ||
|
||
if (context == null) | ||
{ | ||
|
@@ -173,7 +247,7 @@ private ResolutionDetails<T> ResolveValue<T>(string flagKey, T defaultValue, Eva | |
var targetingContext = context.AsDictionary().Add( | ||
FlagdProperties.FlagdPropertiesKey, | ||
new Value(new Structure(flagdProperties)) | ||
); | ||
); | ||
|
||
var targetingString = flagConfiguration.Targeting.ToString(); | ||
// Parse json into hierarchical structure | ||
|
@@ -202,32 +276,39 @@ private ResolutionDetails<T> ResolveValue<T>(string flagKey, T defaultValue, Eva | |
{ | ||
// if variant is null, revert to default | ||
reason = Reason.Default; | ||
flagConfiguration.Variants.TryGetValue(flagConfiguration.DefaultVariant, out var defaultVariantValue); | ||
flagConfiguration.Variants.TryGetValue(flagConfiguration.DefaultVariant, | ||
out var defaultVariantValue); | ||
if (defaultVariantValue == null) | ||
{ | ||
throw new FeatureProviderException(ErrorType.ParseError, "PARSE_ERROR: flag '" + flagKey + "' has missing or invalid defaultVariant."); | ||
throw new FeatureProviderException(ErrorType.ParseError, | ||
"PARSE_ERROR: flag '" + flagKey + "' has missing or invalid defaultVariant."); | ||
} | ||
|
||
var value = ExtractFoundVariant<T>(defaultVariantValue, flagKey); | ||
return new ResolutionDetails<T>( | ||
flagKey: flagKey, | ||
value, | ||
reason: reason, | ||
variant: variant | ||
); | ||
flagKey: flagKey, | ||
value, | ||
reason: reason, | ||
variant: variant, | ||
flagMetadata: flagMetadata | ||
); | ||
} | ||
else if (flagConfiguration.Variants.TryGetValue(variant, out var foundVariantValue)) | ||
{ | ||
// if variant can be found, return it - this could be TARGETING_MATCH or STATIC. | ||
var value = ExtractFoundVariant<T>(foundVariantValue, flagKey); | ||
return new ResolutionDetails<T>( | ||
flagKey: flagKey, | ||
value, | ||
reason: reason, | ||
variant: variant | ||
); | ||
flagKey: flagKey, | ||
value, | ||
reason: reason, | ||
variant: variant, | ||
flagMetadata: flagMetadata | ||
); | ||
} | ||
} | ||
throw new FeatureProviderException(ErrorType.FlagNotFound, "FLAG_NOT_FOUND: flag '" + flagKey + "' not found"); | ||
|
||
throw new FeatureProviderException(ErrorType.FlagNotFound, | ||
"FLAG_NOT_FOUND: flag '" + flagKey + "' not found"); | ||
} | ||
|
||
static T ExtractFoundVariant<T>(object foundVariantValue, string flagKey) | ||
|
@@ -236,6 +317,7 @@ static T ExtractFoundVariant<T>(object foundVariantValue, string flagKey) | |
{ | ||
foundVariantValue = Convert.ToInt32(foundVariantValue); | ||
} | ||
|
||
if (typeof(T) == typeof(double)) | ||
{ | ||
foundVariantValue = Convert.ToDouble(foundVariantValue); | ||
|
@@ -244,11 +326,14 @@ static T ExtractFoundVariant<T>(object foundVariantValue, string flagKey) | |
{ | ||
foundVariantValue = ConvertJObjectToOpenFeatureValue(value); | ||
} | ||
|
||
if (foundVariantValue is T castValue) | ||
{ | ||
return castValue; | ||
} | ||
throw new FeatureProviderException(ErrorType.TypeMismatch, "TYPE_MISMATCH: flag '" + flagKey + "' does not match the expected type"); | ||
|
||
throw new FeatureProviderException(ErrorType.TypeMismatch, | ||
"TYPE_MISMATCH: flag '" + flagKey + "' does not match the expected type"); | ||
} | ||
|
||
static dynamic ConvertToDynamicObject(IImmutableDictionary<string, Value> dictionary) | ||
|
@@ -259,7 +344,9 @@ static dynamic ConvertToDynamicObject(IImmutableDictionary<string, Value> dictio | |
foreach (var kvp in dictionary) | ||
{ | ||
expandoDict.Add(kvp.Key, | ||
kvp.Value.IsStructure ? ConvertToDynamicObject(kvp.Value.AsStructure.AsDictionary()) : kvp.Value.AsObject); | ||
kvp.Value.IsStructure | ||
? ConvertToDynamicObject(kvp.Value.AsStructure.AsDictionary()) | ||
: kvp.Value.AsObject); | ||
} | ||
|
||
return expandoObject; | ||
|
@@ -302,4 +389,4 @@ static Value ConvertJObjectToOpenFeatureValue(JObject jsonValue) | |
return new Value(new Structure(result)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think it would be more typical to do this functionally with Linq (
.Select
) but it's a style choice.