Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions SubMerge/Models/CountryRecord.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using SubMerge.Parsers;

namespace SubMerge.Models;

Expand All @@ -16,18 +17,17 @@ public CountryRecord(int countryId, string name, string code)
Code = code;
}

public int CountryId { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public int CountryId { get; init; }
public string Name { get; init; }
public string Code { get; init; }


public static CountryRecord Parse(string s, IFormatProvider? provider)
{
if (string.IsNullOrWhiteSpace(s)) throw new ArgumentException("Input cannot be null or empty", nameof(s));

var parts = s.Split(',');
if (parts.Length != 3)
throw new FormatException("Input string must contain exactly 3 comma-separated values.");

var parts = CsvParser.SplitAndValidateCsv(s, 3).Select(p => p.Trim()).ToArray();

return new CountryRecord(
int.Parse(parts[0]),
Expand Down
15 changes: 7 additions & 8 deletions SubMerge/Models/GrapeRecord.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using SubMerge.Parsers;

namespace SubMerge.Models;

Expand All @@ -18,19 +19,17 @@ public GrapeRecord(int grapeId, string name, GrapeType type, int harvestYear, in
CountrySourceId = countrySourceId;
}

public int GrapeID { get; set; }
public string Name { get; set; }
public GrapeType Type { get; set; }
public int HarvestYear { get; set; }
public int CountrySourceId { get; set; }
public int GrapeID { get; init; }
public string Name { get; init; }
public GrapeType Type { get; init; }
public int HarvestYear { get; init; }
public int CountrySourceId { get; init; }

public static GrapeRecord Parse(string s, IFormatProvider? provider)
{
if (string.IsNullOrWhiteSpace(s)) throw new ArgumentException("Input cannot be null or empty", nameof(s));

var parts = s.Split(',');
if (parts.Length != 5)
throw new FormatException("Input string must contain exactly 5 comma-separated values.");
var parts = CsvParser.SplitAndValidateCsv(s, 5).Select(p => p.Trim()).ToArray();

return new GrapeRecord(
int.Parse(parts[0]),
Expand Down
17 changes: 8 additions & 9 deletions SubMerge/Models/WineRecord.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using SubMerge.Parsers;

namespace SubMerge.Models;

Expand All @@ -9,7 +10,7 @@ public WineRecord(int wineId, string label, int productionYear, int grapeId, int
if (string.IsNullOrWhiteSpace(label))
throw new ArgumentException("Label cannot be empty", nameof(label));
if (productionYear <= 0)
throw new ArgumentException("Harvest year must be positive", nameof(productionYear));
throw new ArgumentException("Production year must be positive", nameof(productionYear));

WineId = wineId;
Label = label;
Expand All @@ -18,19 +19,17 @@ public WineRecord(int wineId, string label, int productionYear, int grapeId, int
CountryProductionId = countryProductionId;
}

public int WineId { get; set; }
public string Label { get; set; }
public int ProductionYear { get; set; }
public int GrapeId { get; set; }
public int CountryProductionId { get; set; }
public int WineId { get; init; }
public string Label { get; init; }
public int ProductionYear { get; init; }
public int GrapeId { get; init; }
public int CountryProductionId { get; init; }

public static WineRecord Parse(string s, IFormatProvider? provider)
{
if (string.IsNullOrWhiteSpace(s)) throw new ArgumentException("Input cannot be null or empty", nameof(s));

var parts = s.Split(',');
if (parts.Length != 5)
throw new FormatException("Input string must contain exactly 5 comma-separated values.");
var parts = CsvParser.SplitAndValidateCsv(s, 5).Select(p => p.Trim()).ToArray();

return new WineRecord(
int.Parse(parts[0]),
Expand Down
20 changes: 20 additions & 0 deletions SubMerge/Parsers/CsvParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace SubMerge.Parsers;

public static class CsvParser
{
/// <summary>
/// Splits a comma-separated string and validates that it contains exactly N parts.
/// </summary>
/// <param name="s">The input string to parse.</param>
/// <param name="n">The total N of expected parts of csv</param>
/// <returns>An array of strings representing the parsed parts.</returns>
/// <exception cref="FormatException">Thrown if the input string does not contain exactly N comma-separated values.</exception>
public static string[] SplitAndValidateCsv(string s, int n)
{
var parts = s.Split(',');
if (parts.Length != n)
throw new FormatException($"Input string must contain exactly {n} comma-separated values.");

return parts;
}
}
Loading