<commit_before>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using PostSharp.Aspects;
using System.Collections;

namespace CacheAspect
{
    [Serializable]
    public class KeyBuilder
    {
        public static readonly char SPLITTER = '%';

        public string MethodName { get; set; }
        public CacheSettings Settings { get; set; }
        public string GroupName { get; set; }
        public string[] ParameterProperties { get; set; }
        private Dictionary<int, string> _parametersNameValueMapper;
        private ParameterInfo[] _methodParameters;
        public ParameterInfo[] MethodParameters
        {
            get { return _methodParameters; }
            set
            {
                _methodParameters = value;
                TransformParametersIntoNameValueMapper(_methodParameters);
            }
        }

        private void TransformParametersIntoNameValueMapper(ParameterInfo[] methodParameters)
        {
            _parametersNameValueMapper = new Dictionary<int, string>();
            for (var i = 0; i < methodParameters.Count(); i++)
            {
                _parametersNameValueMapper.Add(i, methodParameters[i].Name);
            }
        }

        public string BuildCacheKey(object instance, Arguments arguments)
        {
            StringBuilder cacheKeyBuilder = new StringBuilder();

            // start building a key based on the method name if a group name not set
            if (string.IsNullOrWhiteSpace(GroupName))
            {
                cacheKeyBuilder.Append(MethodName).Append(SPLITTER);
            }
            else
            {
                cacheKeyBuilder.Append(GroupName).Append(SPLITTER);
            }

            if (instance != null)
            {
                cacheKeyBuilder.Append(instance);
                cacheKeyBuilder.Append(";");
            }

            int argIndex;
            switch (Settings)
            {
                case CacheSettings.IgnoreParameters:
                    return cacheKeyBuilder.ToString();
                case CacheSettings.UseId:
                    argIndex = GetArgumentIndexByName("Id");
                    cacheKeyBuilder.Append(arguments.GetArgument(argIndex) ?? "Null").Append(SPLITTER);
                    break;
                case CacheSettings.UseProperty:
                    //argIndex = GetArgumentIndexByName(ParameterProperty);
                    //cacheKeyBuilder.Append(arguments.GetArgument(argIndex) ?? "Null");
                    for (var i = 0; i < ParameterProperties.Length; i++)
                    {
                        argIndex = GetArgumentIndexByName(ParameterProperties[i]);
                        // cacheKeyBuilder.Append( ?? "Null");
                        BuildDefaultKey(arguments.GetArgument(argIndex), cacheKeyBuilder);
                    }
                    break;
                case CacheSettings.Default:
                    for (var i = 0; i < arguments.Count; i++)
                    {
                        BuildDefaultKey(arguments.GetArgument(i), cacheKeyBuilder);
                    }
                    break;
            }

            return cacheKeyBuilder.ToString().TrimEnd(SPLITTER);
        }

        private static void BuildDefaultKey(object argument, StringBuilder cacheKeyBuilder)
        {
            if (argument != null && typeof(ICollection).IsAssignableFrom(argument.GetType()))
            {
                cacheKeyBuilder.Append("{");
                foreach (object o in (ICollection)argument)
                {
                    cacheKeyBuilder.Append(o ?? "Null").Append(SPLITTER);
                }
                cacheKeyBuilder.Append("}");
            }
            else
            {
                cacheKeyBuilder.Append(argument ?? "Null").Append(SPLITTER);
            }
        }

        private int GetArgumentIndexByName(string paramName)<commit_msg>Throw exception when CacheSettings are not supported<commit_after>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using PostSharp.Aspects;
using System.Collections;

namespace CacheAspect
{
    [Serializable]
    public class KeyBuilder
    {
        public static readonly char SPLITTER = '%';

        public string MethodName { get; set; }
        public CacheSettings Settings { get; set; }
        public string GroupName { get; set; }
        public string[] ParameterProperties { get; set; }
        private Dictionary<int, string> _parametersNameValueMapper;
        private ParameterInfo[] _methodParameters;
        public ParameterInfo[] MethodParameters
        {
            get { return _methodParameters; }
            set
            {
                _methodParameters = value;
                TransformParametersIntoNameValueMapper(_methodParameters);
            }
        }

        private void TransformParametersIntoNameValueMapper(ParameterInfo[] methodParameters)
        {
            _parametersNameValueMapper = new Dictionary<int, string>();
            for (var i = 0; i < methodParameters.Count(); i++)
            {
                _parametersNameValueMapper.Add(i, methodParameters[i].Name);
            }
        }

        public string BuildCacheKey(object instance, Arguments arguments)
        {
            StringBuilder cacheKeyBuilder = new StringBuilder();

            // start building a key based on the method name if a group name not set
            if (string.IsNullOrWhiteSpace(GroupName))
            {
                cacheKeyBuilder.Append(MethodName).Append(SPLITTER);
            }
            else
            {
                cacheKeyBuilder.Append(GroupName).Append(SPLITTER);
            }

            if (instance != null)
            {
                cacheKeyBuilder.Append(instance);
                cacheKeyBuilder.Append(";");
            }

            int argIndex;
            switch (Settings)
            {
                case CacheSettings.IgnoreParameters:
                    return cacheKeyBuilder.ToString();
                case CacheSettings.UseId:
                    argIndex = GetArgumentIndexByName("Id");
                    cacheKeyBuilder.Append(arguments.GetArgument(argIndex) ?? "Null").Append(SPLITTER);
                    break;
                case CacheSettings.UseProperty:
                    //argIndex = GetArgumentIndexByName(ParameterProperty);
                    //cacheKeyBuilder.Append(arguments.GetArgument(argIndex) ?? "Null");
                    for (var i = 0; i < ParameterProperties.Length; i++)
                    {
                        argIndex = GetArgumentIndexByName(ParameterProperties[i]);
                        // cacheKeyBuilder.Append( ?? "Null");
                        BuildDefaultKey(arguments.GetArgument(argIndex), cacheKeyBuilder);
                    }
                    break;
                case CacheSettings.Default:
                    for (var i = 0; i < arguments.Count; i++)
                    {
                        BuildDefaultKey(arguments.GetArgument(i), cacheKeyBuilder);
                    }
                    break;
                default:
                    throw new ArgumentException(string.Format("Settings {0} is not supported", Settings));
            }

            return cacheKeyBuilder.ToString().TrimEnd(SPLITTER);
        }

        private static void BuildDefaultKey(object argument, StringBuilder cacheKeyBuilder)
        {
            if (argument != null && typeof(ICollection).IsAssignableFrom(argument.GetType()))
            {
                cacheKeyBuilder.Append("{");
                foreach (object o in (ICollection)argument)
                {
                    cacheKeyBuilder.Append(o ?? "Null").Append(SPLITTER);
                }
                cacheKeyBuilder.Append("}");
            }
            else
            {
                cacheKeyBuilder.Append(argument ?? "Null").Append(SPLITTER);
            }
        }

        private int GetArgumentIndexByName(string paramName)
{'fraction_non_alphanumeric': 0.08428273918902364, 'fraction_numerical': 0.0008575278696557638, 'mean_word_length': 1.6429265134347686, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, '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': '6693697', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1805, 'n_words': 398}