Skip to content

Commit 6502975

Browse files
committed
Use the new helpers as real extension methods
Root cause: the previous revision kept the split helper classes but still invoked them like static utilities, which did not match the requested extension-method style or the naming implied by the new Geocoding.Extensions surface.
1 parent 4164eb0 commit 6502975

8 files changed

Lines changed: 23 additions & 25 deletions

File tree

src/Geocoding.Core/Extensions/CollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static class CollectionExtensions
1313
/// <typeparam name="T">The collection item type.</typeparam>
1414
/// <param name="collection">The collection to test.</param>
1515
/// <returns><c>true</c> when the collection is null or empty.</returns>
16-
public static bool IsNullOrEmpty<T>([NotNullWhen(false)] ICollection<T>? collection)
16+
public static bool IsNullOrEmpty<T>([NotNullWhen(false)] this ICollection<T>? collection)
1717
{
1818
return collection is null || collection.Count == 0;
1919
}

src/Geocoding.Core/Extensions/EnumerableExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class EnumerableExtensions
1111
/// <typeparam name="T">The enumerable item type.</typeparam>
1212
/// <param name="source">The source enumerable.</param>
1313
/// <param name="actor">The action to execute for each item.</param>
14-
public static void ForEach<T>(IEnumerable<T>? source, Action<T> actor)
14+
public static void ForEach<T>(this IEnumerable<T>? source, Action<T> actor)
1515
{
1616
if (actor is null)
1717
throw new ArgumentNullException(nameof(actor));

src/Geocoding.Core/Extensions/JsonExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static class JsonExtensions
2121
/// </summary>
2222
/// <param name="value">The object to serialize.</param>
2323
/// <returns>The JSON payload, or an empty string when the input is null.</returns>
24-
public static string ToJson(object? value)
24+
public static string ToJson(this object? value)
2525
{
2626
if (value is null)
2727
return String.Empty;
@@ -35,7 +35,7 @@ public static string ToJson(object? value)
3535
/// <typeparam name="T">The destination type.</typeparam>
3636
/// <param name="json">The JSON payload.</param>
3737
/// <returns>A deserialized instance, or default value for blank input.</returns>
38-
public static T? FromJson<T>(string? json)
38+
public static T? FromJson<T>(this string? json)
3939
{
4040
if (String.IsNullOrWhiteSpace(json))
4141
return default;

src/Geocoding.MapQuest/BaseRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public virtual string RequestBody
135135
{
136136
get
137137
{
138-
return JsonExtensions.ToJson(this);
138+
return this.ToJson();
139139
}
140140
}
141141

src/Geocoding.MapQuest/BatchGeocodeRequest.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class BatchGeocodeRequest : BaseRequest
1616
public BatchGeocodeRequest(string key, ICollection<string> addresses)
1717
: base(key)
1818
{
19-
if (CollectionExtensions.IsNullOrEmpty(addresses))
19+
if (addresses.IsNullOrEmpty())
2020
throw new ArgumentException("addresses can not be null or empty");
2121

2222
Locations = (from l in addresses select new LocationRequest(l)).ToArray();
@@ -34,15 +34,13 @@ public ICollection<LocationRequest> Locations
3434
get { return _locations; }
3535
set
3636
{
37-
if (CollectionExtensions.IsNullOrEmpty(value))
37+
if (value.IsNullOrEmpty())
3838
throw new ArgumentNullException("Locations can not be null or empty!");
3939

4040
_locations.Clear();
41-
EnumerableExtensions.ForEach(
42-
from v in value
43-
where v is not null
44-
select v,
45-
v => _locations.Add(v));
41+
(from v in value
42+
where v is not null
43+
select v).ForEach(v => _locations.Add(v));
4644

4745
if (_locations.Count == 0)
4846
throw new InvalidOperationException("At least one valid Location is required");

src/Geocoding.MapQuest/MapQuestGeocoder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public MapQuestGeocoder(string key)
5050

5151
private IEnumerable<Address> HandleSingleResponse(MapQuestResponse res)
5252
{
53-
return res is not null && !CollectionExtensions.IsNullOrEmpty(res.Results)
53+
return res is not null && !res.Results.IsNullOrEmpty()
5454
? HandleSingleResponse(from r in res.Results.OfType<MapQuestResult>()
5555
from l in r.Locations?.OfType<MapQuestLocation>() ?? Enumerable.Empty<MapQuestLocation>()
5656
select l)
@@ -139,7 +139,7 @@ where l.Quality < Quality.COUNTRY
139139
using var client = BuildClient();
140140
using var request = CreateRequest(f);
141141
MapQuestResponse r = await Parse(client, request, cancellationToken).ConfigureAwait(false);
142-
if (r is not null && !CollectionExtensions.IsNullOrEmpty(r.Results))
142+
if (r is not null && !r.Results.IsNullOrEmpty())
143143
{
144144
foreach (MapQuestResult o in r.Results)
145145
{
@@ -226,7 +226,7 @@ private async Task<MapQuestResponse> Parse(HttpClient client, HttpRequestMessage
226226
if (String.IsNullOrWhiteSpace(json))
227227
throw new Exception("Remote system response with blank: " + requestInfo);
228228

229-
MapQuestResponse? o = JsonExtensions.FromJson<MapQuestResponse>(json);
229+
MapQuestResponse? o = json.FromJson<MapQuestResponse>();
230230
if (o is null)
231231
throw new Exception("Unable to deserialize remote response: " + requestInfo);
232232

@@ -260,7 +260,7 @@ private static string BuildResponsePreview(string? body)
260260
where !String.IsNullOrWhiteSpace(a)
261261
group a by a into ag
262262
select ag.Key).ToArray();
263-
if (CollectionExtensions.IsNullOrEmpty(adr))
263+
if (adr.IsNullOrEmpty())
264264
throw new ArgumentException("Atleast one none blank item is required in addresses");
265265

266266
var f = new BatchGeocodeRequest(_key, adr) { UseOSM = UseOSM };
@@ -270,7 +270,7 @@ group a by a into ag
270270

271271
private ICollection<ResultItem> HandleBatchResponse(MapQuestResponse res)
272272
{
273-
if (res is not null && !CollectionExtensions.IsNullOrEmpty(res.Results))
273+
if (res is not null && !res.Results.IsNullOrEmpty())
274274
{
275275
return (from r in res.Results.OfType<MapQuestResult>()
276276
let locations = r.Locations?.OfType<MapQuestLocation>().ToArray() ?? Array.Empty<MapQuestLocation>()

src/Geocoding.Microsoft/BingMapsGeocoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ protected virtual IEnumerable<BingAddress> ParseResponse(Json.Response response)
242242
{
243243
var list = new List<BingAddress>();
244244

245-
if (CollectionExtensions.IsNullOrEmpty(response.ResourceSets))
245+
if (response.ResourceSets.IsNullOrEmpty())
246246
return list;
247247

248248
foreach (var resourceSet in response.ResourceSets)

test/Geocoding.Tests/TolerantStringEnumConverterTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void FromJson_UnknownStringForEnumWithUnknownMember_ReturnsUnknown()
1212
const string json = "{\"value\":\"something-new\"}";
1313

1414
// Act
15-
var model = JsonExtensions.FromJson<EnumWithUnknownModel>(json);
15+
var model = json.FromJson<EnumWithUnknownModel>();
1616

1717
// Assert
1818
Assert.NotNull(model);
@@ -26,7 +26,7 @@ public void FromJson_UnknownNumberForEnumWithUnknownMember_ReturnsUnknown()
2626
const string json = "{\"value\":999}";
2727

2828
// Act
29-
var model = JsonExtensions.FromJson<EnumWithUnknownModel>(json);
29+
var model = json.FromJson<EnumWithUnknownModel>();
3030

3131
// Assert
3232
Assert.NotNull(model);
@@ -40,7 +40,7 @@ public void FromJson_NullableEnumWithNullValue_ReturnsNull()
4040
const string json = "{\"value\":null}";
4141

4242
// Act
43-
var model = JsonExtensions.FromJson<NullableEnumWithUnknownModel>(json);
43+
var model = json.FromJson<NullableEnumWithUnknownModel>();
4444

4545
// Assert
4646
Assert.NotNull(model);
@@ -54,7 +54,7 @@ public void FromJson_UnknownStringWithoutUnknownMember_ReturnsDefaultValue()
5454
const string json = "{\"value\":\"something-new\"}";
5555

5656
// Act
57-
var model = JsonExtensions.FromJson<EnumWithoutUnknownModel>(json);
57+
var model = json.FromJson<EnumWithoutUnknownModel>();
5858

5959
// Assert
6060
Assert.NotNull(model);
@@ -68,7 +68,7 @@ public void FromJson_NumericStringForEnumWithUnknownMember_ReturnsUnknown()
6868
const string json = "{\"value\":\"999\"}";
6969

7070
// Act
71-
var model = JsonExtensions.FromJson<EnumWithUnknownModel>(json);
71+
var model = json.FromJson<EnumWithUnknownModel>();
7272

7373
// Assert
7474
Assert.NotNull(model);
@@ -82,7 +82,7 @@ public void FromJson_NumericValueForByteEnum_ReturnsKnownValue()
8282
const string json = "{\"value\":1}";
8383

8484
// Act
85-
var model = JsonExtensions.FromJson<ByteEnumWithUnknownModel>(json);
85+
var model = json.FromJson<ByteEnumWithUnknownModel>();
8686

8787
// Assert
8888
Assert.NotNull(model);
@@ -96,7 +96,7 @@ public void FromJson_UnknownNumericValueForByteEnum_ReturnsUnknown()
9696
const string json = "{\"value\":99}";
9797

9898
// Act
99-
var model = JsonExtensions.FromJson<ByteEnumWithUnknownModel>(json);
99+
var model = json.FromJson<ByteEnumWithUnknownModel>();
100100

101101
// Assert
102102
Assert.NotNull(model);

0 commit comments

Comments
 (0)