Skip to content

Commit e7dab43

Browse files
handling of various nullable
1 parent e6ab4dd commit e7dab43

5 files changed

Lines changed: 25 additions & 16 deletions

File tree

CodiceFiscale/helpers/CodeExtractorsHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static string ExtractConsonantsAndVowels(List<char> consonants, List<char
2424
}
2525

2626
// Method to get a date from string or datetime
27-
public static DateTime? ExtractDate(object date, string separator = "-")
27+
public static DateTime? ExtractDate(object? date, string separator = "-")
2828
{
2929
if (date == null)
3030
{
@@ -36,8 +36,8 @@ public static string ExtractConsonantsAndVowels(List<char> consonants, List<char
3636
return ((DateTime)date).ToUniversalTime();
3737
}
3838

39-
var dateString = date.ToString();
40-
var dateSlug = StringsHelper.Slugify(dateString);
39+
string dateString = date.ToString();
40+
string dateSlug = StringsHelper.Slugify(dateString);
4141

4242
// Split the date
4343
var dateParts = dateSlug.Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
@@ -68,14 +68,14 @@ public static string ExtractConsonantsAndVowels(List<char> consonants, List<char
6868
}
6969

7070
// Method to find the birthplace
71-
public static List<Dictionary<string, object>> ExtractBirthplace(string birthplace, object birthdate = null)
71+
public static List<Dictionary<string, object>>? ExtractBirthplace(string birthplace, object? birthdate = null)
7272
{
7373
// slugify the birthplace
7474
var tmpBirthplaceSlug = StringsHelper.Slugify(birthplace);
7575

7676
// if it's a alphanumeric code it must be uppercase, if it's a place name it must be lowercase
7777
var birthplaceSlug = (StringsHelper.IsAlphaNumeric(tmpBirthplaceSlug)) ? tmpBirthplaceSlug.ToUpper() : tmpBirthplaceSlug.ToLower();
78-
List<Dictionary<string, object>> birthplacesOptions = null;
78+
List<Dictionary<string, object>>? birthplacesOptions = null;
7979

8080
// Check if the slug of the birthplace is in the data
8181
if (DataHelper.INDEXED_DATA["municipalities"].ContainsKey(birthplaceSlug))

CodiceFiscale/helpers/DataHelper.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ public static T GetData<T>(string filename)
2020
{
2121
string filePath = Path.Combine(GetDataBaseDir(), "data", filename);
2222
string jsonContent = File.ReadAllText(filePath);
23-
return JsonSerializer.Deserialize<T>(jsonContent);
23+
T? serializedData = JsonSerializer.Deserialize<T>(jsonContent);
24+
if (serializedData == null)
25+
{
26+
throw new InvalidOperationException($"Failed to deserialize JSON file: {filename}");
27+
}
28+
return serializedData;
2429
}
2530

2631
// Returns municipalities data (municipalities.json)

CodiceFiscale/helpers/StringsHelper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public static bool IsAlphaNumeric(string s)
1515
// Method to slugify a string
1616
public static string Slugify(string input)
1717
{
18+
if (input == null)
19+
{
20+
return "";
21+
}
1822
return Regex.Replace(input.ToLower(), @"\s+", "-");
1923
}
2024
}

CodiceFiscale/models/Country.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ namespace CodiceFiscaleLib.Models;
55
public class Country
66
{
77
[JsonPropertyName("code")]
8-
public string Code { get; set; }
8+
public string? Code { get; set; }
99

1010
[JsonPropertyName("name_slugs")]
11-
public List<string> NameSlugs { get; set; }
11+
public List<string>? NameSlugs { get; set; }
1212

1313
// Convert the class to a dictionary
1414
public Dictionary<string, object> ToDictionary()
1515
{
1616
return new Dictionary<string, object>()
1717
{
18-
{ "code", Code },
19-
{ "name_slugs", NameSlugs }
18+
{ "code", Code ?? string.Empty },
19+
{ "name_slugs", NameSlugs ?? new List<string>() }
2020
};
2121
}
2222
}

CodiceFiscale/models/Municipality.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ namespace CodiceFiscaleLib.Models;
55
public class Municipality
66
{
77
[JsonPropertyName("code")]
8-
public string Code { get; set; }
8+
public string? Code { get; set; }
99

1010
[JsonPropertyName("province")]
11-
public string Province { get; set; }
11+
public string? Province { get; set; }
1212

1313
[JsonPropertyName("name_slugs")]
14-
public List<string> NameSlugs { get; set; }
14+
public List<string>? NameSlugs { get; set; }
1515

1616
// Convert the class to a dictionary
1717
public Dictionary<string, object> ToDictionary()
1818
{
1919
return new Dictionary<string, object>()
2020
{
21-
{ "code", Code },
22-
{ "province", Province },
23-
{ "name_slugs", NameSlugs }
21+
{ "code", Code ?? string.Empty },
22+
{ "province", Province ?? string.Empty },
23+
{ "name_slugs", NameSlugs ?? new List<string>() }
2424
};
2525
}
2626
}

0 commit comments

Comments
 (0)