mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-22 00:59:02 +01:00
Add implicit operators to WireMockList (#823)
* Add implicit operators to WireMockList * .
This commit is contained in:
@@ -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>
|
||||
/// Returns a <see cref="string" /> that represents this instance.
|
||||
/// </summary>
|
||||
public override string ToString()
|
||||
/// <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()
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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[])" />
|
||||
@@ -52,6 +52,6 @@ public class ResponseMessage : IResponseMessage
|
||||
? values.Union(existingValues).ToArray()
|
||||
: values;
|
||||
|
||||
Headers[name] = new WireMockList<string>(newHeaderValues);
|
||||
Headers[name] = newHeaderValues;
|
||||
}
|
||||
}
|
||||
@@ -251,8 +251,15 @@ internal class MappingConverter
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user