* chore: use `TryGetValue`

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

* chore: use `ToArray` over `ToList`
This commit is contained in:
Tim M 2024-01-05 07:45:04 +00:00 коммит произвёл GitHub
Родитель e5bc249551
Коммит 83cf3f8845
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 26 добавлений и 27 удалений

Просмотреть файл

@ -132,9 +132,9 @@ namespace Refit
method => !method.MethodInfo.IsGenericMethod 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); return CloseGenericMethodIfNeeded(possibleMethods[0], genericArgumentTypes);
foreach (var method in possibleMethods) foreach (var method in possibleMethods)
@ -827,9 +827,9 @@ namespace Refit
} }
//if property, add to populate into HttpRequestMessage.Properties //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; isParameterMappedToRequest = true;
} }
@ -1208,34 +1208,33 @@ namespace Refit
var type = value.GetType(); var type = value.GetType();
bool ShouldReturn() => // Bail out early & match string
if (ShouldReturn(type))
return true;
// Get the element type for enumerables
if (value is not IEnumerable)
return false;
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);
static bool ShouldReturn(Type type) =>
type == typeof(string) type == typeof(string)
|| type == typeof(bool) || type == typeof(bool)
|| type == typeof(char) || type == typeof(char)
|| typeof(IFormattable).IsAssignableFrom(type) || typeof(IFormattable).IsAssignableFrom(type)
|| type == typeof(Uri); || type == typeof(Uri);
// Bail out early & match string
if (ShouldReturn())
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 (intType != null)
{
type = intType.GetGenericArguments()[0];
}
}
return ShouldReturn();
} }
static void SetHeader(HttpRequestMessage request, string name, string? value) static void SetHeader(HttpRequestMessage request, string name, string? value)