public static GroupingValues <string, string> Parse(StringValues values)
        {
            HashSet <Grouping <string, string> > groups = new HashSet <Grouping <string, string> >();
            HashSet <string> individuals = new HashSet <string>();

            foreach (StringSegment value in values.SelectMany(x => Delimiter.Parenthetical.Split(x)))
            {
                // TODO: C# 8.0 will support normal FP-style tuple match.
                // see: https://github.com/dotnet/csharplang/issues/1395
                switch (Grouping <TKey, TValue> .TryParse(value.Value))
                {
                case ValueTuple <bool, Grouping <string, string> > t when t.Item1:
                {
                    groups.Add(t.Item2);
                    continue;
                }

                default:
                {
                    individuals.Add(value.Value);
                    continue;
                }
                }
            }

            StringValues items = individuals.Except(groups.SelectMany(x => x)).ToArray();

            if (items.Any())
            {
                groups.Add(Grouping <string, string> .CreateIndividuals(items));
            }

            return(new GroupingValues <string, string>(groups));
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Returns <c> true </c> if the User-Agent HTTP header value is valid, otherwise returns
        ///     <c> false </c>.
        /// </summary>
        /// <param name="headerValues"> The header values. </param>
        /// <returns>
        ///     <c> true </c> if the User-Agent HTTP header values are valid; otherwise, <c> false </c>.
        /// </returns>
        public override bool IsValid(StringValues headerValues)
        {
            var isValid = false;

            if (!StringValues.IsNullOrEmpty(headerValues))
            {
                var values = headerValues
                             .SelectMany(x => Split(x))
                             .Select(
                    x =>
                {
                    var parsed = ProductInfoHeaderValue.TryParse(x, out ProductInfoHeaderValue productInfo);
                    return(new
                    {
                        Parsed = parsed,
                        ProductInfo = productInfo
                    });
                })
                             .ToArray();

                isValid = values.All(x => x.Parsed) &&
                          values
                          .Where(x => x.ProductInfo.Product != null)
                          .All(x => !string.IsNullOrWhiteSpace(x.ProductInfo.Product.Name) &&
                               !string.IsNullOrWhiteSpace(x.ProductInfo.Product.Version));
            }

            return(isValid);
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public virtual void Read(string parameterName, StringValues parameterValue)
        {
            _lastParameterName = parameterName;

            foreach (string value in parameterValue.SelectMany(ExtractParameterValue))
            {
                ReadSingleValue(parameterName, value);
            }
        }
        private static StringValues SplitOnDelimiters(StringValues values, char[] delimiters)
        {
            if (values.Any(v => delimiters.Any(v.Contains)))
            {
                return(new StringValues(values
                                        .SelectMany(x => x.Split(delimiters, StringSplitOptions.RemoveEmptyEntries))
                                        .ToArray()));
            }

            return(values);
        }
Exemplo n.º 5
0
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            EnsureArg.IsNotNull(bindingContext, nameof(bindingContext));
            StringValues values = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).Values;

            IReadOnlyList <string> result = values.Count == 0
                ? Array.Empty <string>()
                : values.SelectMany(x => x.Split(',', StringSplitOptions.TrimEntries)).ToList();

            bindingContext.Result = ModelBindingResult.Success(result);
            return(Task.CompletedTask);
        }
        internal static IEnumerable <string> GetHeaderValues(IHeaderDictionary headers, string headerName)
        {
            IEnumerable <string> result = Enumerable.Empty <string>();

            if (headers != null)
            {
                StringValues headerValues = headers[headerName];
                if (!StringValues.IsNullOrEmpty(headerValues))
                {
                    result = headerValues.SelectMany(headerValue => headerValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                }
            }
            return(result);
        }