public void StringVariationDetailReturnsValueAndReason() { featureStore.Upsert(VersionedDataKind.Features, new FeatureFlagBuilder("key").OffWithValue(new JValue("b")).Build()); var expected = new EvaluationDetail <string>("b", 0, EvaluationReason.OffReason); Assert.Equal(expected, client.StringVariationDetail("key", user, "a")); }
public void StringVariationDetailReturnsValueAndReason() { testData.UsePreconfiguredFlag(new FeatureFlagBuilder("key").OffWithValue(LdValue.Of("b")).Build()); var expected = new EvaluationDetail <string>("b", 0, EvaluationReason.OffReason); Assert.Equal(expected, client.StringVariationDetail("key", user, "a")); }
/// <summary> /// Equivalent to <see cref="ILdClient.StringVariationDetail(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>an <see cref="EvaluationDetail{T}"/> object</returns> public static EvaluationDetail <T> EnumVariationDetail <T>(this ILdClient client, string key, T defaultValue) where T : struct, Enum { var stringDetail = client.StringVariationDetail(key, defaultValue.ToString()); if (!stringDetail.IsDefaultValue && stringDetail.Value != null) { if (Enum.TryParse <T>(stringDetail.Value, out var enumValue)) { return(new EvaluationDetail <T>(enumValue, stringDetail.VariationIndex, stringDetail.Reason)); } return(new EvaluationDetail <T>(defaultValue, stringDetail.VariationIndex, EvaluationReason.ErrorReason(EvaluationErrorKind.WrongType))); } return(new EvaluationDetail <T>(defaultValue, stringDetail.VariationIndex, stringDetail.Reason)); }
/// <summary> /// Equivalent to <see cref="ILdClient.StringVariationDetail(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>an <see cref="EvaluationDetail{T}"/> object</returns> public static EvaluationDetail <T> EnumVariationDetail <T>(this ILdClient client, string key, User user, T defaultValue) { var stringDetail = client.StringVariationDetail(key, user, defaultValue.ToString()); if (stringDetail.Value != null) { try { var enumValue = (T)System.Enum.Parse(typeof(T), stringDetail.Value, true); return(new EvaluationDetail <T>(enumValue, stringDetail.VariationIndex, stringDetail.Reason)); } catch (System.ArgumentException) { return(new EvaluationDetail <T>(defaultValue, stringDetail.VariationIndex, EvaluationReason.ErrorReason(EvaluationErrorKind.WRONG_TYPE))); } } return(new EvaluationDetail <T>(defaultValue, stringDetail.VariationIndex, stringDetail.Reason)); }