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,8 +1,7 @@
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>
@@ -33,12 +32,38 @@ namespace WireMock.Types
{
}
/// <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()
{
return this.Any() ? this.First().ToString() : base.ToString();
switch (Count)
{
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)
{
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;
}
}

View File

@@ -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;