/// <summary>
 /// Returns the value of an individual feature flag at the time the state was recorded.
 /// </summary>
 /// <param name="key">the feature flag key</param>
 /// <returns>the flag's JSON value; <see cref="LdValue.Null"/> if the flag returned
 /// the default value, or if there was no such flag</returns>
 public LdValue GetFlagValueJson(string key)
 {
     if (_flagValues.TryGetValue(key, out var value))
     {
         return(LdValue.FromSafeValue(value));
     }
     return(LdValue.Null);
 }
예제 #2
0
 internal EvaluationDetail <LdValue> GetVariation(int variation, EvaluationReason reason)
 {
     if (variation < 0 || variation >= Variations.Count)
     {
         Log.ErrorFormat("Data inconsistency in feature flag \"{0}\": invalid variation index", Key);
         return(ErrorResult(EvaluationErrorKind.MALFORMED_FLAG));
     }
     return(new EvaluationDetail <LdValue>(LdValue.FromSafeValue(Variations[variation]), variation, reason));
 }
예제 #3
0
 private bool MatchAny(LdValue userValue)
 {
     foreach (var v in Values)
     {
         if (Operator.Apply(Op, userValue, LdValue.FromSafeValue(v)))
         {
             return(true);
         }
     }
     return(false);
 }
 /// <summary>
 /// Returns a dictionary of flag keys to flag values.
 /// </summary>
 /// <remarks>
 /// <para>
 /// If a flag would have evaluated to the default value, its value will be
 /// <see cref="LdValue.Null"/>.
 /// </para>
 /// <para>
 /// Do not use this method if you are passing data to the front end to "bootstrap" the
 /// JavaScript client. Instead, serialize the <see cref="FeatureFlagsState"/> object to JSON
 /// using <c>JsonConvert.SerializeObject()</c>.
 /// </para>
 /// </remarks>
 /// <returns>a dictionary of flag keys to flag values</returns>
 public IReadOnlyDictionary <string, LdValue> ToValuesJsonMap()
 {
     // In the next major version, we will store the map this way in the first place so there will
     // be no conversion step.
     lock (this)
     {
         if (_immutableValuesMap is null)
         {
             // There's a potential race condition here but the result is the same either way, so
             _immutableValuesMap = _flagValues.ToDictionary <KeyValuePair <string, JToken>, string, LdValue>(
                 pair => pair.Key,
                 pair => LdValue.FromSafeValue(pair.Value));
         }
         return(_immutableValuesMap);
     }
 }
예제 #5
0
        // This method was formerly part of User. It has been moved here because it is only needed
        // for server-side evaluation logic, specifically for comparing values with an Operator.
        // Note that ImmutableJsonValue.Of(string) is an efficient operation that does not allocate
        // a new object, but just wraps the string in a struct (or returns ImmutableJsonValue.Null
        // if the string is null).
        public static LdValue GetUserAttributeForEvaluation(User user, string attribute)
        {
            switch (attribute)
            {
            case "key":
                return(LdValue.Of(user.Key));

            case "secondary":
                return(LdValue.Of(user.Secondary));

            case "ip":
                return(LdValue.Of(user.IPAddress));

            case "email":
                return(LdValue.Of(user.Email));

            case "avatar":
                return(LdValue.Of(user.Avatar));

            case "firstName":
                return(LdValue.Of(user.FirstName));

            case "lastName":
                return(LdValue.Of(user.LastName));

            case "name":
                return(LdValue.Of(user.Name));

            case "country":
                return(LdValue.Of(user.Country));

            case "anonymous":
                if (user.Anonymous.HasValue)
                {
                    return(LdValue.Of(user.Anonymous.Value));
                }
                return(LdValue.Null);

            default:
                return(user.Custom.TryGetValue(attribute, out var customValue) ?
                       LdValue.FromSafeValue(customValue) :
                       LdValue.Null);
            }
        }
예제 #6
0
 public void Track(string name, JToken data, User user)
 {
     Track(name, user, LdValue.FromSafeValue(data));
 }
예제 #7
0
 public EvaluationDetail <JToken> JsonVariationDetail(string key, User user, JToken defaultValue)
 {
     return(Evaluate(key, user, LdValue.FromSafeValue(defaultValue), LdValue.Convert.UnsafeJToken, false, EventFactory.DefaultWithReasons));
 }
예제 #8
0
 public JToken JsonVariation(string key, User user, JToken defaultValue)
 {
     return(Evaluate(key, user, LdValue.FromSafeValue(defaultValue), LdValue.Convert.UnsafeJToken, false, EventFactory.Default).Value);
 }