<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Results;

namespace KryptonDotNet
{
    /// <summary>
    ///  Represents a filtered list of items as content and filter info in header values
    ///  : -h krypton-filter-info(json serialized array-[{ "key":key, "value":value },...] )
    /// </summary>
    public class FilteredResult : ResponseMessageResult
    {
        public IQueryable<object> Items { get; }

        public FilteredResult(IQueryable<object> items, HttpActionContext actionContext)
            : base(new HttpResponseMessage(System.Net.HttpStatusCode.OK))
        {
            var filters = HeaderUtil.ResolveFilterInfoHeader(actionContext.Request.Headers);
            filters = filters.Where(f => !string.IsNullOrEmpty(f.Value) && string.Compare(f.Value, "all", true) != 0).ToArray();

            if (filters != null && filters.Length != 0)
            {
                StringBuilder filterClause = new StringBuilder(string.Empty);
                for (int i = 0; i < filters.Length; i++)
                {
                    var filter = filters[i];
                    filterClause.Append(filter.ToString());
                    if (i == filters.Length - 1) continue;
                    filterClause.Append(" and ");
                }
                items = items.Where(filterClause.ToString());

            }
            Items = items;
            this.Response.Content = new ObjectContent(Items.GetType(), items, actionContext.ControllerContext.Configuration.Formatters.JsonFormatter);
        }

    }
    internal class FilterInfo
    {
        public string Key { get; set; }
        public string Value { get; set; }
        public override string ToString()
        {
            return $"{Key}.ToString() = \"{Value}\"";
        }
    }
}
<commit_msg>removed filterinfo class added static method to filteredresult class refactored to check for empty clause added check for filters that select all,any,*<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Results;

namespace KryptonDotNet
{
    /// <summary>
    ///  Represents a filtered list of items as content and filter info in header values
    ///  : -h krypton-filter-info(json serialized array-[{ "key":key, "value":value },...] )
    /// </summary>
    public class FilteredResult : ResponseMessageResult
    {
        public IQueryable<object> Items { get; }

        public FilteredResult(IQueryable<object> items, HttpActionContext actionContext)
            : base(new HttpResponseMessage(System.Net.HttpStatusCode.OK))
        {
            var filters = HeaderUtil.ResolveFilterInfoHeader(actionContext.Request.Headers);
            if (filters != null)
            {
                StringBuilder filterClause = new StringBuilder(string.Empty);
                foreach (var item in filters.Properties())
                {
                    if (string.Compare(item.Value.ToString(), "all", true) == 0
                        || string.Compare(item.Value.ToString(), "any", true) == 0
                        || string.Compare(item.Value.ToString(), "*", true) == 0
                        || string.IsNullOrEmpty(item.Value.ToString())) continue;

                    if (!string.IsNullOrEmpty(filterClause.ToString()))
                        filterClause.Append(" and ");

                    filterClause.Append(BuildFilterClause(item.Path, item.Value.ToString()));
                }
                items = string.IsNullOrEmpty(filterClause.ToString()) ? items : items.Where(filterClause.ToString());
            }
            Items = items;
            this.Response.Content = new ObjectContent(Items.GetType(), Items, actionContext.ControllerContext.Configuration.Formatters.JsonFormatter);
        }

        private static string BuildFilterClause(string key, string value)
        {
            return $"{key}.ToString() = \"{value}\"";
        }
    }
}

{'fraction_non_alphanumeric': 0.11274397244546498, 'fraction_numerical': 0.0016073478760045925, 'mean_word_length': 2.0981507823613086, 'pattern_counts': {'":': 4, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5676703', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1107, 'n_words': 257}