193 lines
5.9 KiB
C#
Executable File
193 lines
5.9 KiB
C#
Executable File
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
[assembly: DoNotParallelize]
|
|
namespace MarketAlly.AIPlugin.Refactoring.Plugins
|
|
{
|
|
public interface IParameterExtractor
|
|
{
|
|
T GetParameter<T>(IReadOnlyDictionary<string, object> parameters, string key, T defaultValue = default);
|
|
T GetParameter<T>(IReadOnlyDictionary<string, object> parameters, string[] keys, T defaultValue = default);
|
|
List<T> GetListParameter<T>(IReadOnlyDictionary<string, object> parameters, params string[] keys);
|
|
int GetIntParameter(IReadOnlyDictionary<string, object> parameters, string key, int defaultValue = 0);
|
|
double GetDoubleParameter(IReadOnlyDictionary<string, object> parameters, string key, double defaultValue = 0.0);
|
|
bool GetBoolParameter(IReadOnlyDictionary<string, object> parameters, string key, bool defaultValue = false);
|
|
}
|
|
|
|
public class ParameterExtractor : IParameterExtractor
|
|
{
|
|
public T GetParameter<T>(IReadOnlyDictionary<string, object> parameters, string key, T defaultValue = default)
|
|
{
|
|
return GetParameter<T>(parameters, new[] { key }, defaultValue);
|
|
}
|
|
|
|
public T GetParameter<T>(IReadOnlyDictionary<string, object> parameters, string[] keys, T defaultValue = default)
|
|
{
|
|
if (parameters == null)
|
|
return defaultValue;
|
|
|
|
foreach (var key in keys)
|
|
{
|
|
if (parameters.TryGetValue(key, out var value) && value != null)
|
|
{
|
|
try
|
|
{
|
|
if (value is T directValue)
|
|
return directValue;
|
|
|
|
// Try conversion for common types
|
|
if (typeof(T) == typeof(string))
|
|
return (T)(object)value.ToString();
|
|
|
|
if (typeof(T) == typeof(int) && int.TryParse(value.ToString(), out var intValue))
|
|
return (T)(object)intValue;
|
|
|
|
if (typeof(T) == typeof(double) && double.TryParse(value.ToString(), out var doubleValue))
|
|
return (T)(object)doubleValue;
|
|
|
|
if (typeof(T) == typeof(bool) && bool.TryParse(value.ToString(), out var boolValue))
|
|
return (T)(object)boolValue;
|
|
|
|
// Generic conversion attempt
|
|
return (T)Convert.ChangeType(value, typeof(T));
|
|
}
|
|
catch
|
|
{
|
|
// Conversion failed, continue to next key
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
return defaultValue;
|
|
}
|
|
|
|
public List<T> GetListParameter<T>(IReadOnlyDictionary<string, object> parameters, params string[] keys)
|
|
{
|
|
if (parameters == null)
|
|
return new List<T>();
|
|
|
|
foreach (var key in keys)
|
|
{
|
|
if (parameters.TryGetValue(key, out var value) && value != null)
|
|
{
|
|
try
|
|
{
|
|
if (value is List<T> directList)
|
|
return directList;
|
|
|
|
if (value is IEnumerable<object> enumerable)
|
|
{
|
|
var result = new List<T>();
|
|
foreach (var item in enumerable)
|
|
{
|
|
if (item is T directItem)
|
|
result.Add(directItem);
|
|
else if (item != null)
|
|
{
|
|
try
|
|
{
|
|
var convertedItem = (T)Convert.ChangeType(item, typeof(T));
|
|
result.Add(convertedItem);
|
|
}
|
|
catch
|
|
{
|
|
// Skip items that can't be converted
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// Try to convert single value to list
|
|
if (value is T singleValue)
|
|
return new List<T> { singleValue };
|
|
}
|
|
catch
|
|
{
|
|
// Conversion failed, continue to next key
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
return new List<T>();
|
|
}
|
|
|
|
public int GetIntParameter(IReadOnlyDictionary<string, object> parameters, string key, int defaultValue = 0)
|
|
{
|
|
return GetParameter<int>(parameters, key, defaultValue);
|
|
}
|
|
|
|
public double GetDoubleParameter(IReadOnlyDictionary<string, object> parameters, string key, double defaultValue = 0.0)
|
|
{
|
|
return GetParameter<double>(parameters, key, defaultValue);
|
|
}
|
|
|
|
public bool GetBoolParameter(IReadOnlyDictionary<string, object> parameters, string key, bool defaultValue = false)
|
|
{
|
|
return GetParameter<bool>(parameters, key, defaultValue);
|
|
}
|
|
}
|
|
|
|
// Common result types
|
|
public static class PluginResultHelpers
|
|
{
|
|
public static AIPluginResult Success(object data, string message = "Operation completed successfully")
|
|
{
|
|
return new AIPluginResult(data, message);
|
|
}
|
|
|
|
public static AIPluginResult Error(string message, Exception exception = null)
|
|
{
|
|
return new AIPluginResult(exception ?? new InvalidOperationException(message), message);
|
|
}
|
|
|
|
public static AIPluginResult ValidationError(string parameterName, string validationMessage)
|
|
{
|
|
return new AIPluginResult(new ArgumentException(validationMessage, parameterName), $"Validation failed for {parameterName}: {validationMessage}");
|
|
}
|
|
|
|
public static AIPluginResult FileNotFound(string filePath)
|
|
{
|
|
return new AIPluginResult(new FileNotFoundException($"File not found: {filePath}"), $"File not found: {filePath}");
|
|
}
|
|
|
|
public static AIPluginResult InvalidOperation(string operation, string reason)
|
|
{
|
|
return new AIPluginResult(new InvalidOperationException(reason), $"Invalid operation '{operation}': {reason}");
|
|
}
|
|
}
|
|
|
|
// Common constants
|
|
public static class PluginConstants
|
|
{
|
|
public const int DefaultMaxFileSize = 10 * 1024 * 1024; // 10MB
|
|
public const int DefaultTimeout = 30000; // 30 seconds
|
|
public const double DefaultSimilarityThreshold = 0.7;
|
|
public const int DefaultMaxResults = 100;
|
|
|
|
// Common parameter names
|
|
public static class ParameterNames
|
|
{
|
|
public const string FilePath = "filePath";
|
|
public const string ProjectPath = "projectPath";
|
|
public const string ApplyChanges = "applyChanges";
|
|
public const string CreateBackup = "createBackup";
|
|
public const string MaxResults = "maxResults";
|
|
public const string Timeout = "timeout";
|
|
}
|
|
|
|
// Common file extensions
|
|
public static readonly HashSet<string> CSharpFileExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
".cs", ".csx"
|
|
};
|
|
|
|
public static readonly HashSet<string> ProjectFileExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
".csproj", ".sln", ".props", ".targets"
|
|
};
|
|
}
|
|
} |