Skip to content

Commit 021cedd

Browse files
Fix all warnings and cleanup FakerLocaleInstance
1 parent 83b8850 commit 021cedd

10 files changed

Lines changed: 24 additions & 27 deletions

File tree

src/Faker.NET.Data/ResourceRetriever.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public ResourceRetriever()
1010
_availableResources = Assembly.GetExecutingAssembly().GetManifestResourceNames() ?? Array.Empty<string>();
1111
}
1212

13-
public T? Get<T>(string locale, string module, string name)
13+
public T Get<T>(string locale, string module, string name)
1414
{
1515
name = $"Faker.NET.Data.{locale.ToUpper()}.{module}.{name}.json";
1616
if (_availableResources.Contains(name))
@@ -20,18 +20,18 @@ public ResourceRetriever()
2020
{
2121
PropertyNameCaseInsensitive = true,
2222
});
23-
return deserialized;
23+
return deserialized ?? Activator.CreateInstance<T>();
2424
}
2525

26-
return default;
26+
return Activator.CreateInstance<T>();
2727
}
2828

2929
public IReadOnlyCollection<T> GetAsArray<T>(string locale, string module, string name)
3030
{
3131
return Get<List<T>>(locale, module, name) ?? new List<T>();
3232
}
3333

34-
public Dictionary<T, U> Get<T, U>(string locale, string module, string name)
34+
public Dictionary<T, U> Get<T, U>(string locale, string module, string name) where T : notnull
3535
{
3636
return Get<Dictionary<T, U>>(locale, module, name) ?? new Dictionary<T, U>();
3737
}

src/Faker.NET/Common/Airline/BaseAirlineInformation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace Faker.NET.Common.Airline;
55
public class BaseAirlineInformation
66
{
77
[JsonPropertyName("name")]
8-
public string Name { get; set; }
8+
public string Name { get; set; } = string.Empty;
99

1010
[JsonPropertyName("iataCode")]
11-
public string IataCode { get; set; }
11+
public string IataCode { get; set; } = string.Empty;
1212

1313
public override string ToString()
1414
{

src/Faker.NET/Common/MustacheHandler.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public string Replace(string template, Dictionary<string, Func<string>> replacem
1313
var indexStarts = Task.Run(() => FindAll(template, "{{").ToArray());
1414
var indexEnds = Task.Run(() => FindAll(template, "}}").ToArray());
1515

16-
_builder = new StringBuilder();
16+
var builder = new StringBuilder();
1717
Task.WaitAll(indexStarts, indexEnds);
1818

1919
int previousEnd = 0;
@@ -22,17 +22,17 @@ public string Replace(string template, Dictionary<string, Func<string>> replacem
2222
{
2323
if (previousEnd < start)
2424
{
25-
_builder.Append(template.AsSpan(previousEnd, start - previousEnd));
25+
builder.Append(template.AsSpan(previousEnd, start - previousEnd));
2626
}
2727

2828
var startToEnd = start + 2;
2929
var word = template.Substring(startToEnd, end - startToEnd);
30-
_builder.Append(GetWordReplacement(word, replacements));
30+
builder.Append(GetWordReplacement(word, replacements));
3131

3232
previousEnd = end + 2;
3333
}
3434

35-
return _builder.ToString();
35+
return builder.ToString();
3636
}
3737

3838
private string GetWordReplacement(string word, Dictionary<string, Func<string>> replacement)
@@ -62,8 +62,6 @@ private IEnumerable<int> FindAll(string template, string characters)
6262
yield break;
6363
}
6464

65-
private StringBuilder _builder;
66-
6765
// private readonly Regex _mustaches = new Regex("^{{(?<word>[\\w\\.]+)(?:\\[(?<index>\\d+)\\])?");
6866
private readonly Regex _index = new Regex("\\[(?<idx>\\d+)\\]");
6967
private readonly Dictionary<string, Func<string>> _commonReplacements = new Dictionary<string, Func<string>>

src/Faker.NET/Common/WeightedArray.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Faker.NET.Common;
44

5-
public class WeightedList<T>
5+
public class WeightedList<T> where T : notnull
66
{
77
public WeightedList()
88
{
@@ -34,6 +34,7 @@ public T GetValue()
3434
{
3535
return _values[0];
3636
}
37+
3738
var value = Faker.Randomizer.Next(_totalWeight);
3839

3940
for (int i = 0; i < _weights.Count; i++)
@@ -44,7 +45,7 @@ public T GetValue()
4445
}
4546
}
4647

47-
return default;
48+
return Activator.CreateInstance<T>();
4849
}
4950

5051
private List<T> _values = new List<T>();

src/Faker.NET/Implementations/Definitions/BaseFakerCommerceProductNameDefinition.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace Faker.NET.Implementations.Definitions
66
internal class BaseFakerCommerceProductNameDefinition : IFakerCommerceProductNameDefinition
77
{
88
[JsonPropertyName("adjective")]
9-
public string[] Adjective { get; set; }
9+
public string[] Adjective { get; set; } = Array.Empty<string>();
1010

1111
[JsonPropertyName("material")]
12-
public string[] Material { get; set; }
12+
public string[] Material { get; set; } = Array.Empty<string>();
1313

1414
[JsonPropertyName("product")]
15-
public string[] Product { get; set; }
15+
public string[] Product { get; set; } = Array.Empty<string>();
1616
}
1717
}

src/Faker.NET/Implementations/Modules/FakerLorem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System.Text;
22
using Faker.NET.Extensions;
3-
using Faker.NET.Interfaces.Definitions;
3+
using Faker.NET.Implementations.Definitions;
44
using Faker.NET.Interfaces.Modules;
55

66
namespace Faker.NET.Implementations.Modules;
77

8-
internal class FakerLorem<T> : FakerDefinitionHandler<T>, IFakerLorem where T : IFakerLoremDefinition
8+
internal class FakerLorem : FakerDefinitionHandler<BaseFakerLoremDefinition>, IFakerLorem
99
{
1010
public string Words(int min, int max) => Data.Words.CreateRandomString(min, max);
1111

src/Faker.NET/Locales/DE/DELocale.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Globalization;
2-
using Faker.NET.Implementations.Definitions;
32
using Faker.NET.Implementations.Modules;
43
using Faker.NET.Locales.DE.Data;
54

@@ -11,7 +10,7 @@ public DELocale()
1110
{
1211
Person = new FakerPerson<DeFakerPersonData>();
1312
PhoneNumber = new FakerPhone<DePhoneNumberData>();
14-
Lorem = new FakerLorem<BaseFakerLoremDefinition>();
13+
Lorem = new FakerLorem();
1514
Location = new FakerLocation<DeLocationData>();
1615
Culture = CultureInfo.GetCultureInfo("de");
1716
}

src/Faker.NET/Locales/EN/ENLocale.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public ENLocale()
1818
Date = new FakerDate();
1919
Internet = new FakerInternet<BaseFakerInternetDefinition>();
2020
PhoneNumber = new FakerPhone<EnFakerPhoneData>();
21-
Lorem = new FakerLorem<BaseFakerLoremDefinition>();
21+
Lorem = new FakerLorem();
2222
}
2323
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Globalization;
2-
using Faker.NET.Implementations.Definitions;
32
using Faker.NET.Implementations.Modules;
43

54
namespace Faker.NET.Locales.ES
@@ -8,8 +7,8 @@ internal class ESLocale : FakerLocaleInstance
87
{
98
public ESLocale()
109
{
11-
Lorem = new FakerLorem<BaseFakerLoremDefinition>();
1210
Culture = CultureInfo.GetCultureInfo("es");
11+
Lorem = new FakerLorem();
1312
}
1413
}
1514
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Globalization;
2-
using Faker.NET.Implementations.Definitions;
32
using Faker.NET.Implementations.Modules;
43
using Faker.NET.Locales.RU.Data;
54

@@ -9,10 +8,11 @@ internal class RULocale : FakerLocaleInstance
98
{
109
public RULocale()
1110
{
11+
Culture = CultureInfo.GetCultureInfo("ru");
12+
1213
Person = new FakerPerson<RuFakerPersonData>();
13-
Lorem = new FakerLorem<BaseFakerLoremDefinition>();
14+
Lorem = new FakerLorem();
1415
PhoneNumber = new FakerPhone<RuPhoneNumberData>();
15-
Culture = CultureInfo.GetCultureInfo("ru");
1616
}
1717
}
1818
}

0 commit comments

Comments
 (0)