Add implicit operators to WireMockList (#823)

* Add implicit operators to WireMockList

* .
This commit is contained in:
Stef Heyenrath
2022-10-01 10:50:18 +02:00
committed by GitHub
parent f7b04f3234
commit 430c01a461
3 changed files with 68 additions and 36 deletions

View File

@@ -1,44 +1,69 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
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> /// <summary>
/// A special List which overrides the ToString() to return first value. /// Initializes a new instance of the <see cref="WireMockList{T}"/> class.
/// </summary> /// </summary>
/// <typeparam name="T">The generic type</typeparam> public WireMockList()
/// <seealso cref="List{T}" />
public class WireMockList<T> : List<T>
{ {
/// <summary> }
/// Initializes a new instance of the <see cref="WireMockList{T}"/> class.
/// </summary>
public WireMockList()
{
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WireMockList{T}"/> class. /// Initializes a new instance of the <see cref="WireMockList{T}"/> class.
/// </summary> /// </summary>
/// <param name="collection">The collection whose elements are copied to the new list.</param> /// <param name="collection">The collection whose elements are copied to the new list.</param>
public WireMockList(params T[] collection) : base(collection) public WireMockList(params T[] collection) : base(collection)
{ {
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WireMockList{T}"/> class. /// Initializes a new instance of the <see cref="WireMockList{T}"/> class.
/// </summary> /// </summary>
/// <param name="collection">The collection whose elements are copied to the new list.</param> /// <param name="collection">The collection whose elements are copied to the new list.</param>
public WireMockList(IEnumerable<T> collection) : base(collection) public WireMockList(IEnumerable<T> collection) : base(collection)
{ {
} }
/// <summary> /// <summary>
/// Returns a <see cref="string" /> that represents this instance. /// Operator for setting T
/// </summary> /// </summary>
public override string ToString() /// <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();
} }
} }
} }

View File

@@ -39,7 +39,7 @@ public class ResponseMessage : IResponseMessage
public void AddHeader(string name, string value) public void AddHeader(string name, string value)
{ {
Headers ??= new Dictionary<string, WireMockList<string>>(); Headers ??= new Dictionary<string, WireMockList<string>>();
Headers.Add(name, new WireMockList<string>(value)); Headers.Add(name, value);
} }
/// <inheritdoc cref="IResponseMessage.AddHeader(string, string[])" /> /// <inheritdoc cref="IResponseMessage.AddHeader(string, string[])" />
@@ -52,6 +52,6 @@ public class ResponseMessage : IResponseMessage
? values.Union(existingValues).ToArray() ? values.Union(existingValues).ToArray()
: values; : values;
Headers[name] = new WireMockList<string>(newHeaderValues); Headers[name] = newHeaderValues;
} }
} }

View File

@@ -251,8 +251,15 @@ internal class MappingConverter
var newDictionary = new Dictionary<string, object>(); var newDictionary = new Dictionary<string, object>();
foreach (var entry in dictionary) foreach (var entry in dictionary)
{ {
object value = entry.Value.Count == 1 ? entry.Value.ToString() : entry.Value; // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
newDictionary.Add(entry.Key, value); if (entry.Value.Count == 1)
{
newDictionary.Add(entry.Key, entry.Value.ToString());
}
else
{
newDictionary.Add(entry.Key, entry.Value);
}
} }
return newDictionary; return newDictionary;