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

Add implicit operators to WireMockList #823

Merged
merged 2 commits into from
Oct 1, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 57 additions & 32 deletions src/WireMock.Net.Abstractions/Types/WireMockList.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,69 @@
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;

namespace WireMock.Types
namespace WireMock.Types;

/// <summary>
/// A special List which overrides the ToString() to return first value.
/// </summary>
/// <typeparam name="T">The generic type</typeparam>
/// <seealso cref="List{T}" />
public class WireMockList<T> : List<T>
{
/// <summary>
/// A special List which overrides the ToString() to return first value.
/// Initializes a new instance of the <see cref="WireMockList{T}"/> class.
/// </summary>
/// <typeparam name="T">The generic type</typeparam>
/// <seealso cref="List{T}" />
public class WireMockList<T> : List<T>
public WireMockList()
{
/// <summary>
/// Initializes a new instance of the <see cref="WireMockList{T}"/> class.
/// </summary>
public WireMockList()
{
}
}

/// <summary>
/// Initializes a new instance of the <see cref="WireMockList{T}"/> class.
/// </summary>
/// <param name="collection">The collection whose elements are copied to the new list.</param>
public WireMockList(params T[] collection) : base(collection)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WireMockList{T}"/> class.
/// </summary>
/// <param name="collection">The collection whose elements are copied to the new list.</param>
public WireMockList(params T[] collection) : base(collection)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="WireMockList{T}"/> class.
/// </summary>
/// <param name="collection">The collection whose elements are copied to the new list.</param>
public WireMockList(IEnumerable<T> collection) : base(collection)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WireMockList{T}"/> class.
/// </summary>
/// <param name="collection">The collection whose elements are copied to the new list.</param>
public WireMockList(IEnumerable<T> collection) : base(collection)
{
}

/// <summary>
/// Operator for setting T
/// </summary>
/// <param name="value">The value to set.</param>
public static implicit operator WireMockList<T>(T value) => new(value);

/// <summary>
/// Operator for setting T[]
/// </summary>
/// <param name="values">The values to set.</param>
public static implicit operator WireMockList<T>(T[] values) => new(values);

/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
/// </summary>
public override string ToString()
/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
/// </summary>
public override string ToString()
{
switch (Count)
{
return this.Any() ? this.First().ToString() : base.ToString();
case 0:
return string.Empty;

case 1:
if (this[0] is string strValue)
{
return strValue;
}

return this[0]?.ToString();

default:
return base.ToString();
}
}
}
4 changes: 2 additions & 2 deletions src/WireMock.Net/ResponseMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ResponseMessage : IResponseMessage
public void AddHeader(string name, string value)
{
Headers ??= new Dictionary<string, WireMockList<string>>();
Headers.Add(name, new WireMockList<string>(value));
Headers.Add(name, value);
}

/// <inheritdoc cref="IResponseMessage.AddHeader(string, string[])" />
Expand All @@ -52,6 +52,6 @@ public void AddHeader(string name, params string[] values)
? values.Union(existingValues).ToArray()
: values;

Headers[name] = new WireMockList<string>(newHeaderValues);
Headers[name] = newHeaderValues;
}
}
11 changes: 9 additions & 2 deletions src/WireMock.Net/Serialization/MappingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,15 @@ private static IDictionary<string, object> MapHeaders(IDictionary<string, WireMo
var newDictionary = new Dictionary<string, object>();
foreach (var entry in dictionary)
{
object value = entry.Value.Count == 1 ? entry.Value.ToString() : entry.Value;
newDictionary.Add(entry.Key, value);
// ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
if (entry.Value.Count == 1)
{
newDictionary.Add(entry.Key, entry.Value.ToString());
}
else
{
newDictionary.Add(entry.Key, entry.Value);
}
}

return newDictionary;
Expand Down