Пример #1
0
 // Checks prerequisites if any; returns null if successful, or an EvaluationReason if we have to
 // short-circuit due to a prerequisite failure.
 private EvaluationReason CheckPrerequisites(User user, IFeatureStore featureStore, IList <FeatureRequestEvent> events,
                                             EventFactory eventFactory)
 {
     if (Prerequisites == null || Prerequisites.Count == 0)
     {
         return(null);
     }
     foreach (var prereq in Prerequisites)
     {
         var prereqOk          = true;
         var prereqFeatureFlag = featureStore.Get(VersionedDataKind.Features, prereq.Key);
         if (prereqFeatureFlag == null)
         {
             Log.ErrorFormat("Could not retrieve prerequisite flag \"{0}\" when evaluating \"{1}\"",
                             prereq.Key, Key);
             prereqOk = false;
         }
         else
         {
             var prereqEvalResult = prereqFeatureFlag.Evaluate(user, featureStore, events, eventFactory);
             // Note that if the prerequisite flag is off, we don't consider it a match no matter
             // what its off variation was. But we still need to evaluate it in order to generate
             // an event.
             if (!prereqFeatureFlag.On || prereqEvalResult.VariationIndex == null || prereqEvalResult.VariationIndex.Value != prereq.Variation)
             {
                 prereqOk = false;
             }
             events.Add(eventFactory.NewPrerequisiteFeatureRequestEvent(prereqFeatureFlag, user,
                                                                        prereqEvalResult, this));
         }
         if (!prereqOk)
         {
             return(EvaluationReason.PrerequisiteFailedReason(prereq.Key));
         }
     }
     return(null);
 }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            LdValue o = (LdValue)LdValueSerializer.Instance.ReadJson(reader, typeof(LdValue), LdValue.Null, serializer);

            if (o.IsNull)
            {
                return(null);
            }
            EvaluationReasonKind kind = (EvaluationReasonKind)Enum.Parse(typeof(EvaluationReasonKind), o.Get("kind").AsString);

            switch (kind)
            {
            case EvaluationReasonKind.OFF:
                return(EvaluationReason.OffReason);

            case EvaluationReasonKind.FALLTHROUGH:
                return(EvaluationReason.FallthroughReason);

            case EvaluationReasonKind.TARGET_MATCH:
                return(EvaluationReason.TargetMatchReason);

            case EvaluationReasonKind.RULE_MATCH:
                var index = o.Get("ruleIndex").AsInt;
                var id    = o.Get("ruleId").AsString;
                return(EvaluationReason.RuleMatchReason(index, id));

            case EvaluationReasonKind.PREREQUISITE_FAILED:
                var key = o.Get("prerequisiteKey").AsString;
                return(EvaluationReason.PrerequisiteFailedReason(key));

            case EvaluationReasonKind.ERROR:
                var errorKind = (EvaluationErrorKind)Enum.Parse(typeof(EvaluationErrorKind), o.Get("errorKind").AsString);
                return(EvaluationReason.ErrorReason(errorKind));
            }
            throw new ArgumentException();
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject o = serializer.Deserialize <JObject>(reader);

            if (o == null)
            {
                return(null);
            }
            EvaluationReasonKind kind = o.GetValue("kind").ToObject <EvaluationReasonKind>();

            switch (kind)
            {
            case EvaluationReasonKind.OFF:
                return(EvaluationReason.OffReason);

            case EvaluationReasonKind.FALLTHROUGH:
                return(EvaluationReason.FallthroughReason);

            case EvaluationReasonKind.TARGET_MATCH:
                return(EvaluationReason.TargetMatchReason);

            case EvaluationReasonKind.RULE_MATCH:
                var index = (int)o.GetValue("ruleIndex");
                var id    = (string)o.GetValue("ruleId");
                return(EvaluationReason.RuleMatchReason(index, id));

            case EvaluationReasonKind.PREREQUISITE_FAILED:
                var key = (string)o.GetValue("prerequisiteKey");
                return(EvaluationReason.PrerequisiteFailedReason(key));

            case EvaluationReasonKind.ERROR:
                var errorKind = o.GetValue("errorKind").ToObject <EvaluationErrorKind>();
                return(EvaluationReason.ErrorReason(errorKind));
            }
            throw new ArgumentException();
        }