Beispiel #1
0
        JToken Variation(string featureKey, JToken defaultValue)
        {
            FeatureFlagEvent    featureFlagEvent = FeatureFlagEvent.Default(featureKey);
            FeatureRequestEvent featureRequestEvent;

            if (!Initialized())
            {
                Log.Warn("LaunchDarkly client has not yet been initialized. Returning default");
                return(defaultValue);
            }

            if (User == null || User.Key == null)
            {
                Log.Warn("Feature flag evaluation called with null user or null user key. Returning default");
                featureRequestEvent = eventFactory.NewDefaultFeatureRequestEvent(featureFlagEvent,
                                                                                 User,
                                                                                 defaultValue);
                eventProcessor.SendEvent(featureRequestEvent);
                return(defaultValue);
            }

            var flag = flagCacheManager.FlagForUser(featureKey, User);

            if (flag != null)
            {
                featureFlagEvent = new FeatureFlagEvent(featureKey, flag);
                var value = flag.value;
                if (value == null)
                {
                    featureRequestEvent = eventFactory.NewDefaultFeatureRequestEvent(featureFlagEvent,
                                                                                     User,
                                                                                     defaultValue);
                    value = defaultValue;
                }
                else
                {
                    featureRequestEvent = eventFactory.NewFeatureRequestEvent(featureFlagEvent,
                                                                              User,
                                                                              flag.variation,
                                                                              flag.value,
                                                                              defaultValue);
                }
                eventProcessor.SendEvent(featureRequestEvent);
                return(value);
            }

            Log.InfoFormat("Unknown feature flag {0}; returning default value",
                           featureKey);
            featureRequestEvent = eventFactory.NewUnknownFeatureRequestEvent(featureKey,
                                                                             User,
                                                                             defaultValue);
            eventProcessor.SendEvent(featureRequestEvent);
            return(defaultValue);
        }
Beispiel #2
0
        EvaluationDetail <T> VariationInternal <T>(string featureKey, T defaultValue, ValueType <T> desiredType, EventFactory eventFactory)
        {
            FeatureFlagEvent featureFlagEvent = FeatureFlagEvent.Default(featureKey);
            JToken           defaultJson      = desiredType.ValueToJson(defaultValue);

            EvaluationDetail <T> errorResult(EvaluationErrorKind kind) =>
            new EvaluationDetail <T>(defaultValue, null, new EvaluationReason.Error(kind));

            var flag = flagCacheManager.FlagForUser(featureKey, User);

            if (flag == null)
            {
                if (!Initialized)
                {
                    Log.Warn("LaunchDarkly client has not yet been initialized. Returning default value");
                    SendEventIfOnline(eventFactory.NewUnknownFeatureRequestEvent(featureKey, User, defaultJson,
                                                                                 EvaluationErrorKind.CLIENT_NOT_READY));
                    return(errorResult(EvaluationErrorKind.CLIENT_NOT_READY));
                }
                else
                {
                    Log.InfoFormat("Unknown feature flag {0}; returning default value", featureKey);
                    SendEventIfOnline(eventFactory.NewUnknownFeatureRequestEvent(featureKey, User, defaultJson,
                                                                                 EvaluationErrorKind.FLAG_NOT_FOUND));
                    return(errorResult(EvaluationErrorKind.FLAG_NOT_FOUND));
                }
            }
            else
            {
                if (!Initialized)
                {
                    Log.Warn("LaunchDarkly client has not yet been initialized. Returning cached value");
                }
            }

            featureFlagEvent = new FeatureFlagEvent(featureKey, flag);
            EvaluationDetail <T> result;
            JToken valueJson;

            if (flag.value == null || flag.value.Type == JTokenType.Null)
            {
                valueJson = defaultJson;
                result    = new EvaluationDetail <T>(defaultValue, flag.variation, flag.reason);
            }
            else
            {
                try
                {
                    valueJson = flag.value;
                    var value = desiredType.ValueFromJson(flag.value);
                    result = new EvaluationDetail <T>(value, flag.variation, flag.reason);
                }
                catch (Exception)
                {
                    valueJson = defaultJson;
                    result    = new EvaluationDetail <T>(defaultValue, null, new EvaluationReason.Error(EvaluationErrorKind.WRONG_TYPE));
                }
            }
            var featureEvent = eventFactory.NewFeatureRequestEvent(featureFlagEvent, User,
                                                                   new EvaluationDetail <JToken>(valueJson, flag.variation, flag.reason), defaultJson);

            SendEventIfOnline(featureEvent);
            return(result);
        }