internal static bool HasOption(FlagsStateOption[]  options, FlagsStateOption option)
 {
     foreach (var o in options)
     {
         if (o == option)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public FeatureFlagsState AllFlagsState(User user, params FlagsStateOption[] options)
        {
            if (IsOffline())
            {
                _evalLog.Warn("AllFlagsState() called when client is in offline mode; returning empty state");
                return(new FeatureFlagsState(false));
            }
            if (!Initialized)
            {
                if (_dataStore.Initialized())
                {
                    _evalLog.Warn("AllFlagsState() called before client initialized; using last known values from data store");
                }
                else
                {
                    _evalLog.Warn("AllFlagsState() called before client initialized; data store unavailable, returning empty state");
                    return(new FeatureFlagsState(false));
                }
            }
            if (user == null || user.Key == null)
            {
                _evalLog.Warn("AllFlagsState() called with null user or null user key; returning empty state");
                return(new FeatureFlagsState(false));
            }

            var builder              = new FeatureFlagsStateBuilder(options);
            var clientSideOnly       = FlagsStateOption.HasOption(options, FlagsStateOption.ClientSideOnly);
            var withReasons          = FlagsStateOption.HasOption(options, FlagsStateOption.WithReasons);
            var detailsOnlyIfTracked = FlagsStateOption.HasOption(options, FlagsStateOption.DetailsOnlyForTrackedFlags);
            KeyedItems <ItemDescriptor> flags;

            try
            {
                flags = _dataStore.GetAll(DataModel.Features);
            }
            catch (Exception e)
            {
                LogHelpers.LogException(_log, "Exception while retrieving flags for AllFlagsState", e);
                return(new FeatureFlagsState(false));
            }
            foreach (var pair in flags.Items)
            {
                if (pair.Value.Item is null || !(pair.Value.Item is FeatureFlag flag))
                {
                    continue;
                }
                if (clientSideOnly && !flag.ClientSide)
                {
                    continue;
                }
                try
                {
                    Evaluator.EvalResult result = _evaluator.Evaluate(flag, user, EventFactory.Default);
                    builder.AddFlag(flag.Key, result.Result.Value, result.Result.VariationIndex,
                                    result.Result.Reason, flag.Version, flag.TrackEvents, flag.DebugEventsUntilDate);
                }
                catch (Exception e)
                {
                    LogHelpers.LogException(_evalLog,
                                            string.Format("Exception caught for feature flag \"{0}\" when evaluating all flags", flag.Key),
                                            e);
                    EvaluationReason reason = EvaluationReason.ErrorReason(EvaluationErrorKind.Exception);
                    builder.AddFlag(flag.Key, new EvaluationDetail <LdValue>(LdValue.Null, null, reason));
                }
            }
            return(builder.Build());
        }