From 476597c9c719284f2078ad88ff45937b87732f6d Mon Sep 17 00:00:00 2001 From: 01Dri Date: Fri, 2 May 2025 23:55:58 -0300 Subject: [PATCH 1/4] Refactor --- MapperAI.sln.DotSettings.user | 4 ++ src/MapperAI.Core/Clients/ClientBaseAI.cs | 18 ----- src/MapperAI.Core/Clients/GeminiClientAI.cs | 70 ------------------- .../Clients/GeminiMapperClient.cs | 63 +++++++++++++++++ .../Clients/Interfaces/IClientAI.cs | 3 +- .../Clients/Interfaces/IClientFactoryAI.cs | 2 +- src/MapperAI.Core/Clients/MapperClientBase.cs | 44 ++++++++++++ ...entFactoryAI.cs => MapperClientFactory.cs} | 12 ++-- .../Clients/Models/ClientResponse.cs | 6 -- ...ration.cs => MapperClientConfiguration.cs} | 2 +- .../Clients/Models/MapperClientResponse.cs | 11 +++ src/MapperAI.Core/Clients/OllamaClientAI.cs | 60 ---------------- .../Clients/OllamaMapperClient.cs | 44 ++++++++++++ .../Exceptions/ConverterIAException.cs | 12 ---- .../Exceptions/MapperException.cs | 12 ++++ .../MapperRequestStatusException.cs | 12 ++++ .../Exceptions/RequestStatusIAException.cs | 12 ---- .../Initializers/BaseModelJson.cs | 13 ---- src/MapperAI.Core/Initializers/BaseTypes.cs | 7 -- .../Initializers/DependencyInitializer.cs | 18 ++++- .../DependencyInitializerFacade.cs | 30 -------- .../Initializers/EntityInitializer.cs | 37 ---------- .../Interfaces/IDependencyInitializer.cs | 8 --- src/MapperAI.Core/Mappers/ClassMapper.cs | 8 +-- src/MapperAI.Core/Mappers/FileMapper.cs | 33 ++++----- .../Mappers/Interfaces/IClassMapper.cs | 2 +- .../Mappers/Interfaces/IFileMapper.cs | 2 +- .../Mappers/Interfaces/IPDFMapper.cs | 2 +- .../Mappers/Models/FileMapperConfiguration.cs | 4 +- ...{ClassContent.cs => MapperClassContent.cs} | 2 +- src/MapperAI.Core/Mappers/PdfMapper.cs | 11 ++- .../Interfaces/IMapperSerializer.cs | 3 +- .../Serializers/MapperSerializer.cs | 2 +- test/MapperAI.Test/ClassMapperTests.cs | 6 +- test/MapperAI.Test/FileMapperTests.cs | 14 ++-- .../MapperAI.Test}/FoldersHelpers.cs | 0 test/MapperAI.Test/MappedClasses/Carro.js | 36 ---------- .../MappedClasses/ContaBancaria.js | 35 ---------- test/MapperAI.Test/MappedClasses/Student.js | 29 -------- test/MapperAI.Test/MappedClasses/Usuario.js | 41 ----------- test/MapperAI.Test/PdfMapperTests.cs | 7 +- 41 files changed, 259 insertions(+), 478 deletions(-) delete mode 100644 src/MapperAI.Core/Clients/ClientBaseAI.cs delete mode 100644 src/MapperAI.Core/Clients/GeminiClientAI.cs create mode 100644 src/MapperAI.Core/Clients/GeminiMapperClient.cs create mode 100644 src/MapperAI.Core/Clients/MapperClientBase.cs rename src/MapperAI.Core/Clients/{ClientFactoryAI.cs => MapperClientFactory.cs} (50%) delete mode 100644 src/MapperAI.Core/Clients/Models/ClientResponse.cs rename src/MapperAI.Core/Clients/Models/{ClientConfiguration.cs => MapperClientConfiguration.cs} (84%) create mode 100644 src/MapperAI.Core/Clients/Models/MapperClientResponse.cs delete mode 100644 src/MapperAI.Core/Clients/OllamaClientAI.cs create mode 100644 src/MapperAI.Core/Clients/OllamaMapperClient.cs delete mode 100644 src/MapperAI.Core/Exceptions/ConverterIAException.cs create mode 100644 src/MapperAI.Core/Exceptions/MapperException.cs create mode 100644 src/MapperAI.Core/Exceptions/MapperRequestStatusException.cs delete mode 100644 src/MapperAI.Core/Exceptions/RequestStatusIAException.cs delete mode 100644 src/MapperAI.Core/Initializers/BaseModelJson.cs delete mode 100644 src/MapperAI.Core/Initializers/BaseTypes.cs delete mode 100644 src/MapperAI.Core/Initializers/DependencyInitializerFacade.cs delete mode 100644 src/MapperAI.Core/Initializers/EntityInitializer.cs delete mode 100644 src/MapperAI.Core/Initializers/Interfaces/IDependencyInitializer.cs rename src/MapperAI.Core/Mappers/Models/{ClassContent.cs => MapperClassContent.cs} (79%) rename {src/MapperAI.Core/Helpers => test/MapperAI.Test}/FoldersHelpers.cs (100%) delete mode 100644 test/MapperAI.Test/MappedClasses/Carro.js delete mode 100644 test/MapperAI.Test/MappedClasses/ContaBancaria.js delete mode 100644 test/MapperAI.Test/MappedClasses/Student.js delete mode 100644 test/MapperAI.Test/MappedClasses/Usuario.js diff --git a/MapperAI.sln.DotSettings.user b/MapperAI.sln.DotSettings.user index be9dad4..ee45d31 100644 --- a/MapperAI.sln.DotSettings.user +++ b/MapperAI.sln.DotSettings.user @@ -1,7 +1,11 @@  + ForceIncluded <SessionState ContinuousTestingMode="0" IsActive="True" Name="Test_Should_Create_4_Files_With_CSharp_Extension" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> <TestAncestor> <TestId>xUnit::8B3E109D-96CA-4B6D-B379-6AF70646DC25::net8.0::MapperAI.Test.FileMapperTests.Test_Should_Create_4_Files_With_CSharp_Extension</TestId> + <TestId>xUnit::8B3E109D-96CA-4B6D-B379-6AF70646DC25::net8.0::MapperAI.Test.ClassMapperTests.Test1</TestId> + <TestId>xUnit::8B3E109D-96CA-4B6D-B379-6AF70646DC25::net8.0::MapperAI.Test.PdfMapperTests.Test1</TestId> + <TestId>xUnit::8B3E109D-96CA-4B6D-B379-6AF70646DC25::net8.0::MapperAI.Test.FileMapperTests.Test_Should_Create_4_Files_With_Go_Extension</TestId> </TestAncestor> </SessionState> <SessionState ContinuousTestingMode="0" Name="Test1" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> diff --git a/src/MapperAI.Core/Clients/ClientBaseAI.cs b/src/MapperAI.Core/Clients/ClientBaseAI.cs deleted file mode 100644 index c23f9d6..0000000 --- a/src/MapperAI.Core/Clients/ClientBaseAI.cs +++ /dev/null @@ -1,18 +0,0 @@ -using MapperAI.Core.Clients.Models; -using MapperIA.Core.Serializers.Interfaces; - -namespace MapperAI.Core.Clients; - -public class ClientBaseAI -{ - protected ClientConfiguration ClientConfiguration; - protected HttpClient HttpClient; - protected IMapperSerializer Serializer; - - public ClientBaseAI(ClientConfiguration clientConfiguration, IMapperSerializer serializer) - { - ClientConfiguration = clientConfiguration; - HttpClient = new HttpClient(); - Serializer = serializer; - } -} \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/GeminiClientAI.cs b/src/MapperAI.Core/Clients/GeminiClientAI.cs deleted file mode 100644 index e3f5c00..0000000 --- a/src/MapperAI.Core/Clients/GeminiClientAI.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System.Text; -using System.Text.Json; -using MapperAI.Core.Clients.Interfaces; -using MapperAI.Core.Clients.Models; -using MapperAI.Core.Exceptions; -using MapperIA.Core.Serializers.Interfaces; - -namespace MapperAI.Core.Clients; - -public class GeminiClientAI : ClientBaseAI ,IClientAI -{ - private const string EndpointBase = "https://generativelanguage.googleapis.com/v1beta"; - - public GeminiClientAI(ClientConfiguration clientConfiguration, IMapperSerializer serializer) : base(clientConfiguration, serializer) - { - } - - public async Task SendAsync(string prompt, CancellationToken cancellationToken) - { - var payload = new - { - contents = new[] - { - new - { - parts = new[] - { - new { text = prompt } - } - } - } - }; - var payloadJson = Serializer.Serialize(payload); - var mediaTypeRequest = new StringContent(payloadJson, Encoding.UTF8, "application/json"); - var endpoint = $"{EndpointBase}/models/{ClientConfiguration.Model}:generateContent?key={ClientConfiguration.ApiKey}"; - - try - { - var response = await HttpClient.PostAsync(endpoint, mediaTypeRequest, cancellationToken); - if (!response.IsSuccessStatusCode) - { - throw new RequestStatusIAException($"Request failed with status: {response.StatusCode}"); - } - - var result = await response.Content.ReadAsStringAsync(); - using var doc = JsonDocument.Parse(result); - var text = doc - .RootElement - .GetProperty("candidates")[0] - .GetProperty("content") - .GetProperty("parts")[0] - .GetProperty("text") - .GetString(); - - if (text == null) throw new Exception("Error"); - var cleanedJson = text - .Replace("```json", string.Empty) - .Replace("```", string.Empty) - .Replace("\\$", "\\\\$") - .Trim(); - - // var textSanitized = SanitizeJson(text); - return new ClientResponse { Value = cleanedJson}; - } - catch (Exception ex) - { - throw new ConverterIAException($"An error occurred during processing: {ex.Message}"); - } - } -} \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/GeminiMapperClient.cs b/src/MapperAI.Core/Clients/GeminiMapperClient.cs new file mode 100644 index 0000000..340affc --- /dev/null +++ b/src/MapperAI.Core/Clients/GeminiMapperClient.cs @@ -0,0 +1,63 @@ +using System.Text.Json; +using MapperAI.Core.Clients.Interfaces; +using MapperAI.Core.Clients.Models; +using MapperAI.Core.Exceptions; +using MapperAI.Core.Serializers.Interfaces; + +namespace MapperAI.Core.Clients; + +public class GeminiMapperClient : MapperClientBase, IClientAI +{ + private const string EndpointBase = "https://generativelanguage.googleapis.com/v1beta"; + + public GeminiMapperClient(MapperClientConfiguration mapperClientConfiguration, IMapperSerializer serializer) : base( + mapperClientConfiguration, serializer) + { + } + + public async Task SendAsync(string prompt, CancellationToken cancellationToken) + { + dynamic payload = CreatePayload(prompt); + string endpoint = + $"{EndpointBase}/models/{MapperClientConfiguration.Model}:generateContent?key={MapperClientConfiguration.ApiKey}"; + using JsonDocument result = await GetAsync(endpoint, payload, cancellationToken); + string? resultParsed = ParseResponse(result); + if (resultParsed == null) throw new MapperException("Result request from AI is null."); + string cleanedResult = resultParsed + .Replace("```json", string.Empty) + .Replace("```", string.Empty) + .Replace("\\$", "\\\\$") + .Trim(); + + return new MapperClientResponse(cleanedResult); + } + + private dynamic CreatePayload(string promptText) + { + return new + { + contents = new[] + { + new + { + parts = new[] + { + new { text = promptText } + } + } + } + }; +} + + +private string? ParseResponse(JsonDocument response) + => response + .RootElement + .GetProperty("candidates")[0] + .GetProperty("content") + .GetProperty("parts")[0] + .GetProperty("text") + .GetString(); + + +} \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/Interfaces/IClientAI.cs b/src/MapperAI.Core/Clients/Interfaces/IClientAI.cs index 64e8f1b..333f024 100644 --- a/src/MapperAI.Core/Clients/Interfaces/IClientAI.cs +++ b/src/MapperAI.Core/Clients/Interfaces/IClientAI.cs @@ -4,5 +4,6 @@ namespace MapperAI.Core.Clients.Interfaces; public interface IClientAI { - Task SendAsync(string prompt, CancellationToken cancellationToken); + Task SendAsync(string prompt, CancellationToken cancellationToken); + } \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/Interfaces/IClientFactoryAI.cs b/src/MapperAI.Core/Clients/Interfaces/IClientFactoryAI.cs index 93981e2..0c07ceb 100644 --- a/src/MapperAI.Core/Clients/Interfaces/IClientFactoryAI.cs +++ b/src/MapperAI.Core/Clients/Interfaces/IClientFactoryAI.cs @@ -4,5 +4,5 @@ namespace MapperAI.Core.Clients.Interfaces; public interface IClientFactoryAI { - IClientAI CreateClient(ClientConfiguration configuration); + IClientAI CreateClient(MapperClientConfiguration configuration); } \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/MapperClientBase.cs b/src/MapperAI.Core/Clients/MapperClientBase.cs new file mode 100644 index 0000000..091ede7 --- /dev/null +++ b/src/MapperAI.Core/Clients/MapperClientBase.cs @@ -0,0 +1,44 @@ +using System.Text; +using System.Text.Json; +using MapperAI.Core.Clients.Models; +using MapperAI.Core.Exceptions; +using MapperAI.Core.Serializers.Interfaces; + +namespace MapperAI.Core.Clients; + +public abstract class MapperClientBase +{ + protected MapperClientConfiguration MapperClientConfiguration; + private readonly HttpClient _httpClient; + private readonly IMapperSerializer _serializer; + + protected MapperClientBase(MapperClientConfiguration mapperClientConfiguration, IMapperSerializer serializer) + { + MapperClientConfiguration = mapperClientConfiguration; + _httpClient = new HttpClient(); + _serializer = serializer; + } + + protected async Task GetAsync(string endpoint, object body, CancellationToken cancellationToken = default) + { + try + { + string json = _serializer.Serialize(body); + StringContent mediaTypeRequest = new(json, Encoding.UTF8, "application/json"); + HttpResponseMessage response = await _httpClient.PostAsync(endpoint, mediaTypeRequest, cancellationToken); + if (!response.IsSuccessStatusCode) + { + throw new MapperRequestStatusException($"Request failed with status: {response.StatusCode}"); + } + + string result = await response.Content.ReadAsStringAsync(); + return JsonDocument.Parse(result); + } + catch (Exception ex) + { + throw new MapperException($"An error occurred during processing: {ex.Message}"); + } + + } + +} \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/ClientFactoryAI.cs b/src/MapperAI.Core/Clients/MapperClientFactory.cs similarity index 50% rename from src/MapperAI.Core/Clients/ClientFactoryAI.cs rename to src/MapperAI.Core/Clients/MapperClientFactory.cs index c95adfa..ea7af4e 100644 --- a/src/MapperAI.Core/Clients/ClientFactoryAI.cs +++ b/src/MapperAI.Core/Clients/MapperClientFactory.cs @@ -1,28 +1,28 @@ using MapperAI.Core.Clients.Interfaces; using MapperAI.Core.Clients.Models; using MapperAI.Core.Enums; -using MapperIA.Core.Serializers.Interfaces; +using MapperAI.Core.Serializers.Interfaces; namespace MapperAI.Core.Clients; -public class ClientFactoryAI : IClientFactoryAI +public class MapperClientFactory : IClientFactoryAI { private readonly IMapperSerializer _serializer; - public ClientFactoryAI(IMapperSerializer serializer) + public MapperClientFactory(IMapperSerializer serializer) { _serializer = serializer; } - public IClientAI CreateClient(ClientConfiguration configuration) + public IClientAI CreateClient(MapperClientConfiguration configuration) { switch (configuration.Type) { case ModelType.Ollama: - return new OllamaClientAI(configuration, _serializer); + return new OllamaMapperClient(configuration, _serializer); default: - return new GeminiClientAI(configuration, _serializer); + return new GeminiMapperClient(configuration, _serializer); } } } \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/Models/ClientResponse.cs b/src/MapperAI.Core/Clients/Models/ClientResponse.cs deleted file mode 100644 index eb56345..0000000 --- a/src/MapperAI.Core/Clients/Models/ClientResponse.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace MapperAI.Core.Clients.Models; - -public class ClientResponse -{ - public string Value { get; set; } -} \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/Models/ClientConfiguration.cs b/src/MapperAI.Core/Clients/Models/MapperClientConfiguration.cs similarity index 84% rename from src/MapperAI.Core/Clients/Models/ClientConfiguration.cs rename to src/MapperAI.Core/Clients/Models/MapperClientConfiguration.cs index b12b196..bcdefc9 100644 --- a/src/MapperAI.Core/Clients/Models/ClientConfiguration.cs +++ b/src/MapperAI.Core/Clients/Models/MapperClientConfiguration.cs @@ -2,7 +2,7 @@ namespace MapperAI.Core.Clients.Models; -public class ClientConfiguration +public class MapperClientConfiguration { public required string Model { get; set; } public string? ApiKey { get; set; } diff --git a/src/MapperAI.Core/Clients/Models/MapperClientResponse.cs b/src/MapperAI.Core/Clients/Models/MapperClientResponse.cs new file mode 100644 index 0000000..e7b02c2 --- /dev/null +++ b/src/MapperAI.Core/Clients/Models/MapperClientResponse.cs @@ -0,0 +1,11 @@ +namespace MapperAI.Core.Clients.Models; + +public class MapperClientResponse +{ + public MapperClientResponse(string value) + { + Value = value; + } + + public string Value { get; } +} \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/OllamaClientAI.cs b/src/MapperAI.Core/Clients/OllamaClientAI.cs deleted file mode 100644 index cf1248c..0000000 --- a/src/MapperAI.Core/Clients/OllamaClientAI.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System.Text; -using System.Text.Json; -using System.Text.RegularExpressions; -using MapperAI.Core.Clients.Interfaces; -using MapperAI.Core.Clients.Models; -using MapperAI.Core.Exceptions; -using MapperIA.Core.Serializers.Interfaces; - -namespace MapperAI.Core.Clients; - -public class OllamaClientAI : ClientBaseAI ,IClientAI -{ - private const string Endpoint = "http://localhost:11434/api/generate"; - - public OllamaClientAI(ClientConfiguration clientConfiguration, IMapperSerializer serializer) : base(clientConfiguration, serializer) - { - } - - - public async Task SendAsync(string prompt, CancellationToken cancellationToken) - { - var payload = new - { - model = ClientConfiguration.Model, - prompt, - stream = false - }; - - var json = Serializer.Serialize(payload); - var mediaTypeRequest = new StringContent(json, Encoding.UTF8, "application/json"); - - try - { - HttpResponseMessage httpResponse = await HttpClient.PostAsync(Endpoint, mediaTypeRequest, cancellationToken); - if (!httpResponse.IsSuccessStatusCode) - { - throw new RequestStatusIAException($"Request failed with status: {httpResponse.StatusCode}"); - } - - string responseData = await httpResponse.Content.ReadAsStringAsync(cancellationToken); - using var jsonDoc = JsonDocument.Parse(responseData); - var content = jsonDoc.RootElement.GetProperty("response").GetString(); - string sanitized = RemoveThinkSections(content); - - return new ClientResponse { Value = sanitized ?? string.Empty }; - } - catch (Exception ex) when (!(ex is RequestStatusIAException)) - { - throw new ConverterIAException($"Erro ao processar a resposta da LLM local: {ex.Message}"); - } - } - private static string RemoveThinkSections(string input) - { - if (string.IsNullOrWhiteSpace(input)) return string.Empty; - // Remove tags and their content - string pattern = "[\\s\\S]*?"; - var cleaned = Regex.Replace(input, pattern, string.Empty, RegexOptions.IgnoreCase); - return cleaned.Trim(); - } -} \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/OllamaMapperClient.cs b/src/MapperAI.Core/Clients/OllamaMapperClient.cs new file mode 100644 index 0000000..37710ba --- /dev/null +++ b/src/MapperAI.Core/Clients/OllamaMapperClient.cs @@ -0,0 +1,44 @@ +using System.Text.Json; +using System.Text.RegularExpressions; +using MapperAI.Core.Clients.Interfaces; +using MapperAI.Core.Clients.Models; +using MapperAI.Core.Exceptions; +using MapperAI.Core.Serializers.Interfaces; + +namespace MapperAI.Core.Clients; + +public class OllamaMapperClient : MapperClientBase ,IClientAI +{ + private const string Endpoint = "http://localhost:11434/api/generate"; + + public OllamaMapperClient(MapperClientConfiguration mapperClientConfiguration, IMapperSerializer serializer) : base(mapperClientConfiguration, serializer) + { + } + + + public async Task SendAsync(string prompt, CancellationToken cancellationToken) + { + dynamic payload = CreatePayload(prompt); + using JsonDocument jsonDoc = await GetAsync(Endpoint, payload, cancellationToken); + string? content = jsonDoc.RootElement.GetProperty("response").GetString(); + if (content == null) throw new MapperException("Result request from AI is null."); + return new MapperClientResponse(RemoveThinkSections(content)); + } + + private dynamic CreatePayload(string promptText) + { + return new + { + model = MapperClientConfiguration.Model, + promptText, + stream = false + }; + } + private static string RemoveThinkSections(string input) + { + if (string.IsNullOrWhiteSpace(input)) return string.Empty; + string pattern = "[\\s\\S]*?"; + var cleaned = Regex.Replace(input, pattern, string.Empty, RegexOptions.IgnoreCase); + return cleaned.Trim(); + } +} \ No newline at end of file diff --git a/src/MapperAI.Core/Exceptions/ConverterIAException.cs b/src/MapperAI.Core/Exceptions/ConverterIAException.cs deleted file mode 100644 index 51339dc..0000000 --- a/src/MapperAI.Core/Exceptions/ConverterIAException.cs +++ /dev/null @@ -1,12 +0,0 @@ - -namespace MapperAI.Core.Exceptions; - -public class ConverterIAException : Exception -{ - public ConverterIAException(string msg) - :base(msg) - { - - } - -} \ No newline at end of file diff --git a/src/MapperAI.Core/Exceptions/MapperException.cs b/src/MapperAI.Core/Exceptions/MapperException.cs new file mode 100644 index 0000000..250f3de --- /dev/null +++ b/src/MapperAI.Core/Exceptions/MapperException.cs @@ -0,0 +1,12 @@ + +namespace MapperAI.Core.Exceptions; + +public class MapperException : Exception +{ + public MapperException(string msg) + :base(msg) + { + + } + +} \ No newline at end of file diff --git a/src/MapperAI.Core/Exceptions/MapperRequestStatusException.cs b/src/MapperAI.Core/Exceptions/MapperRequestStatusException.cs new file mode 100644 index 0000000..3973066 --- /dev/null +++ b/src/MapperAI.Core/Exceptions/MapperRequestStatusException.cs @@ -0,0 +1,12 @@ + +namespace MapperAI.Core.Exceptions; + +public class MapperRequestStatusException : Exception +{ + public MapperRequestStatusException(string msg) + :base(msg) + { + + } + +} \ No newline at end of file diff --git a/src/MapperAI.Core/Exceptions/RequestStatusIAException.cs b/src/MapperAI.Core/Exceptions/RequestStatusIAException.cs deleted file mode 100644 index 4d23fe5..0000000 --- a/src/MapperAI.Core/Exceptions/RequestStatusIAException.cs +++ /dev/null @@ -1,12 +0,0 @@ - -namespace MapperAI.Core.Exceptions; - -public class RequestStatusIAException : Exception -{ - public RequestStatusIAException(string msg) - :base(msg) - { - - } - -} \ No newline at end of file diff --git a/src/MapperAI.Core/Initializers/BaseModelJson.cs b/src/MapperAI.Core/Initializers/BaseModelJson.cs deleted file mode 100644 index 98345ed..0000000 --- a/src/MapperAI.Core/Initializers/BaseModelJson.cs +++ /dev/null @@ -1,13 +0,0 @@ - -namespace MapperAI.Core.Initializers; - -public class BaseModelJson -{ - public string BaseJson { get; set; } - public List Types { get; set; } = new List(); - - public BaseModelJson(string baseJson) - { - BaseJson = baseJson; - } -} \ No newline at end of file diff --git a/src/MapperAI.Core/Initializers/BaseTypes.cs b/src/MapperAI.Core/Initializers/BaseTypes.cs deleted file mode 100644 index 8774f09..0000000 --- a/src/MapperAI.Core/Initializers/BaseTypes.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace MapperAI.Core.Initializers; - -public class BaseTypes -{ - public string Name { get; set; } - public string Type { get; set; } -} \ No newline at end of file diff --git a/src/MapperAI.Core/Initializers/DependencyInitializer.cs b/src/MapperAI.Core/Initializers/DependencyInitializer.cs index 2f82fa2..8f7154e 100644 --- a/src/MapperAI.Core/Initializers/DependencyInitializer.cs +++ b/src/MapperAI.Core/Initializers/DependencyInitializer.cs @@ -1,11 +1,23 @@ using System.Reflection; -using MapperAI.Core.Initializers.Interfaces; namespace MapperAI.Core.Initializers; -public class DependencyInitializer : IDependencyInitializer +public class DependencyInitializer { - public void InitializeDependencyProperties(Type? itemType, PropertyInfo property, object obj) + + public static void Initialize(object obj) + { + var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); + var genericProperties = properties.Where(x => x.PropertyType.IsGenericType); + foreach (var property in genericProperties) + { + foreach (var itemType in property.PropertyType.GetGenericArguments()) + { + InitializeDependencyProperties(itemType, property, obj); + } + } + } + private static void InitializeDependencyProperties(Type? itemType, PropertyInfo property, object obj) { if (itemType == null) throw new ArgumentNullException(nameof(itemType)); diff --git a/src/MapperAI.Core/Initializers/DependencyInitializerFacade.cs b/src/MapperAI.Core/Initializers/DependencyInitializerFacade.cs deleted file mode 100644 index c2a9a95..0000000 --- a/src/MapperAI.Core/Initializers/DependencyInitializerFacade.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System.Reflection; -using MapperAI.Core.Initializers.Interfaces; - -namespace MapperAI.Core.Initializers; - -public class DependencyInitializerFacade -{ - private object _obj; - private readonly IDependencyInitializer _dependencyInitializer; - - public DependencyInitializerFacade(object obj, IDependencyInitializer dependencyInitializer) - { - _obj = obj; - _dependencyInitializer = dependencyInitializer; - } - - - public void Initialize() - { - var properties = _obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); - var genericProperties = properties.Where(x => x.PropertyType.IsGenericType); - foreach (var property in genericProperties) - { - foreach (var itemType in property.PropertyType.GetGenericArguments()) - { - _dependencyInitializer.InitializeDependencyProperties(itemType, property, _obj); - } - } - } -} \ No newline at end of file diff --git a/src/MapperAI.Core/Initializers/EntityInitializer.cs b/src/MapperAI.Core/Initializers/EntityInitializer.cs deleted file mode 100644 index 30cbad5..0000000 --- a/src/MapperAI.Core/Initializers/EntityInitializer.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System.Reflection; - -namespace MapperAI.Core.Initializers; - -public static class EntityInitializer -{ - - public static BaseModelJson InitializeBaseModel(string objJson) - { - Type type = typeof(T); - PropertyInfo[] properties = type.GetProperties(); - BaseModelJson baseModelJson = new BaseModelJson(objJson); - - foreach (var property in properties) - { - baseModelJson.Types.Add(new BaseTypes() - { - Name = property.Name, - Type = property.PropertyType.ToString() - }); - } - - return baseModelJson; - } - public static void CopyEntityProperties(T? source, T destination) - { - var properties = typeof(T).GetProperties(); - foreach (var property in properties) - { - if (property.CanWrite) - { - var value = property.GetValue(source); - property.SetValue(destination, value); - } - } - } -} \ No newline at end of file diff --git a/src/MapperAI.Core/Initializers/Interfaces/IDependencyInitializer.cs b/src/MapperAI.Core/Initializers/Interfaces/IDependencyInitializer.cs deleted file mode 100644 index cb797ba..0000000 --- a/src/MapperAI.Core/Initializers/Interfaces/IDependencyInitializer.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System.Reflection; - -namespace MapperAI.Core.Initializers.Interfaces; - -public interface IDependencyInitializer -{ - void InitializeDependencyProperties(Type? itemType, PropertyInfo propertyInfo, object obj); -} \ No newline at end of file diff --git a/src/MapperAI.Core/Mappers/ClassMapper.cs b/src/MapperAI.Core/Mappers/ClassMapper.cs index ca4c533..574aadd 100644 --- a/src/MapperAI.Core/Mappers/ClassMapper.cs +++ b/src/MapperAI.Core/Mappers/ClassMapper.cs @@ -1,7 +1,7 @@ using MapperAI.Core.Clients.Interfaces; using MapperAI.Core.Clients.Models; using MapperAI.Core.Mappers.Interfaces; -using MapperIA.Core.Serializers.Interfaces; +using MapperAI.Core.Serializers.Interfaces; namespace MapperAI.Core.Mappers; @@ -17,12 +17,12 @@ public ClassMapper(IMapperSerializer serializer, IClientFactoryAI clientFactoryA } - public async Task MapAsync(T origin, ClientConfiguration configuration, CancellationToken cancellationToken = default) where T : class where TK : class, new() + public async Task MapAsync(T origin, MapperClientConfiguration configuration, CancellationToken cancellationToken = default) where T : class where TK : class, new() { - IClientAI iai = _clientFactoryAi.CreateClient(configuration); + IClientAI client = _clientFactoryAi.CreateClient(configuration); TK destinyObject = new TK(); string prompt = CreatePrompt(_serializer.Serialize(origin), destinyObject); - var result = await iai.SendAsync(prompt, cancellationToken); + var result = await client.SendAsync(prompt, cancellationToken); return _serializer.Deserialize(result.Value); } diff --git a/src/MapperAI.Core/Mappers/FileMapper.cs b/src/MapperAI.Core/Mappers/FileMapper.cs index 889041e..7da2738 100644 --- a/src/MapperAI.Core/Mappers/FileMapper.cs +++ b/src/MapperAI.Core/Mappers/FileMapper.cs @@ -3,7 +3,7 @@ using MapperAI.Core.Clients.Models; using MapperAI.Core.Mappers.Interfaces; using MapperAI.Core.Mappers.Models; -using MapperIA.Core.Serializers.Interfaces; +using MapperAI.Core.Serializers.Interfaces; namespace MapperAI.Core.Mappers; @@ -19,27 +19,25 @@ public FileMapper(IClientFactoryAI clientFactoryAi, IMapperSerializer serializer _serializer = serializer; } - public async Task MapAsync(FileMapperConfiguration configuration, ClientConfiguration clientConfiguration, + public async Task MapAsync(FileMapperConfiguration configuration, MapperClientConfiguration mapperClientConfiguration, CancellationToken cancellationToken = default) { - IClientAI client = _clientFactoryAi.CreateClient(clientConfiguration); - List filesToMap = GetFilesToMap(configuration); + IClientAI client = _clientFactoryAi.CreateClient(mapperClientConfiguration); + List filesToMap = GetFilesToMap(configuration); string prompt = CreatePrompt(filesToMap, configuration); - ClientResponse result = await client.SendAsync(prompt, cancellationToken); - List? contents = _serializer.Deserialize>(result.Value); + MapperClientResponse result = await client.SendAsync(prompt, cancellationToken); + List? contents = _serializer.Deserialize>(result.Value); if (contents != null && contents.Any()) { CreateFiles(contents, configuration.OutputFolder); + return; } - else - { - throw new SerializationException("Failed to serialize contents"); - } + throw new SerializationException("Failed to serialize contents"); } - private void CreateFiles(List contents, string outputFolderPath) + private void CreateFiles(List contents, string outputFolderPath) { - if (!Directory.Exists(outputFolderPath)) + if (!Directory.Exists(outputFolderPath)) { Directory.CreateDirectory(outputFolderPath); } @@ -51,7 +49,7 @@ private void CreateFiles(List contents, string outputFolderPath) }); } - private string CreatePrompt(List filesToMap, FileMapperConfiguration configuration) + private string CreatePrompt(List filesToMap, FileMapperConfiguration configuration) { var serializedFiles = _serializer.Serialize(filesToMap); @@ -103,25 +101,24 @@ private string CreatePrompt(List filesToMap, FileMapperConfigurati "; } - private List GetFilesToMap(FileMapperConfiguration configuration) + private List GetFilesToMap(FileMapperConfiguration configuration) { string path; if (configuration.IsUniqueClass) { if (configuration.FileName == null) throw new ArgumentException("FileName is required!"); path = Path.Combine(configuration.InputFolder, configuration.FileName); - var content = new ClassContent() + var content = new MapperClassContent() { Name = Path.GetFileName(configuration.FileName), Content = File.ReadAllText(path) }; - return new List() { content }; + return new List() { content }; } - var files = Directory.GetFiles(configuration.InputFolder); return Directory.GetFiles(configuration.InputFolder) .Where(file => file.Contains(".")) - .Select(x => new ClassContent() + .Select(x => new MapperClassContent() { Name = Path.GetFileName(x), Content = File.ReadAllText(x) diff --git a/src/MapperAI.Core/Mappers/Interfaces/IClassMapper.cs b/src/MapperAI.Core/Mappers/Interfaces/IClassMapper.cs index 6e8a2d3..aaa7353 100644 --- a/src/MapperAI.Core/Mappers/Interfaces/IClassMapper.cs +++ b/src/MapperAI.Core/Mappers/Interfaces/IClassMapper.cs @@ -4,7 +4,7 @@ namespace MapperAI.Core.Mappers.Interfaces; public interface IClassMapper { - Task MapAsync(T origin, ClientConfiguration configuration, CancellationToken cancellationToken = default) + Task MapAsync(T origin, MapperClientConfiguration configuration, CancellationToken cancellationToken = default) where T : class where TK : class, new(); diff --git a/src/MapperAI.Core/Mappers/Interfaces/IFileMapper.cs b/src/MapperAI.Core/Mappers/Interfaces/IFileMapper.cs index 18ff50c..d043ff7 100644 --- a/src/MapperAI.Core/Mappers/Interfaces/IFileMapper.cs +++ b/src/MapperAI.Core/Mappers/Interfaces/IFileMapper.cs @@ -8,7 +8,7 @@ public interface IFileMapper Task MapAsync ( FileMapperConfiguration configuration, - ClientConfiguration clientConfiguration, + MapperClientConfiguration mapperClientConfiguration, CancellationToken cancellationToken = default ); } \ No newline at end of file diff --git a/src/MapperAI.Core/Mappers/Interfaces/IPDFMapper.cs b/src/MapperAI.Core/Mappers/Interfaces/IPDFMapper.cs index 3ed8e6c..eb8ffcb 100644 --- a/src/MapperAI.Core/Mappers/Interfaces/IPDFMapper.cs +++ b/src/MapperAI.Core/Mappers/Interfaces/IPDFMapper.cs @@ -4,7 +4,7 @@ namespace MapperAI.Core.Mappers.Interfaces; public interface IPDFMapper { - Task MapAsync(string pdfPath, ClientConfiguration configuration, CancellationToken cancellationToken = default) + Task MapAsync(string pdfPath, MapperClientConfiguration configuration, CancellationToken cancellationToken = default) where T : class, new(); } \ No newline at end of file diff --git a/src/MapperAI.Core/Mappers/Models/FileMapperConfiguration.cs b/src/MapperAI.Core/Mappers/Models/FileMapperConfiguration.cs index 221c4c1..1e1c8cc 100644 --- a/src/MapperAI.Core/Mappers/Models/FileMapperConfiguration.cs +++ b/src/MapperAI.Core/Mappers/Models/FileMapperConfiguration.cs @@ -1,6 +1,4 @@ -using MapperAI.Core.Helpers; - -namespace MapperAI.Core.Mappers.Models; +namespace MapperAI.Core.Mappers.Models; public class FileMapperConfiguration { diff --git a/src/MapperAI.Core/Mappers/Models/ClassContent.cs b/src/MapperAI.Core/Mappers/Models/MapperClassContent.cs similarity index 79% rename from src/MapperAI.Core/Mappers/Models/ClassContent.cs rename to src/MapperAI.Core/Mappers/Models/MapperClassContent.cs index aa941a6..ad1faf5 100644 --- a/src/MapperAI.Core/Mappers/Models/ClassContent.cs +++ b/src/MapperAI.Core/Mappers/Models/MapperClassContent.cs @@ -1,7 +1,7 @@  namespace MapperAI.Core.Mappers.Models; -public class ClassContent +public class MapperClassContent { public string Name { get; set; } public string Content { get; set; } diff --git a/src/MapperAI.Core/Mappers/PdfMapper.cs b/src/MapperAI.Core/Mappers/PdfMapper.cs index a7fbc4b..1db3ef4 100644 --- a/src/MapperAI.Core/Mappers/PdfMapper.cs +++ b/src/MapperAI.Core/Mappers/PdfMapper.cs @@ -4,7 +4,7 @@ using MapperAI.Core.Clients.Models; using MapperAI.Core.Initializers; using MapperAI.Core.Mappers.Interfaces; -using MapperIA.Core.Serializers.Interfaces; +using MapperAI.Core.Serializers.Interfaces; namespace MapperAI.Core.Mappers; @@ -20,17 +20,14 @@ public PdfMapper(IMapperSerializer serializer, IClientFactoryAI clientFactoryAi) } - public async Task MapAsync(string pdfPath, ClientConfiguration configuration, CancellationToken cancellationToken = default) where T : class, new() + public async Task MapAsync(string pdfPath, MapperClientConfiguration configuration, CancellationToken cancellationToken = default) where T : class, new() { IClientAI iai = _clientFactoryAi.CreateClient(configuration); string pdfContent = ExtractPdfContent(pdfPath); T destinyObject = new T(); - - // Ajustar esse codigo de initializer - new DependencyInitializerFacade(destinyObject, new DependencyInitializer()) - .Initialize(); + DependencyInitializer.Initialize(destinyObject); string prompt = CreatePrompt(pdfContent, _serializer.Serialize(destinyObject)); - ClientResponse result = await iai.SendAsync(prompt, cancellationToken); + MapperClientResponse result = await iai.SendAsync(prompt, cancellationToken); return _serializer.Deserialize(result.Value); } diff --git a/src/MapperAI.Core/Serializers/Interfaces/IMapperSerializer.cs b/src/MapperAI.Core/Serializers/Interfaces/IMapperSerializer.cs index 1fbfd1e..656e526 100644 --- a/src/MapperAI.Core/Serializers/Interfaces/IMapperSerializer.cs +++ b/src/MapperAI.Core/Serializers/Interfaces/IMapperSerializer.cs @@ -1,4 +1,5 @@ -namespace MapperIA.Core.Serializers.Interfaces; + +namespace MapperAI.Core.Serializers.Interfaces; public interface IMapperSerializer { diff --git a/src/MapperAI.Core/Serializers/MapperSerializer.cs b/src/MapperAI.Core/Serializers/MapperSerializer.cs index dcb1406..503017f 100644 --- a/src/MapperAI.Core/Serializers/MapperSerializer.cs +++ b/src/MapperAI.Core/Serializers/MapperSerializer.cs @@ -1,6 +1,6 @@ using System.Text.Json; using System.Text.Json.Serialization; -using MapperIA.Core.Serializers.Interfaces; +using MapperAI.Core.Serializers.Interfaces; namespace MapperAI.Core.Serializers; diff --git a/test/MapperAI.Test/ClassMapperTests.cs b/test/MapperAI.Test/ClassMapperTests.cs index 87a3e43..a84e493 100644 --- a/test/MapperAI.Test/ClassMapperTests.cs +++ b/test/MapperAI.Test/ClassMapperTests.cs @@ -9,8 +9,8 @@ namespace MapperAI.Test; public class ClassMapperTests { - private readonly IClassMapper _classClassMapper = new ClassMapper(new MapperSerializer(), new ClientFactoryAI(new MapperSerializer())); - private readonly ClientConfiguration _clientConfiguration = new () + private readonly IClassMapper _classClassMapper = new ClassMapper(new MapperSerializer(), new MapperClientFactory(new MapperSerializer())); + private readonly MapperClientConfiguration _mapperClientConfiguration = new () { Type = ModelType.Gemini, Model = "gemini-2.0-flash", @@ -27,7 +27,7 @@ public async Task Test1() Name = "Diego" }; - var result = await _classClassMapper.MapAsync(person, _clientConfiguration); + var result = await _classClassMapper.MapAsync(person, _mapperClientConfiguration); Assert.Equal(person.Name, result?.Name); } } diff --git a/test/MapperAI.Test/FileMapperTests.cs b/test/MapperAI.Test/FileMapperTests.cs index 20d78fd..466af48 100644 --- a/test/MapperAI.Test/FileMapperTests.cs +++ b/test/MapperAI.Test/FileMapperTests.cs @@ -10,9 +10,9 @@ namespace MapperAI.Test; public class FileMapperTests { - private readonly FileMapper _mapper = new FileMapper(new ClientFactoryAI(new MapperSerializer()), new MapperSerializer()); + private readonly FileMapper _mapper = new (new MapperClientFactory(new MapperSerializer()), new MapperSerializer()); - private readonly ClientConfiguration _clientConfiguration = new() + private readonly MapperClientConfiguration _mapperClientConfiguration = new() { Type = ModelType.Gemini, Model = "gemini-2.0-flash", @@ -32,7 +32,7 @@ public async Task Test_Should_Create_4_Files_With_Go_Extension() NameSpace = "MapperAI.Test.MappedClasses", Extension = "go", }; - await _mapper.MapAsync(configuration, _clientConfiguration); + await _mapper.MapAsync(configuration, _mapperClientConfiguration); var files = Directory.GetFiles(OutputFolder); Assert.True(files.Length == 4); Assert.True(files.All(x => x.Contains(".go"))); @@ -47,12 +47,12 @@ public async Task Test_Should_Create_4_Files_With_CSharp_Extension() FileMapperConfiguration configuration = new FileMapperConfiguration(InputFolder, OutputFolder) { NameSpace = "MapperAI.Test.MappedClasses", - Extension = "c#" + Extension = "php" }; - await _mapper.MapAsync(configuration, _clientConfiguration); + await _mapper.MapAsync(configuration, _mapperClientConfiguration); var files = Directory.GetFiles(OutputFolder); Assert.True(files.Length == 4); - Assert.True(files.All(x => x.Contains(".cs"))); + Assert.True(files.All(x => x.Contains(".php"))); } @@ -68,7 +68,7 @@ public async Task Test_Should_Create_1_File_With_CSharp_Extension() FileName = "Carro.java", LanguageVersion = "13" }; - await _mapper.MapAsync(configuration, _clientConfiguration); + await _mapper.MapAsync(configuration, _mapperClientConfiguration); var files = Directory.GetFiles(OutputFolder); Assert.True(files.Length == 1); Assert.True(files.All(x => x.Contains(".cs"))); diff --git a/src/MapperAI.Core/Helpers/FoldersHelpers.cs b/test/MapperAI.Test/FoldersHelpers.cs similarity index 100% rename from src/MapperAI.Core/Helpers/FoldersHelpers.cs rename to test/MapperAI.Test/FoldersHelpers.cs diff --git a/test/MapperAI.Test/MappedClasses/Carro.js b/test/MapperAI.Test/MappedClasses/Carro.js deleted file mode 100644 index 3b97881..0000000 --- a/test/MapperAI.Test/MappedClasses/Carro.js +++ /dev/null @@ -1,36 +0,0 @@ -namespace MapperAI.Test.MappedClasses { - export class Carro { - private marca: string; - private modelo: string; - private ano: number; - - constructor(marca: string, modelo: string, ano: number) { - this.marca = marca; - this.modelo = modelo; - this.ano = ano; - } - - public getMarca(): string { - return this.marca; - } - - public getModelo(): string { - return this.modelo; - } - - public getAno(): number { - return this.ano; - } - - public exibirInformacoes(): void { - console.log(`Marca: ${this.marca}`); - console.log(`Modelo: ${this.modelo}`); - console.log(`Ano: ${this.ano}`); - } - - public isAntigo(): boolean { - const anoAtual = new Date().getFullYear(); - return (anoAtual - this.ano) > 20; - } - } -} diff --git a/test/MapperAI.Test/MappedClasses/ContaBancaria.js b/test/MapperAI.Test/MappedClasses/ContaBancaria.js deleted file mode 100644 index 455ee96..0000000 --- a/test/MapperAI.Test/MappedClasses/ContaBancaria.js +++ /dev/null @@ -1,35 +0,0 @@ -namespace MapperAI.Test.MappedClasses { - export class ContaBancaria { - private numero: string; - private titular: string; - private saldo: number; - - constructor(numero: string, titular: string, saldo: number = 0) { - this.numero = numero; - this.titular = titular; - this.saldo = saldo; - } - - public depositar(valor: number): string { - if (valor > 0) { - this.saldo += valor; - return `Depósito de R$${valor} realizado com sucesso. Novo saldo: R$${this.saldo}.`; - } else { - return "Valor de depósito inválido."; - } - } - - public sacar(valor: number): string { - if (valor > 0 && valor <= this.saldo) { - this.saldo -= valor; - return `Saque de R$${valor} realizado com sucesso. Saldo restante: R$${this.saldo}.`; - } else { - return "Saldo insuficiente ou valor de saque inválido."; - } - } - - public consultarSaldo(): string { - return `Saldo atual: R$${this.saldo}.`; - } - } -} diff --git a/test/MapperAI.Test/MappedClasses/Student.js b/test/MapperAI.Test/MappedClasses/Student.js deleted file mode 100644 index bf55956..0000000 --- a/test/MapperAI.Test/MappedClasses/Student.js +++ /dev/null @@ -1,29 +0,0 @@ -namespace MapperAI.Test.MappedClasses { - export class Student { - private name: string; - private age: number; - private student_id: string; - - constructor(name: string, age: number, student_id: string) { - this.name = name; - this.age = age; - this.student_id = student_id; - } - - public getName(): string { - return this.name; - } - - public getAge(): number { - return this.age; - } - - public getStudentId(): string { - return this.student_id; - } - - public isAdult(): boolean { - return this.age >= 18; - } - } -} diff --git a/test/MapperAI.Test/MappedClasses/Usuario.js b/test/MapperAI.Test/MappedClasses/Usuario.js deleted file mode 100644 index 0325337..0000000 --- a/test/MapperAI.Test/MappedClasses/Usuario.js +++ /dev/null @@ -1,41 +0,0 @@ -namespace MapperAI.Test.MappedClasses { - export class Usuario { - private username: string; - private email: string; - private password: string; - - constructor(user: string, mail: string, pass: string) { - this.username = user; - this.email = mail; - this.password = pass; - } - - public getUsername(): string { - return this.username; - } - - public setUsername(user: string): void { - this.username = user; - } - - public getEmail(): string { - return this.email; - } - - public setEmail(mail: string): void { - this.email = mail; - } - - public getPassword(): string { - return this.password; - } - - public setPassword(pass: string): void { - this.password = pass; - } - - public displayInfo(): void { - console.log(`Username: ${this.username}, Email: ${this.email}`); - } - } -} diff --git a/test/MapperAI.Test/PdfMapperTests.cs b/test/MapperAI.Test/PdfMapperTests.cs index f3fe388..77621fb 100644 --- a/test/MapperAI.Test/PdfMapperTests.cs +++ b/test/MapperAI.Test/PdfMapperTests.cs @@ -4,14 +4,13 @@ using MapperAI.Core.Mappers; using MapperAI.Core.Mappers.Interfaces; using MapperAI.Core.Serializers; -using MapperIA.Core.Serializers; namespace MapperAI.Test; public class PdfMapperTests { - private readonly IPDFMapper _pdfMapper = new PdfMapper(new MapperSerializer(), new ClientFactoryAI(new MapperSerializer())); - private readonly ClientConfiguration _clientConfiguration = new () + private readonly IPDFMapper _pdfMapper = new PdfMapper(new MapperSerializer(), new MapperClientFactory(new MapperSerializer())); + private readonly MapperClientConfiguration _mapperClientConfiguration = new () { Type = ModelType.Gemini, Model = "gemini-2.0-flash", @@ -22,7 +21,7 @@ public class PdfMapperTests public async Task Test1() { var pdfPath = Path.Combine(@"../../../Curriculo - Diego.pdf"); - CurriculumModel? curriculumModel = await _pdfMapper.MapAsync(pdfPath, _clientConfiguration); + CurriculumModel? curriculumModel = await _pdfMapper.MapAsync(pdfPath, _mapperClientConfiguration); Assert.Contains("Uninter", curriculumModel?.Faculdade); Assert.Equal("Análise e desenvolvimento de sistemas EAD", curriculumModel?.Curso); Assert.Equal(2, curriculumModel?.Projects.Count); From 18d3bca2332fdb7430d9713c33a02b633486677b Mon Sep 17 00:00:00 2001 From: 01Dri Date: Fri, 2 May 2025 23:56:45 -0300 Subject: [PATCH 2/4] Up --- ...miniMapperClient.cs => GeminiMapperMapperClient.cs} | 4 ++-- .../Clients/Interfaces/IClientFactoryAI.cs | 8 -------- .../Interfaces/{IClientAI.cs => IMapperClient.cs} | 2 +- .../Clients/Interfaces/IMapperClientFactory.cs | 8 ++++++++ ...erClientFactory.cs => MapperMapperClientFactory.cs} | 10 +++++----- ...lamaMapperClient.cs => OllamaMapperMapperClient.cs} | 4 ++-- src/MapperAI.Core/Mappers/ClassMapper.cs | 10 +++++----- src/MapperAI.Core/Mappers/FileMapper.cs | 10 +++++----- src/MapperAI.Core/Mappers/PdfMapper.cs | 8 ++++---- test/MapperAI.Test/ClassMapperTests.cs | 2 +- test/MapperAI.Test/FileMapperTests.cs | 2 +- test/MapperAI.Test/PdfMapperTests.cs | 2 +- 12 files changed, 35 insertions(+), 35 deletions(-) rename src/MapperAI.Core/Clients/{GeminiMapperClient.cs => GeminiMapperMapperClient.cs} (88%) delete mode 100644 src/MapperAI.Core/Clients/Interfaces/IClientFactoryAI.cs rename src/MapperAI.Core/Clients/Interfaces/{IClientAI.cs => IMapperClient.cs} (85%) create mode 100644 src/MapperAI.Core/Clients/Interfaces/IMapperClientFactory.cs rename src/MapperAI.Core/Clients/{MapperClientFactory.cs => MapperMapperClientFactory.cs} (54%) rename src/MapperAI.Core/Clients/{OllamaMapperClient.cs => OllamaMapperMapperClient.cs} (85%) diff --git a/src/MapperAI.Core/Clients/GeminiMapperClient.cs b/src/MapperAI.Core/Clients/GeminiMapperMapperClient.cs similarity index 88% rename from src/MapperAI.Core/Clients/GeminiMapperClient.cs rename to src/MapperAI.Core/Clients/GeminiMapperMapperClient.cs index 340affc..a3ff984 100644 --- a/src/MapperAI.Core/Clients/GeminiMapperClient.cs +++ b/src/MapperAI.Core/Clients/GeminiMapperMapperClient.cs @@ -6,11 +6,11 @@ namespace MapperAI.Core.Clients; -public class GeminiMapperClient : MapperClientBase, IClientAI +public class GeminiMapperMapperClient : MapperClientBase, IMapperClient { private const string EndpointBase = "https://generativelanguage.googleapis.com/v1beta"; - public GeminiMapperClient(MapperClientConfiguration mapperClientConfiguration, IMapperSerializer serializer) : base( + public GeminiMapperMapperClient(MapperClientConfiguration mapperClientConfiguration, IMapperSerializer serializer) : base( mapperClientConfiguration, serializer) { } diff --git a/src/MapperAI.Core/Clients/Interfaces/IClientFactoryAI.cs b/src/MapperAI.Core/Clients/Interfaces/IClientFactoryAI.cs deleted file mode 100644 index 0c07ceb..0000000 --- a/src/MapperAI.Core/Clients/Interfaces/IClientFactoryAI.cs +++ /dev/null @@ -1,8 +0,0 @@ -using MapperAI.Core.Clients.Models; - -namespace MapperAI.Core.Clients.Interfaces; - -public interface IClientFactoryAI -{ - IClientAI CreateClient(MapperClientConfiguration configuration); -} \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/Interfaces/IClientAI.cs b/src/MapperAI.Core/Clients/Interfaces/IMapperClient.cs similarity index 85% rename from src/MapperAI.Core/Clients/Interfaces/IClientAI.cs rename to src/MapperAI.Core/Clients/Interfaces/IMapperClient.cs index 333f024..0122a5d 100644 --- a/src/MapperAI.Core/Clients/Interfaces/IClientAI.cs +++ b/src/MapperAI.Core/Clients/Interfaces/IMapperClient.cs @@ -2,7 +2,7 @@ namespace MapperAI.Core.Clients.Interfaces; -public interface IClientAI +public interface IMapperClient { Task SendAsync(string prompt, CancellationToken cancellationToken); diff --git a/src/MapperAI.Core/Clients/Interfaces/IMapperClientFactory.cs b/src/MapperAI.Core/Clients/Interfaces/IMapperClientFactory.cs new file mode 100644 index 0000000..2ec39f3 --- /dev/null +++ b/src/MapperAI.Core/Clients/Interfaces/IMapperClientFactory.cs @@ -0,0 +1,8 @@ +using MapperAI.Core.Clients.Models; + +namespace MapperAI.Core.Clients.Interfaces; + +public interface IMapperClientFactory +{ + IMapperClient CreateClient(MapperClientConfiguration configuration); +} \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/MapperClientFactory.cs b/src/MapperAI.Core/Clients/MapperMapperClientFactory.cs similarity index 54% rename from src/MapperAI.Core/Clients/MapperClientFactory.cs rename to src/MapperAI.Core/Clients/MapperMapperClientFactory.cs index ea7af4e..65763b0 100644 --- a/src/MapperAI.Core/Clients/MapperClientFactory.cs +++ b/src/MapperAI.Core/Clients/MapperMapperClientFactory.cs @@ -5,24 +5,24 @@ namespace MapperAI.Core.Clients; -public class MapperClientFactory : IClientFactoryAI +public class MapperMapperClientFactory : IMapperClientFactory { private readonly IMapperSerializer _serializer; - public MapperClientFactory(IMapperSerializer serializer) + public MapperMapperClientFactory(IMapperSerializer serializer) { _serializer = serializer; } - public IClientAI CreateClient(MapperClientConfiguration configuration) + public IMapperClient CreateClient(MapperClientConfiguration configuration) { switch (configuration.Type) { case ModelType.Ollama: - return new OllamaMapperClient(configuration, _serializer); + return new OllamaMapperMapperClient(configuration, _serializer); default: - return new GeminiMapperClient(configuration, _serializer); + return new GeminiMapperMapperClient(configuration, _serializer); } } } \ No newline at end of file diff --git a/src/MapperAI.Core/Clients/OllamaMapperClient.cs b/src/MapperAI.Core/Clients/OllamaMapperMapperClient.cs similarity index 85% rename from src/MapperAI.Core/Clients/OllamaMapperClient.cs rename to src/MapperAI.Core/Clients/OllamaMapperMapperClient.cs index 37710ba..0067137 100644 --- a/src/MapperAI.Core/Clients/OllamaMapperClient.cs +++ b/src/MapperAI.Core/Clients/OllamaMapperMapperClient.cs @@ -7,11 +7,11 @@ namespace MapperAI.Core.Clients; -public class OllamaMapperClient : MapperClientBase ,IClientAI +public class OllamaMapperMapperClient : MapperClientBase ,IMapperClient { private const string Endpoint = "http://localhost:11434/api/generate"; - public OllamaMapperClient(MapperClientConfiguration mapperClientConfiguration, IMapperSerializer serializer) : base(mapperClientConfiguration, serializer) + public OllamaMapperMapperClient(MapperClientConfiguration mapperClientConfiguration, IMapperSerializer serializer) : base(mapperClientConfiguration, serializer) { } diff --git a/src/MapperAI.Core/Mappers/ClassMapper.cs b/src/MapperAI.Core/Mappers/ClassMapper.cs index 574aadd..a842c21 100644 --- a/src/MapperAI.Core/Mappers/ClassMapper.cs +++ b/src/MapperAI.Core/Mappers/ClassMapper.cs @@ -8,21 +8,21 @@ namespace MapperAI.Core.Mappers; public class ClassMapper : IClassMapper { private readonly IMapperSerializer _serializer; - private readonly IClientFactoryAI _clientFactoryAi; + private readonly IMapperClientFactory _mapperClientFactory; - public ClassMapper(IMapperSerializer serializer, IClientFactoryAI clientFactoryAi) + public ClassMapper(IMapperSerializer serializer, IMapperClientFactory mapperClientFactory) { _serializer = serializer; - _clientFactoryAi = clientFactoryAi; + _mapperClientFactory = mapperClientFactory; } public async Task MapAsync(T origin, MapperClientConfiguration configuration, CancellationToken cancellationToken = default) where T : class where TK : class, new() { - IClientAI client = _clientFactoryAi.CreateClient(configuration); + IMapperClient mapperClient = _mapperClientFactory.CreateClient(configuration); TK destinyObject = new TK(); string prompt = CreatePrompt(_serializer.Serialize(origin), destinyObject); - var result = await client.SendAsync(prompt, cancellationToken); + var result = await mapperClient.SendAsync(prompt, cancellationToken); return _serializer.Deserialize(result.Value); } diff --git a/src/MapperAI.Core/Mappers/FileMapper.cs b/src/MapperAI.Core/Mappers/FileMapper.cs index 7da2738..231f7bc 100644 --- a/src/MapperAI.Core/Mappers/FileMapper.cs +++ b/src/MapperAI.Core/Mappers/FileMapper.cs @@ -10,22 +10,22 @@ namespace MapperAI.Core.Mappers; public class FileMapper : IFileMapper { - private readonly IClientFactoryAI _clientFactoryAi; + private readonly IMapperClientFactory _mapperClientFactory; private readonly IMapperSerializer _serializer; - public FileMapper(IClientFactoryAI clientFactoryAi, IMapperSerializer serializer) + public FileMapper(IMapperClientFactory mapperClientFactory, IMapperSerializer serializer) { - _clientFactoryAi = clientFactoryAi; + _mapperClientFactory = mapperClientFactory; _serializer = serializer; } public async Task MapAsync(FileMapperConfiguration configuration, MapperClientConfiguration mapperClientConfiguration, CancellationToken cancellationToken = default) { - IClientAI client = _clientFactoryAi.CreateClient(mapperClientConfiguration); + IMapperClient mapperClient = _mapperClientFactory.CreateClient(mapperClientConfiguration); List filesToMap = GetFilesToMap(configuration); string prompt = CreatePrompt(filesToMap, configuration); - MapperClientResponse result = await client.SendAsync(prompt, cancellationToken); + MapperClientResponse result = await mapperClient.SendAsync(prompt, cancellationToken); List? contents = _serializer.Deserialize>(result.Value); if (contents != null && contents.Any()) { diff --git a/src/MapperAI.Core/Mappers/PdfMapper.cs b/src/MapperAI.Core/Mappers/PdfMapper.cs index 1db3ef4..1bc05a6 100644 --- a/src/MapperAI.Core/Mappers/PdfMapper.cs +++ b/src/MapperAI.Core/Mappers/PdfMapper.cs @@ -11,18 +11,18 @@ namespace MapperAI.Core.Mappers; public class PdfMapper : IPDFMapper { private readonly IMapperSerializer _serializer; - private readonly IClientFactoryAI _clientFactoryAi; + private readonly IMapperClientFactory _mapperClientFactory; - public PdfMapper(IMapperSerializer serializer, IClientFactoryAI clientFactoryAi) + public PdfMapper(IMapperSerializer serializer, IMapperClientFactory mapperClientFactory) { _serializer = serializer; - _clientFactoryAi = clientFactoryAi; + _mapperClientFactory = mapperClientFactory; } public async Task MapAsync(string pdfPath, MapperClientConfiguration configuration, CancellationToken cancellationToken = default) where T : class, new() { - IClientAI iai = _clientFactoryAi.CreateClient(configuration); + IMapperClient iai = _mapperClientFactory.CreateClient(configuration); string pdfContent = ExtractPdfContent(pdfPath); T destinyObject = new T(); DependencyInitializer.Initialize(destinyObject); diff --git a/test/MapperAI.Test/ClassMapperTests.cs b/test/MapperAI.Test/ClassMapperTests.cs index a84e493..0296d45 100644 --- a/test/MapperAI.Test/ClassMapperTests.cs +++ b/test/MapperAI.Test/ClassMapperTests.cs @@ -9,7 +9,7 @@ namespace MapperAI.Test; public class ClassMapperTests { - private readonly IClassMapper _classClassMapper = new ClassMapper(new MapperSerializer(), new MapperClientFactory(new MapperSerializer())); + private readonly IClassMapper _classClassMapper = new ClassMapper(new MapperSerializer(), new MapperMapperClientFactory(new MapperSerializer())); private readonly MapperClientConfiguration _mapperClientConfiguration = new () { Type = ModelType.Gemini, diff --git a/test/MapperAI.Test/FileMapperTests.cs b/test/MapperAI.Test/FileMapperTests.cs index 466af48..5fe9dc7 100644 --- a/test/MapperAI.Test/FileMapperTests.cs +++ b/test/MapperAI.Test/FileMapperTests.cs @@ -10,7 +10,7 @@ namespace MapperAI.Test; public class FileMapperTests { - private readonly FileMapper _mapper = new (new MapperClientFactory(new MapperSerializer()), new MapperSerializer()); + private readonly FileMapper _mapper = new (new MapperMapperClientFactory(new MapperSerializer()), new MapperSerializer()); private readonly MapperClientConfiguration _mapperClientConfiguration = new() { diff --git a/test/MapperAI.Test/PdfMapperTests.cs b/test/MapperAI.Test/PdfMapperTests.cs index 77621fb..504ee6b 100644 --- a/test/MapperAI.Test/PdfMapperTests.cs +++ b/test/MapperAI.Test/PdfMapperTests.cs @@ -9,7 +9,7 @@ namespace MapperAI.Test; public class PdfMapperTests { - private readonly IPDFMapper _pdfMapper = new PdfMapper(new MapperSerializer(), new MapperClientFactory(new MapperSerializer())); + private readonly IPDFMapper _pdfMapper = new PdfMapper(new MapperSerializer(), new MapperMapperClientFactory(new MapperSerializer())); private readonly MapperClientConfiguration _mapperClientConfiguration = new () { Type = ModelType.Gemini, From cd8dfcceec9833f63f51d5eca349223e91984569 Mon Sep 17 00:00:00 2001 From: 01Dri Date: Sat, 3 May 2025 00:48:31 -0300 Subject: [PATCH 3/4] Refactor --- .../Models/MapperClientConfiguration.cs | 19 ++++++++-- src/MapperAI.Core/Mappers/ClassMapper.cs | 12 +++++-- src/MapperAI.Core/Mappers/FileMapper.cs | 9 ++--- .../Mappers/Interfaces/IClassMapper.cs | 11 +++--- .../Mappers/Interfaces/IFileMapper.cs | 4 +-- .../Mappers/Interfaces/IPDFMapper.cs | 6 ++-- src/MapperAI.Core/Mappers/PdfMapper.cs | 8 +++-- test/MapperAI.Test/BaseTests.cs | 18 ++++++++++ test/MapperAI.Test/ClassMapperTests.cs | 23 ++++++------ test/MapperAI.Test/FileMapperTests.cs | 36 ++++++++++--------- .../{ => Helpers}/FoldersHelpers.cs | 3 +- test/MapperAI.Test/PdfMapperTests.cs | 21 ++++++----- 12 files changed, 103 insertions(+), 67 deletions(-) create mode 100644 test/MapperAI.Test/BaseTests.cs rename test/MapperAI.Test/{ => Helpers}/FoldersHelpers.cs (87%) diff --git a/src/MapperAI.Core/Clients/Models/MapperClientConfiguration.cs b/src/MapperAI.Core/Clients/Models/MapperClientConfiguration.cs index bcdefc9..a81bab9 100644 --- a/src/MapperAI.Core/Clients/Models/MapperClientConfiguration.cs +++ b/src/MapperAI.Core/Clients/Models/MapperClientConfiguration.cs @@ -1,10 +1,23 @@ -using MapperAI.Core.Enums; +using MapperAI.Core.Enums; namespace MapperAI.Core.Clients.Models; public class MapperClientConfiguration { - public required string Model { get; set; } + public string Model { get; set; } public string? ApiKey { get; set; } - public required ModelType Type { get; set; } + public ModelType Type { get; set; } + + public MapperClientConfiguration(string model, string? apiKey, ModelType type) + { + Model = model; + ApiKey = apiKey; + Type = type; + } + + public MapperClientConfiguration(string model, ModelType type) + { + Model = model; + Type = type; + } } \ No newline at end of file diff --git a/src/MapperAI.Core/Mappers/ClassMapper.cs b/src/MapperAI.Core/Mappers/ClassMapper.cs index a842c21..c1b13d0 100644 --- a/src/MapperAI.Core/Mappers/ClassMapper.cs +++ b/src/MapperAI.Core/Mappers/ClassMapper.cs @@ -1,5 +1,6 @@ using MapperAI.Core.Clients.Interfaces; using MapperAI.Core.Clients.Models; +using MapperAI.Core.Exceptions; using MapperAI.Core.Mappers.Interfaces; using MapperAI.Core.Serializers.Interfaces; @@ -9,23 +10,28 @@ public class ClassMapper : IClassMapper { private readonly IMapperSerializer _serializer; private readonly IMapperClientFactory _mapperClientFactory; + private readonly MapperClientConfiguration _clientConfiguration; + - public ClassMapper(IMapperSerializer serializer, IMapperClientFactory mapperClientFactory) + public ClassMapper(IMapperSerializer serializer, IMapperClientFactory mapperClientFactory, MapperClientConfiguration clientConfiguration) { _serializer = serializer; _mapperClientFactory = mapperClientFactory; + _clientConfiguration = clientConfiguration; } - public async Task MapAsync(T origin, MapperClientConfiguration configuration, CancellationToken cancellationToken = default) where T : class where TK : class, new() + public async Task MapAsync(T origin, CancellationToken cancellationToken = default) where T : class where TK : class, new() { - IMapperClient mapperClient = _mapperClientFactory.CreateClient(configuration); + if (_clientConfiguration == null) throw new MapperException("Client configuration is null."); + IMapperClient mapperClient = _mapperClientFactory.CreateClient(_clientConfiguration); TK destinyObject = new TK(); string prompt = CreatePrompt(_serializer.Serialize(origin), destinyObject); var result = await mapperClient.SendAsync(prompt, cancellationToken); return _serializer.Deserialize(result.Value); } + private string CreatePrompt(string originSerialized, TK destinyObject) where TK : class, new() { string destinyJson = _serializer.Serialize(destinyObject); diff --git a/src/MapperAI.Core/Mappers/FileMapper.cs b/src/MapperAI.Core/Mappers/FileMapper.cs index 231f7bc..91fd7f4 100644 --- a/src/MapperAI.Core/Mappers/FileMapper.cs +++ b/src/MapperAI.Core/Mappers/FileMapper.cs @@ -12,17 +12,18 @@ public class FileMapper : IFileMapper private readonly IMapperClientFactory _mapperClientFactory; private readonly IMapperSerializer _serializer; + private readonly MapperClientConfiguration _clientConfiguration; - public FileMapper(IMapperClientFactory mapperClientFactory, IMapperSerializer serializer) + public FileMapper(IMapperClientFactory mapperClientFactory, IMapperSerializer serializer, MapperClientConfiguration clientConfiguration) { _mapperClientFactory = mapperClientFactory; _serializer = serializer; + _clientConfiguration = clientConfiguration; } - public async Task MapAsync(FileMapperConfiguration configuration, MapperClientConfiguration mapperClientConfiguration, - CancellationToken cancellationToken = default) + public async Task MapAsync(FileMapperConfiguration configuration, CancellationToken cancellationToken = default) { - IMapperClient mapperClient = _mapperClientFactory.CreateClient(mapperClientConfiguration); + IMapperClient mapperClient = _mapperClientFactory.CreateClient(_clientConfiguration); List filesToMap = GetFilesToMap(configuration); string prompt = CreatePrompt(filesToMap, configuration); MapperClientResponse result = await mapperClient.SendAsync(prompt, cancellationToken); diff --git a/src/MapperAI.Core/Mappers/Interfaces/IClassMapper.cs b/src/MapperAI.Core/Mappers/Interfaces/IClassMapper.cs index aaa7353..80551aa 100644 --- a/src/MapperAI.Core/Mappers/Interfaces/IClassMapper.cs +++ b/src/MapperAI.Core/Mappers/Interfaces/IClassMapper.cs @@ -1,11 +1,10 @@ -using MapperAI.Core.Clients.Models; +namespace MapperAI.Core.Mappers.Interfaces; -namespace MapperAI.Core.Mappers.Interfaces; - -public interface IClassMapper +public interface IClassMapper { - Task MapAsync(T origin, MapperClientConfiguration configuration, CancellationToken cancellationToken = default) + Task MapAsync(T origin, CancellationToken cancellationToken = default) where T : class where TK : class, new(); - + + } \ No newline at end of file diff --git a/src/MapperAI.Core/Mappers/Interfaces/IFileMapper.cs b/src/MapperAI.Core/Mappers/Interfaces/IFileMapper.cs index d043ff7..c639818 100644 --- a/src/MapperAI.Core/Mappers/Interfaces/IFileMapper.cs +++ b/src/MapperAI.Core/Mappers/Interfaces/IFileMapper.cs @@ -1,5 +1,4 @@ -using MapperAI.Core.Clients.Models; -using MapperAI.Core.Mappers.Models; +using MapperAI.Core.Mappers.Models; namespace MapperAI.Core.Mappers.Interfaces; @@ -8,7 +7,6 @@ public interface IFileMapper Task MapAsync ( FileMapperConfiguration configuration, - MapperClientConfiguration mapperClientConfiguration, CancellationToken cancellationToken = default ); } \ No newline at end of file diff --git a/src/MapperAI.Core/Mappers/Interfaces/IPDFMapper.cs b/src/MapperAI.Core/Mappers/Interfaces/IPDFMapper.cs index eb8ffcb..d0d5638 100644 --- a/src/MapperAI.Core/Mappers/Interfaces/IPDFMapper.cs +++ b/src/MapperAI.Core/Mappers/Interfaces/IPDFMapper.cs @@ -1,10 +1,8 @@ -using MapperAI.Core.Clients.Models; - -namespace MapperAI.Core.Mappers.Interfaces; +namespace MapperAI.Core.Mappers.Interfaces; public interface IPDFMapper { - Task MapAsync(string pdfPath, MapperClientConfiguration configuration, CancellationToken cancellationToken = default) + Task MapAsync(string pdfPath, CancellationToken cancellationToken = default) where T : class, new(); } \ No newline at end of file diff --git a/src/MapperAI.Core/Mappers/PdfMapper.cs b/src/MapperAI.Core/Mappers/PdfMapper.cs index 1bc05a6..c961a8f 100644 --- a/src/MapperAI.Core/Mappers/PdfMapper.cs +++ b/src/MapperAI.Core/Mappers/PdfMapper.cs @@ -12,17 +12,19 @@ public class PdfMapper : IPDFMapper { private readonly IMapperSerializer _serializer; private readonly IMapperClientFactory _mapperClientFactory; + private readonly MapperClientConfiguration _clientConfiguration; - public PdfMapper(IMapperSerializer serializer, IMapperClientFactory mapperClientFactory) + public PdfMapper(IMapperSerializer serializer, IMapperClientFactory mapperClientFactory, MapperClientConfiguration clientConfiguration) { _serializer = serializer; _mapperClientFactory = mapperClientFactory; + _clientConfiguration = clientConfiguration; } - public async Task MapAsync(string pdfPath, MapperClientConfiguration configuration, CancellationToken cancellationToken = default) where T : class, new() + public async Task MapAsync(string pdfPath, CancellationToken cancellationToken = default) where T : class, new() { - IMapperClient iai = _mapperClientFactory.CreateClient(configuration); + IMapperClient iai = _mapperClientFactory.CreateClient(_clientConfiguration); string pdfContent = ExtractPdfContent(pdfPath); T destinyObject = new T(); DependencyInitializer.Initialize(destinyObject); diff --git a/test/MapperAI.Test/BaseTests.cs b/test/MapperAI.Test/BaseTests.cs new file mode 100644 index 0000000..24d117f --- /dev/null +++ b/test/MapperAI.Test/BaseTests.cs @@ -0,0 +1,18 @@ +using MapperAI.Core.Clients; +using MapperAI.Core.Clients.Interfaces; +using MapperAI.Core.Serializers; +using MapperAI.Core.Serializers.Interfaces; + +namespace MapperAI.Test; + +public abstract class BaseTests +{ + public readonly IMapperSerializer Serializer; + public readonly IMapperClientFactory Factory; + + public BaseTests() + { + Serializer = new MapperSerializer(); + Factory = new MapperMapperClientFactory(Serializer); + } +} \ No newline at end of file diff --git a/test/MapperAI.Test/ClassMapperTests.cs b/test/MapperAI.Test/ClassMapperTests.cs index 0296d45..7d70ccb 100644 --- a/test/MapperAI.Test/ClassMapperTests.cs +++ b/test/MapperAI.Test/ClassMapperTests.cs @@ -1,23 +1,22 @@ -using MapperAI.Core.Clients; using MapperAI.Core.Clients.Models; using MapperAI.Core.Enums; using MapperAI.Core.Mappers; using MapperAI.Core.Mappers.Interfaces; -using MapperAI.Core.Serializers; namespace MapperAI.Test; -public class ClassMapperTests +public class ClassMapperTests : BaseTests { - private readonly IClassMapper _classClassMapper = new ClassMapper(new MapperSerializer(), new MapperMapperClientFactory(new MapperSerializer())); - private readonly MapperClientConfiguration _mapperClientConfiguration = new () + private readonly IClassMapper _classMapper; + private readonly MapperClientConfiguration _clientConfiguration; + + public ClassMapperTests() { - Type = ModelType.Gemini, - Model = "gemini-2.0-flash", - ApiKey = Environment.GetEnvironmentVariable("GEMINI_KEY") - }; - - + _clientConfiguration = new MapperClientConfiguration("gemini-2.0-flash", Environment.GetEnvironmentVariable("GEMINI_KEY"),ModelType.Gemini); + _classMapper = new ClassMapper(Serializer, Factory, _clientConfiguration); + } + + [Fact] public async Task Test1() { @@ -27,7 +26,7 @@ public async Task Test1() Name = "Diego" }; - var result = await _classClassMapper.MapAsync(person, _mapperClientConfiguration); + var result = await _classMapper.MapAsync(person); Assert.Equal(person.Name, result?.Name); } } diff --git a/test/MapperAI.Test/FileMapperTests.cs b/test/MapperAI.Test/FileMapperTests.cs index 5fe9dc7..bc79dd4 100644 --- a/test/MapperAI.Test/FileMapperTests.cs +++ b/test/MapperAI.Test/FileMapperTests.cs @@ -1,27 +1,28 @@ -using MapperAI.Core.Clients; -using MapperAI.Core.Clients.Models; +using MapperAI.Core.Clients.Models; using MapperAI.Core.Enums; -using MapperAI.Core.Helpers; using MapperAI.Core.Mappers; +using MapperAI.Core.Mappers.Interfaces; using MapperAI.Core.Mappers.Models; -using MapperAI.Core.Serializers; +using MapperAI.Test.Helpers; namespace MapperAI.Test; -public class FileMapperTests +public class FileMapperTests : BaseTests { - private readonly FileMapper _mapper = new (new MapperMapperClientFactory(new MapperSerializer()), new MapperSerializer()); - private readonly MapperClientConfiguration _mapperClientConfiguration = new() - { - Type = ModelType.Gemini, - Model = "gemini-2.0-flash", - ApiKey = Environment.GetEnvironmentVariable("GEMINI_KEY") - }; + private readonly MapperClientConfiguration _clientConfiguration; + private readonly IFileMapper _mapper; private string InputFolder => Path.Combine(FoldersHelpers.GetProjectDefaultPath(), "Class"); private string OutputFolder => Path.Combine(FoldersHelpers.GetProjectDefaultPath(), "MappedClasses"); + public FileMapperTests() + { + _clientConfiguration = new MapperClientConfiguration("gemini-2.0-flash", Environment.GetEnvironmentVariable("GEMINI_KEY"),ModelType.Gemini); + _mapper = new FileMapper(Factory, Serializer, _clientConfiguration); + + } + [Fact] public async Task Test_Should_Create_4_Files_With_Go_Extension() @@ -32,7 +33,7 @@ public async Task Test_Should_Create_4_Files_With_Go_Extension() NameSpace = "MapperAI.Test.MappedClasses", Extension = "go", }; - await _mapper.MapAsync(configuration, _mapperClientConfiguration); + await _mapper.MapAsync(configuration); var files = Directory.GetFiles(OutputFolder); Assert.True(files.Length == 4); Assert.True(files.All(x => x.Contains(".go"))); @@ -44,15 +45,16 @@ public async Task Test_Should_Create_4_Files_With_Go_Extension() public async Task Test_Should_Create_4_Files_With_CSharp_Extension() { + string extesionToMap = "js"; FileMapperConfiguration configuration = new FileMapperConfiguration(InputFolder, OutputFolder) { NameSpace = "MapperAI.Test.MappedClasses", - Extension = "php" + Extension = extesionToMap }; - await _mapper.MapAsync(configuration, _mapperClientConfiguration); + await _mapper.MapAsync(configuration); var files = Directory.GetFiles(OutputFolder); Assert.True(files.Length == 4); - Assert.True(files.All(x => x.Contains(".php"))); + Assert.True(files.All(x => x.Contains($".{extesionToMap}"))); } @@ -68,7 +70,7 @@ public async Task Test_Should_Create_1_File_With_CSharp_Extension() FileName = "Carro.java", LanguageVersion = "13" }; - await _mapper.MapAsync(configuration, _mapperClientConfiguration); + await _mapper.MapAsync(configuration); var files = Directory.GetFiles(OutputFolder); Assert.True(files.Length == 1); Assert.True(files.All(x => x.Contains(".cs"))); diff --git a/test/MapperAI.Test/FoldersHelpers.cs b/test/MapperAI.Test/Helpers/FoldersHelpers.cs similarity index 87% rename from test/MapperAI.Test/FoldersHelpers.cs rename to test/MapperAI.Test/Helpers/FoldersHelpers.cs index 832fdd2..2744ac7 100644 --- a/test/MapperAI.Test/FoldersHelpers.cs +++ b/test/MapperAI.Test/Helpers/FoldersHelpers.cs @@ -1,4 +1,5 @@ -namespace MapperAI.Core.Helpers; + +namespace MapperAI.Test.Helpers; public class FoldersHelpers { diff --git a/test/MapperAI.Test/PdfMapperTests.cs b/test/MapperAI.Test/PdfMapperTests.cs index 504ee6b..cad5e75 100644 --- a/test/MapperAI.Test/PdfMapperTests.cs +++ b/test/MapperAI.Test/PdfMapperTests.cs @@ -1,27 +1,26 @@ -using MapperAI.Core.Clients; -using MapperAI.Core.Clients.Models; +using MapperAI.Core.Clients.Models; using MapperAI.Core.Enums; using MapperAI.Core.Mappers; using MapperAI.Core.Mappers.Interfaces; -using MapperAI.Core.Serializers; namespace MapperAI.Test; -public class PdfMapperTests +public class PdfMapperTests : BaseTests { - private readonly IPDFMapper _pdfMapper = new PdfMapper(new MapperSerializer(), new MapperMapperClientFactory(new MapperSerializer())); - private readonly MapperClientConfiguration _mapperClientConfiguration = new () + private readonly IPDFMapper _pdfMapper; + private readonly MapperClientConfiguration _clientConfiguration; + + public PdfMapperTests() { - Type = ModelType.Gemini, - Model = "gemini-2.0-flash", - ApiKey = Environment.GetEnvironmentVariable("GEMINI_KEY") - }; + _clientConfiguration = new MapperClientConfiguration("gemini-2.0-flash", Environment.GetEnvironmentVariable("GEMINI_KEY"),ModelType.Gemini); + _pdfMapper = new PdfMapper(Serializer, Factory, _clientConfiguration); + } [Fact] public async Task Test1() { var pdfPath = Path.Combine(@"../../../Curriculo - Diego.pdf"); - CurriculumModel? curriculumModel = await _pdfMapper.MapAsync(pdfPath, _mapperClientConfiguration); + CurriculumModel? curriculumModel = await _pdfMapper.MapAsync(pdfPath); Assert.Contains("Uninter", curriculumModel?.Faculdade); Assert.Equal("Análise e desenvolvimento de sistemas EAD", curriculumModel?.Curso); Assert.Equal(2, curriculumModel?.Projects.Count); From 9415b48485d129ef6c211e0d68fbfb0cfca3ca34 Mon Sep 17 00:00:00 2001 From: 01Dri Date: Sat, 3 May 2025 00:49:51 -0300 Subject: [PATCH 4/4] Tag --- src/MapperAI.Core/MapperAI.Core.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MapperAI.Core/MapperAI.Core.csproj b/src/MapperAI.Core/MapperAI.Core.csproj index e7cd976..804a4b8 100644 --- a/src/MapperAI.Core/MapperAI.Core.csproj +++ b/src/MapperAI.Core/MapperAI.Core.csproj @@ -7,7 +7,7 @@ MapperAI-Alpha Dridev true - 0.0.4-alpha + 0.1.0-alpha MIT https://github.com/01Dri/MapperAI MapperAI.Core