public void StringVariationReturnsFlagValue() { featureStore.Upsert(VersionedDataKind.Features, new FeatureFlagBuilder("key").OffWithValue(new JValue("b")).Build()); Assert.Equal("b", client.StringVariation("key", user, "a")); }
public void StringVariationSendsEvent() { var flag = new FeatureFlagBuilder("key").OffWithValue(new JValue("b")).Build(); featureStore.Upsert(VersionedDataKind.Features, flag); client.StringVariation("key", user, "a"); Assert.Equal(1, eventSink.Events.Count); CheckFeatureEvent(eventSink.Events[0], flag, new JValue("b"), new JValue("a"), null); }
public void StringVariationSendsEvent() { var flag = new FeatureFlagBuilder("key").Version(1).OffWithValue(LdValue.Of("b")).Build(); testData.UsePreconfiguredFlag(flag); client.StringVariation("key", user, "a"); Assert.Equal(1, eventSink.Events.Count); CheckFeatureEvent(eventSink.Events[0], flag, LdValue.Of("b"), LdValue.Of("a"), null); }
private void VerifyNoFlagValues(ILdClient client, IDictionary <string, string> flags) { Assert.True(client.Initialized); foreach (var e in flags) { Assert.Null(client.StringVariation(e.Key, null)); } }
/// <summary> /// Equivalent to <see cref="ILdClient.StringVariation(string, string)"/>, but converts the /// flag's string value to an enum value. /// </summary> /// <remarks> /// <para> /// If the flag has a value that is not one of the allowed enum value names, or is not a string, /// <c>defaultValue</c> is returned. /// </para> /// </remarks> /// <typeparam name="T">the enum type</typeparam> /// <param name="client">the client instance</param> /// <param name="key">the unique feature key for the feature flag</param> /// <param name="defaultValue">the default value of the flag (as an enum value)</param> /// <returns>the variation for the given user, or <c>defaultValue</c> if the flag cannot /// be evaluated or does not have a valid enum value</returns> public static T EnumVariation <T>(this ILdClient client, string key, T defaultValue) where T : struct, Enum { var stringVal = client.StringVariation(key, defaultValue.ToString()); if (stringVal != null) { if (Enum.TryParse <T>(stringVal, out var enumValue)) { return(enumValue); } } return(defaultValue); }
/// <summary> /// Equivalent to <see cref="ILdClient.StringVariation(string, User, string)"/>, but converts the /// flag's string value to an enum value. /// </summary> /// <remarks> /// <para> /// If the flag has a value that is not one of the allowed enum value names, or is not a string, /// <c>defaultValue</c> is returned. /// </para> /// <para> /// Note that there is no type constraint to guarantee that T really is an enum type, because that is /// a C# 7.3 feature that is unavailable in older versions of .NET Standard. If you try to use a /// non-enum type, you will simply receive the default value back. /// </para> /// </remarks> /// <typeparam name="T">the enum type</typeparam> /// <param name="client">the client instance</param> /// <param name="key">the unique feature key for the feature flag</param> /// <param name="user">the end user requesting the flag</param> /// <param name="defaultValue">the default value of the flag (as an enum value)</param> /// <returns>the variation for the given user, or <c>defaultValue</c> if the flag cannot /// be evaluated or does not have a valid enum value</returns> public static T EnumVariation <T>(this ILdClient client, string key, User user, T defaultValue) { var stringVal = client.StringVariation(key, user, defaultValue.ToString()); if (stringVal != null) { try { return((T)System.Enum.Parse(typeof(T), stringVal, true)); } catch (System.ArgumentException) { } } return(defaultValue); }
public string GetStringFlag <T>(string defaultValue) where T : FeatureFlag <string>, new() { return(_ldClient.StringVariation(new T().Keyname, _defaultUser, defaultValue)); }
public void StringVariationReturnsFlagValue() { testData.UsePreconfiguredFlag(new FeatureFlagBuilder("key").OffWithValue(LdValue.Of("b")).Build()); Assert.Equal("b", client.StringVariation("key", user, "a")); }
public T Get <T>(Enum featureFlag, IDictionary <string, object> userAttributes = null) { var value = default(T); var type = typeof(T); _user = GetUser(userAttributes); var feature = featureFlag.GetFlag(); switch (feature.Type) { case VariationType.Boolean: if (feature.Type != VariationType.Boolean && type == typeof(bool)) { throw new InvalidCastException($"Feature flag is configure to be {feature.Type} except to be bool"); } value = (T)Convert.ChangeType(_ldClient.BoolVariation(feature.Key, _user, (bool)feature.DefaultValue), typeof(T)); break; case VariationType.String: if (feature.Type != VariationType.String && type == typeof(string)) { throw new InvalidCastException($"Feature flag is configure to be {feature.Type} except to be string"); } value = (T)Convert.ChangeType(_ldClient.StringVariation(feature.Key, _user, (string)feature.DefaultValue), typeof(T)); break; case VariationType.Number: if (feature.Type != VariationType.Number && type == typeof(int)) { throw new InvalidCastException($"Feature flag is configure to be {feature.Type} except to be int"); } value = (T)Convert.ChangeType(_ldClient.IntVariation(feature.Key, _user, (int)feature.DefaultValue), typeof(T)); break; case VariationType.Float: if (feature.Type != VariationType.Float && type == typeof(float)) { throw new InvalidCastException($"Feature flag is configure to be {feature.Type} except to be float"); } value = (T)Convert.ChangeType(_ldClient.FloatVariation(feature.Key, _user, (float)feature.DefaultValue), typeof(T)); break; case VariationType.ArrayOfString: if (feature.Type != VariationType.ArrayOfString && type == typeof(string)) { throw new InvalidCastException($"Feature flag is configure to be {feature.Type} except to be array of string"); } var commaSeperateStr = _ldClient.StringVariation(feature.Key, _user, (string)feature.DefaultValue); value = (T)Convert.ChangeType(commaSeperateStr.Split(new[] { ',' }, StringSplitOptions.None), typeof(T)); break; case VariationType.Json: if (feature.Type != VariationType.Json && type == typeof(JToken)) { throw new InvalidCastException($"Feature flag is configure to be {feature.Type} except to be JToken"); } value = (T)Convert.ChangeType(_ldClient.JsonVariation(feature.Key, _user, (JToken)feature.DefaultValue), typeof(T)); break; } return(value); }