Skip to content

Commit

Permalink
chore: minor refactor (#1625)
Browse files Browse the repository at this point in the history
* chore: use `TryGetValue`

* chore: refactor `DoNotConvertToQueryMap` invert if and reduce unneeded function calls

* chore: use `ToArray` over `ToList`
  • Loading branch information
TimothyMakkison committed Jan 5, 2024
1 parent e5bc249 commit 83cf3f8
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ RestMethodInfoInternal FindMatchingRestMethodInfo(
method => !method.MethodInfo.IsGenericMethod
);

var possibleMethods = possibleMethodsList.ToList();
var possibleMethods = possibleMethodsList.ToArray();

if (possibleMethods.Count == 1)
if (possibleMethods.Length == 1)
return CloseGenericMethodIfNeeded(possibleMethods[0], genericArgumentTypes);

foreach (var method in possibleMethods)
Expand Down Expand Up @@ -827,9 +827,9 @@ param as IDictionary<string, string>
}
//if property, add to populate into HttpRequestMessage.Properties
if (restMethod.PropertyParameterMap.ContainsKey(i))
if (restMethod.PropertyParameterMap.TryGetValue(i, out var propertyParameter))
{
propertiesToAdd[restMethod.PropertyParameterMap[i]] = param;
propertiesToAdd[propertyParameter] = param;
isParameterMappedToRequest = true;
}
Expand Down Expand Up @@ -1208,34 +1208,33 @@ static bool DoNotConvertToQueryMap(object? value)

var type = value.GetType();

bool ShouldReturn() =>
type == typeof(string)
|| type == typeof(bool)
|| type == typeof(char)
|| typeof(IFormattable).IsAssignableFrom(type)
|| type == typeof(Uri);

// Bail out early & match string
if (ShouldReturn())
if (ShouldReturn(type))
return true;

// Get the element type for enumerables
if (value is IEnumerable enu)
{
var ienu = typeof(IEnumerable<>);
// We don't want to enumerate to get the type, so we'll just look for IEnumerable<T>
var intType = type.GetInterfaces()
.FirstOrDefault(
i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == ienu
);
if (value is not IEnumerable)
return false;

if (intType != null)
{
type = intType.GetGenericArguments()[0];
}
}
var ienu = typeof(IEnumerable<>);
// We don't want to enumerate to get the type, so we'll just look for IEnumerable<T>
var intType = type.GetInterfaces()
.FirstOrDefault(
i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == ienu
);

if (intType == null)
return false;

type = intType.GetGenericArguments()[0];
return ShouldReturn(type);

return ShouldReturn();
static bool ShouldReturn(Type type) =>
type == typeof(string)
|| type == typeof(bool)
|| type == typeof(char)
|| typeof(IFormattable).IsAssignableFrom(type)
|| type == typeof(Uri);
}

static void SetHeader(HttpRequestMessage request, string name, string? value)
Expand Down

0 comments on commit 83cf3f8

Please sign in to comment.