From 64046d8e643d53b7010f3ce39ad8deaae4e540f0 Mon Sep 17 00:00:00 2001 From: Luca <106596790+RealLHI@users.noreply.github.com> Date: Mon, 9 Sep 2024 11:10:12 +0200 Subject: [PATCH] hotfix/csv-format (#88) * feat(csv): remove formatcharackter option, refactor(csv): format * fix(csv): refactoring mistake --- .../src/FormatOptions.cs | 17 ------- .../src/CSV/CsvFormat.cs | 50 ++++++++++++------- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/Ashampoo.Translation.Systems.Formats.Abstractions/src/FormatOptions.cs b/src/Ashampoo.Translation.Systems.Formats.Abstractions/src/FormatOptions.cs index fe0af93..2c565e3 100644 --- a/src/Ashampoo.Translation.Systems.Formats.Abstractions/src/FormatOptions.cs +++ b/src/Ashampoo.Translation.Systems.Formats.Abstractions/src/FormatOptions.cs @@ -28,23 +28,6 @@ public record FormatStringOption(string Name, bool Required = false) : FormatOpt public string Value { get; set; } = ""; } -/// -/// Format option for a char value. -/// -/// -/// -/// -/// -/// -/// -public record FormatCharacterOption(string Name, bool Required = false) : FormatOption(Name, Required) -{ - /// - /// The value of the option. - /// - public string Value { get; set; } = ";"; -} - /// /// Configuration object for a containing a list of FormatOptions. /// diff --git a/src/Ashampoo.Translation.Systems.Formats/src/CSV/CsvFormat.cs b/src/Ashampoo.Translation.Systems.Formats/src/CSV/CsvFormat.cs index 30900c4..01b4a45 100644 --- a/src/Ashampoo.Translation.Systems.Formats/src/CSV/CsvFormat.cs +++ b/src/Ashampoo.Translation.Systems.Formats/src/CSV/CsvFormat.cs @@ -81,32 +81,44 @@ private async Task GetHeaderInformation(StreamReader reader, FormatReadOptions o switch (headerLine[0].Trim()) { case "#Delimiter": - if (char.IsWhiteSpace(Delimiter) && !string.IsNullOrWhiteSpace(headerLine[1])) - { - CsvFormatHeader.Delimiter = headerLine[1].Trim().ToCharArray().First(); - } - + TryApplyDelimiter(headerLine); break; case "#Source Language": - if (string.IsNullOrWhiteSpace(options.SourceLanguage?.Value) && - !string.IsNullOrWhiteSpace(headerLine[1])) - { - Header.SourceLanguage = new Language(headerLine[1].Trim()); - } - + TryApplySourceLanguage(options, headerLine); break; case "#Target Language": - if (string.IsNullOrWhiteSpace(options.TargetLanguage.Value) && - !string.IsNullOrWhiteSpace(headerLine[1])) - { - Header.TargetLanguage = new Language(headerLine[1].Trim()); - } - + TryApplyTargetLanguage(options, headerLine); break; } } } + private void TryApplyTargetLanguage(FormatReadOptions options, string[] headerLine) + { + if (string.IsNullOrWhiteSpace(options.TargetLanguage.Value) && + !string.IsNullOrWhiteSpace(headerLine[1])) + { + Header.TargetLanguage = new Language(headerLine[1].Trim()); + } + } + + private void TryApplySourceLanguage(FormatReadOptions options, string[] headerLine) + { + if (string.IsNullOrWhiteSpace(options.SourceLanguage?.Value) && + !string.IsNullOrWhiteSpace(headerLine[1])) + { + Header.SourceLanguage = new Language(headerLine[1].Trim()); + } + } + + private void TryApplyDelimiter(string[] headerLine) + { + if (char.IsWhiteSpace(Delimiter) && !string.IsNullOrWhiteSpace(headerLine[1])) + { + CsvFormatHeader.Delimiter = headerLine[1].Trim()[0]; + } + } + private async Task ReadCsv(CsvReader reader) { await reader.ReadAsync(); @@ -200,7 +212,7 @@ private async Task ConfigureOptionsAsync(FormatReadOptions options) FormatStringOption targetLanguageOption = new("Target language", true); FormatStringOption sourceLanguageOption = new("Source language", true); - FormatCharacterOption delimiterOption = new("Delimiter", true); + FormatStringOption delimiterOption = new("Delimiter", true); List optionList = []; if (setTargetLanguage) optionList.Add(targetLanguageOption); @@ -237,7 +249,7 @@ private async Task ConfigureOptionsAsync(FormatReadOptions options) } } -file record CsvRecordFormat +file sealed record CsvRecordFormat { [Name("id")] public string Id { get; init; } = string.Empty;