Pārlūkot izejas kodu

- extends ParametrizedString to set parametr value.

Dalibor Votruba 1 gadu atpakaļ
vecāks
revīzija
6a349eb9d1

+ 42 - 42
qdr.fnd.core/Data/CsvHelper.cs

@@ -1,23 +1,23 @@
-using System;
-using System.Collections.Generic;
-using System.Data;
-using System.IO;
-using System.IO.Abstractions;
-using System.Linq;
-using System.Text;
-
-namespace Quadarax.Foundation.Core.Data
-{
-    public static class CsvHelper
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.IO;
+using System.IO.Abstractions;
+using System.Linq;
+using System.Text;
+
+namespace Quadarax.Foundation.Core.Data
+{
+    public static class CsvHelper
     {
-        /// <summary>
-        /// Read CSV file and creates DataTable with filled content, handling quoted values.
-        /// </summary>
-        /// <param name="fs">Used filesystem.</param>
-        /// <param name="csvFile">Full path to the CSV file.</param>
-        /// <param name="delimiter">The delimiter used in the CSV file.</param>
-        /// <param name="hasHeaderRow">Indicates whether the CSV file has a header row.</param>
-        /// <returns>New created DataTable with content.</returns>
+        /// <summary>
+        /// Read CSV file and creates DataTable with filled content, handling quoted values.
+        /// </summary>
+        /// <param name="fs">Used filesystem.</param>
+        /// <param name="csvFile">Full path to the CSV file.</param>
+        /// <param name="delimiter">The delimiter used in the CSV file.</param>
+        /// <param name="hasHeaderRow">Indicates whether the CSV file has a header row.</param>
+        /// <returns>New created DataTable with content.</returns>
         public static DataTable CsvToDataTable(IFileSystem fs, string csvFile, string delimiter = ",", bool hasHeaderRow = true)
         {
 
@@ -63,12 +63,12 @@ namespace Quadarax.Foundation.Core.Data
             return dt;
         }
 
-        /// <summary>
-        /// Parses a single CSV line, handling quoted values and delimiters within quotes.
-        /// </summary>
-        /// <param name="line">The CSV line to parse.</param>
-        /// <param name="delimiter">The delimiter used in the CSV line.</param>
-        /// <returns>An array of field values.</returns>
+        /// <summary>
+        /// Parses a single CSV line, handling quoted values and delimiters within quotes.
+        /// </summary>
+        /// <param name="line">The CSV line to parse.</param>
+        /// <param name="delimiter">The delimiter used in the CSV line.</param>
+        /// <returns>An array of field values.</returns>
         private static string[]? ParseCsvLine(string? line, string delimiter)
         {
             if (string.IsNullOrEmpty(line))
@@ -121,13 +121,13 @@ namespace Quadarax.Foundation.Core.Data
             return result.ToArray();
         }
 
-        /// <summary>
-        /// Writes a DataTable to a CSV file, properly handling quoting.
-        /// </summary>
-        /// <param name="fs">Used filesystem.</param>
-        /// <param name="dataTable">The DataTable to write.</param>
-        /// <param name="filePath">The full path of the output CSV file.</param>
-        /// <param name="delimiter">The delimiter to use in the CSV file.</param>
+        /// <summary>
+        /// Writes a DataTable to a CSV file, properly handling quoting.
+        /// </summary>
+        /// <param name="fs">Used filesystem.</param>
+        /// <param name="dataTable">The DataTable to write.</param>
+        /// <param name="filePath">The full path of the output CSV file.</param>
+        /// <param name="delimiter">The delimiter to use in the CSV file.</param>
         public static void DataTableToCsv(IFileSystem fs,DataTable dataTable, string filePath, string delimiter = ",")
         {
             using (var sw = fs.File.CreateText(filePath))
@@ -144,13 +144,13 @@ namespace Quadarax.Foundation.Core.Data
             }
         }
 
-        /// <summary>
-        /// Reads a CSV file and returns it as a list of dictionaries.
-        /// </summary>
-        /// <param name="fs">Used filesystem.</param>
-        /// <param name="csvFile">Full path to the CSV file.</param>
-        /// <param name="delimiter">The delimiter used in the CSV file.</param>
-        /// <returns>List of dictionaries representing the CSV data.</returns>
+        /// <summary>
+        /// Reads a CSV file and returns it as a list of dictionaries.
+        /// </summary>
+        /// <param name="fs">Used filesystem.</param>
+        /// <param name="csvFile">Full path to the CSV file.</param>
+        /// <param name="delimiter">The delimiter used in the CSV file.</param>
+        /// <returns>List of dictionaries representing the CSV data.</returns>
         public static List<Dictionary<string, string>> CsvToDictionaryList(IFileSystem fs, string csvFile, string delimiter = ",")
         {
             if (!fs.File.Exists(csvFile)) throw new FileNotFoundException($"File '{csvFile}' not found!", csvFile);
@@ -189,6 +189,6 @@ namespace Quadarax.Foundation.Core.Data
                 return $"\"{value.Replace("\"", "\"\"")}\"";
             }
             return value;
-        }
-    }
-}
+        }
+    }
+}

+ 51 - 0
qdr.fnd.core/Value/ParametrizedString.cs

@@ -1,8 +1,14 @@
 using System.Collections.Generic;
+using System.Linq;
 using System.Text;
 
 namespace Quadarax.Foundation.Core.Value
 {
+    /// <summary>
+    /// Represents a string with parameters (bracked by defined tags) that can be replaced.
+    /// Sample: Hello {name}, how are you?
+    /// If name = John, the result will be: Hello John, how are you?
+    /// </summary>
     public class ParameterizedString
     {
         private readonly string _parameterPrefix;
@@ -10,6 +16,14 @@ namespace Quadarax.Foundation.Core.Value
         private readonly string _initialString;
         private readonly IDictionary<string, string> _parameters;
 
+        public string[] Parameters => _parameters.Keys.ToArray();
+
+        /// <summary>
+        /// Creates a new instance of the ParameterizedString class.
+        /// </summary>
+        /// <param name="initialString">Initial string with parameters thats bracked by <paramref name="parameterPrefix"/> and <paramref name="parameterPostfix"/> </param>
+        /// <param name="parameterPrefix">Left parameter name bracket</param>
+        /// <param name="parameterPostfix">Right parameter name bracket</param>
         public ParameterizedString(string initialString,string parameterPrefix = "", string parameterPostfix = "")
         {
             _parameterPostfix = parameterPostfix;
@@ -19,6 +33,12 @@ namespace Quadarax.Foundation.Core.Value
             _parameters = new Dictionary<string, string>();
         }
 
+        /// <summary>
+        /// Add new parameter with value.
+        /// </summary>
+        /// <param name="name">Parameter name.</param>
+        /// <param name="value">Parameter value</param>
+        /// <returns>If parameter already exists, returns false</returns>
         public bool AddParameter(string name, string value)
         {
             if(_parameters.ContainsKey(name))
@@ -29,6 +49,37 @@ namespace Quadarax.Foundation.Core.Value
             return true;
         }
 
+        /// <summary>
+        /// Set existing parameter value.
+        /// </summary>
+        /// <param name="name">Parameter name.</param>
+        /// <param name="value">Parameter value</param>
+        /// <returns>If parameter not exists, returns false</returns>
+        public bool SetParameter(string name, string value)
+        {
+            if(!_parameters.ContainsKey(name))
+            {
+                return false;
+            }
+            _parameters[name] = value;
+            return true;
+        }
+		
+		/// <summary>
+        /// Add or Set existing parameter value.
+        /// </summary>
+        /// <param name="name">Parameter name.</param>
+        /// <param name="value">Parameter value</param>
+        public void AddOrSetParameter(string name, string value)
+        {
+            if(!SetParameter(name, value))
+				AddParameter(name, value);
+        }
+
+        /// <summary>
+        /// Renders the string with the parameters.
+        /// </summary>
+        /// <returns>Transalted string</returns>
         public override string ToString()
         {
             var sb = new StringBuilder(_initialString);

+ 1 - 0
qdr.fnd.core/qdr.fnd.core.csproj

@@ -20,6 +20,7 @@
     <Version>0.0.5.0-alpha</Version>
     <PackageReleaseNotes>Date:26.9.2024
       - improve class CsvHelper to support with quoted values, add operation to write DataTable to CSV file.
+	  - extends ParametrizedString to set parametr value.
       - update dependencies packages (System.IO.Abstractions)
     </PackageReleaseNotes>
     <PackageReadmeFile>releasenotes.md</PackageReadmeFile>

+ 1 - 0
qdr.fnd.core/releasenotes.md

@@ -4,6 +4,7 @@ Ordered by version descending
 ## 0.0.5.0
 Date:__26.9.2024__
 - improve class CsvHelper to support with quoted values, add operation to write DataTable to CSV file.
+- extends ParametrizedString to set parametr value.
 - update dependencies packages (System.IO.Abstractions)
 ---