Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve UrlFormatter #108

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Elastic.Transport/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal static void ThrowIfEmpty<T>(this IEnumerable<T> @object, string paramet
throw new ArgumentNullException(name);
}

internal static bool IsNullOrEmpty(this string value) => string.IsNullOrEmpty(value);
internal static bool IsNullOrEmpty(this string? value) => string.IsNullOrEmpty(value);

internal static string Utf8String(this byte[] bytes) => bytes == null ? null : Encoding.UTF8.GetString(bytes, 0, bytes.Length);

Expand Down
116 changes: 85 additions & 31 deletions src/Elastic.Transport/Requests/UrlFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// See the LICENSE file in the project root for more information

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Runtime.Serialization;
using System.Text;
using Elastic.Transport.Extensions;

namespace Elastic.Transport;
Expand All @@ -22,59 +22,113 @@ public sealed class UrlFormatter : IFormatProvider, ICustomFormatter
public UrlFormatter(ITransportConfiguration settings) => _settings = settings;

/// <inheritdoc cref="ICustomFormatter.Format"/>>
public string Format(string format, object arg, IFormatProvider formatProvider)
public string Format(string? format, object? arg, IFormatProvider? formatProvider)
{
if (arg == null) throw new ArgumentNullException();

if (format == "r") return arg.ToString();
if (format == "r") return arg.ToString() ?? string.Empty;

var value = CreateString(arg, _settings);
if (value.IsNullOrEmpty() && !format.IsNullOrEmpty())
throw new ArgumentException($"The parameter: {format} to the url is null or empty");

return value.IsNullOrEmpty() ? string.Empty : Uri.EscapeDataString(value);
return string.IsNullOrEmpty(value) ? string.Empty : Uri.EscapeDataString(value);
}

/// <inheritdoc cref="IFormatProvider.GetFormat"/>
public object GetFormat(Type formatType) => formatType == typeof(ICustomFormatter) ? this : null;
public object? GetFormat(Type formatType) => formatType == typeof(ICustomFormatter) ? this : null;

/// <inheritdoc cref="CreateString(object, ITransportConfiguration)"/>
public string CreateString(object value) => CreateString(value, _settings);
public string? CreateString(object? value) => CreateString(value, _settings);

/// <summary> Creates a query string representation for <paramref name="value"/> </summary>
public static string? CreateString(object? value, ITransportConfiguration settings)
public static string? CreateString(object? value, ITransportConfiguration settings) =>
value switch
{
null => null,
string s => s,
string[] ss => string.Join(",", ss),
Enum e => e.GetStringValue(),
bool b => b ? "true" : "false",
DateTimeOffset offset => offset.ToString("o"),
TimeSpan timeSpan => timeSpan.ToTimeUnit(),
// Special handling to support non-zero based arrays
Array pns => CreateStringFromArray(pns, settings),
// Performance optimization for directly indexable collections
IList pns => CreateStringFromIList(pns, settings),
// Generic implementation for all other collections
IEnumerable pns => CreateStringFromIEnumerable(pns, settings),
_ => ResolveUrlParameterOrDefault(value, settings)
};

private static string CreateStringFromArray(Array value, ITransportConfiguration settings)
{
switch (value)
switch (value.Length)
{
case 0:
return string.Empty;
case 1:
return ResolveUrlParameterOrDefault(value.GetValue(value.GetLowerBound(0)), settings);
}

var sb = new StringBuilder();

for (var i = value.GetLowerBound(0); i <= value.GetUpperBound(0); ++i)
{
case null: return null;
case string s: return s;
case string[] ss: return string.Join(",", ss);
case Enum e: return e.GetStringValue();
case bool b: return b ? "true" : "false";
case DateTimeOffset offset: return offset.ToString("o");
case IEnumerable<object> pns:
return CreateStringFromIEnumerable(pns, settings);

case Array pns:
return CreateStringFromIEnumerable(ConvertArrayToEnumerable(pns), settings);

case TimeSpan timeSpan: return timeSpan.ToTimeUnit();
default:
return ResolveUrlParameterOrDefault(value, settings);
if (sb.Length != 0)
sb.Append(',');

sb.Append(ResolveUrlParameterOrDefault(value.GetValue(i), settings));
}

return sb.ToString();
}

private static string CreateStringFromIEnumerable(IEnumerable<object> value, ITransportConfiguration settings) =>
string.Join(",", value.Select(o => ResolveUrlParameterOrDefault(o, settings)));
private static string CreateStringFromIList(IList value, ITransportConfiguration settings)
{
switch (value.Count)
{
case 0:
return string.Empty;
case 1:
return ResolveUrlParameterOrDefault(value[0], settings);
}

var sb = new StringBuilder();

private static IEnumerable<object> ConvertArrayToEnumerable(Array array)
for (var i = 0; i < value.Count; ++i)
{
if (sb.Length != 0)
sb.Append(',');

sb.Append(ResolveUrlParameterOrDefault(value[i], settings));
}

return sb.ToString();
}

private static string CreateStringFromIEnumerable(IEnumerable value, ITransportConfiguration settings)
{
for (var i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
yield return array.GetValue(i);
var sb = new StringBuilder();

foreach (var v in value)
{
if (sb.Length != 0)
sb.Append(',');

sb.Append(ResolveUrlParameterOrDefault(v, settings));
}

return sb.ToString();
}

private static string ResolveUrlParameterOrDefault(object value, ITransportConfiguration settings) =>
value is IUrlParameter urlParam ? urlParam.GetString(settings) : GetEnumMemberName(value) ?? value.ToString();
private static string ResolveUrlParameterOrDefault(object? value, ITransportConfiguration settings) =>
value switch
{
null => string.Empty,
IUrlParameter urlParam => urlParam.GetString(settings),
_ => GetEnumMemberName(value) ?? value.ToString() ?? string.Empty
};

private static string? GetEnumMemberName(object value)
{
Expand Down