public bool Evaluate(Constraint constraint, UnleashContext context)
        {
            var contextValueString = context.GetByName(constraint.ContextName);

            if (string.IsNullOrWhiteSpace(contextValueString))
            {
                return(false);
            }

            if (!double.TryParse(contextValueString, NumberStyles.Number, CultureInfo.InvariantCulture, out var contextNumber))
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(constraint.Value) || !double.TryParse(constraint.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var constraintNumber))
            {
                return(false);
            }

            if (constraint.Inverted)
            {
                return(!Eval(constraint.Operator, constraintNumber, contextNumber));
            }

            return(Eval(constraint.Operator, constraintNumber, contextNumber));
        }
        public bool Evaluate(Constraint constraint, UnleashContext context)
        {
            var             contextValue = context.GetByName(constraint.ContextName);
            SemanticVersion contextSemver;

            if (!SemanticVersion.TryParse(contextValue, out contextSemver))
            {
                Logger.Info("Couldn't parse version {0} from context");
                return(false);
            }

            if (!string.IsNullOrWhiteSpace(constraint.Value))
            {
                SemanticVersion constraintSemver;
                if (!SemanticVersion.TryParse(constraint.Value, out constraintSemver))
                {
                    return(false);
                }

                if (constraint.Inverted)
                {
                    return(!Eval(constraint.Operator, contextSemver, constraintSemver));
                }

                return(Eval(constraint.Operator, contextSemver, constraintSemver));
            }

            return(false);
        }
        private static bool ValidateConstraint(Constraint constraint, UnleashContext context)
        {
            var contextValue = context.GetByName(constraint.ContextName);
            var isIn         = contextValue != null && constraint.Values.Contains(contextValue.Trim());

            return((constraint.Operator == Operator.IN) == isIn);
        }
        private static string GetIdentifier(UnleashContext context, string stickiness)
        {
            if (stickiness != "default")
            {
                var stickinessValue = context.GetByName(stickiness);
                return(stickinessValue ?? GetRandomValue());
            }

            return(context.UserId
                   ?? context.SessionId
                   ?? context.RemoteAddress
                   ?? GetRandomValue());
        }
        private string ResolveStickiness(string stickiness, UnleashContext context)
        {
            switch (stickiness)
            {
            case "random":
                return(randomGenerator());

            case "default":
                return(context.UserId
                       ?? context.SessionId
                       ?? randomGenerator());

            default:
                return(context.GetByName(stickiness));
            }
        }
        public bool Evaluate(Constraint constraint, UnleashContext context)
        {
            if (constraint.Values == null || !constraint.Values.Any())
            {
                return(false);
            }

            var contextValue = context.GetByName(constraint.ContextName);

            if (string.IsNullOrWhiteSpace(contextValue))
            {
                return(false);
            }

            if (constraint.Inverted)
            {
                return(!constraint.Values.Any(val => Eval(constraint.Operator, val, contextValue, constraint.CaseInsensitive)));
            }

            return(constraint.Values.Any(val => Eval(constraint.Operator, val, contextValue, constraint.CaseInsensitive)));
        }
        public bool Evaluate(Constraint constraint, UnleashContext context)
        {
            if (string.IsNullOrWhiteSpace(constraint.Value) || !DateTimeOffset.TryParse(constraint.Value, out var constraintDate))
            {
                return(false);
            }

            var            contextValue = context.GetByName(constraint.ContextName);
            DateTimeOffset?contextDate;

            if (!string.IsNullOrWhiteSpace(contextValue))
            {
                if (!DateTimeOffset.TryParse(contextValue, out var date))
                {
                    return(false);
                }
                else
                {
                    contextDate = date;
                }
            }
            else
            {
                contextDate = context.CurrentTime;
                if (!contextDate.HasValue)
                {
                    return(false);
                }
            }

            if (constraint.Inverted)
            {
                return(!Eval(constraint.Operator, constraintDate, contextDate.Value));
            }

            return(Eval(constraint.Operator, constraintDate, contextDate.Value));
        }
Пример #8
0
        private static bool ValidateConstraint(Constraint constraint, UnleashContext context)
        {
            var contextValue = context.GetByName(constraint.ContextName);

            if (operators.ContainsKey(constraint.Operator))
            {
                return(operators[constraint.Operator].Evaluate(constraint, context));
            }
            else
            {
                var isIn = contextValue != null && constraint.Values.Contains(contextValue.Trim());

                if (constraint.Operator == Operator.IN)
                {
                    return(isIn);
                }
                if (constraint.Operator == Operator.NOT_IN)
                {
                    return(!isIn);
                }
            }

            return(false);
        }