Bumped package version to 1.8.5. Added enum route argument converting support. Cleaned up code and added unit test.

This commit is contained in:
MattMo 2024-07-10 06:37:02 -07:00
parent a698e71e4b
commit 670605ce91
3 changed files with 98 additions and 23 deletions

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using MontoyaTech.Rest.Net;
using Xunit;
namespace Rest.Net.Tests
{
public class RouteArgumentConverterTests
{
public enum TestEnum : long
{
A = 1,
B = 2,
C = 3
}
[Fact]
public static void RouteArgumentConverter_Should_Convert_To_Enum()
{
var converted = RouteArgumentConverter.Convert<TestEnum>("1");
converted.GetType().IsEquivalentTo(typeof(TestEnum)).Should().BeTrue();
converted.Should().Be(TestEnum.A);
}
[Fact]
public static void RouteArgumentConverter_Should_Convert_OutOfRange_To_Enum()
{
var converted = RouteArgumentConverter.Convert<TestEnum>("4");
converted.GetType().IsEquivalentTo(typeof(TestEnum)).Should().BeTrue();
((int)converted).Should().Be(4);
}
}
}

View File

@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName> <AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace> <RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile> <GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.8.4</Version> <Version>1.8.5</Version>
<PackageReleaseNotes></PackageReleaseNotes> <PackageReleaseNotes></PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon> <PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup> </PropertyGroup>

View File

@ -19,96 +19,130 @@ namespace MontoyaTech.Rest.Net
/// <returns></returns> /// <returns></returns>
public static T Convert<T>(string input) public static T Convert<T>(string input)
{ {
var typeCode = Type.GetTypeCode(typeof(T)); var result = Convert(typeof(T), input);
if (typeCode == TypeCode.String) if (result == null)
return default(T);
return (T)result;
}
/// <summary>
/// Converts a string to a given type if possible. Otherwise returns null.
/// </summary>
/// <param name="type"></param>
/// <param name="input"></param>
/// <returns></returns>
public static object Convert(Type type, string input)
{
var typeCode = Type.GetTypeCode(type);
if (type.IsEnum)
{ {
return (dynamic)input; return Enum.Parse(type, input, true);
}
else if (typeCode == TypeCode.String)
{
return input;
} }
else if (typeCode == TypeCode.Object) else if (typeCode == TypeCode.Object)
{ {
var castOperator = typeof(T).GetMethod("op_Explicit", new[] { typeof(string) }); var castOperator = type.GetMethod("op_Explicit", new[] { typeof(string) });
if (castOperator == null) if (castOperator == null)
return default(T); return null;
return (T)castOperator.Invoke(null, new[] { input }); return castOperator.Invoke(null, new[] { input });
} }
else if (typeCode == TypeCode.Double) else if (typeCode == TypeCode.Double)
{ {
double.TryParse(input, out double result); double.TryParse(input, out double result);
return (dynamic)result;
return result;
} }
else if (typeCode == TypeCode.Single) else if (typeCode == TypeCode.Single)
{ {
float.TryParse(input, out float result); float.TryParse(input, out float result);
return (dynamic)result;
return result;
} }
else if (typeCode == TypeCode.Decimal) else if (typeCode == TypeCode.Decimal)
{ {
decimal.TryParse(input, out decimal result); decimal.TryParse(input, out decimal result);
return (dynamic)result;
return result;
} }
else if (typeCode == TypeCode.Int64) else if (typeCode == TypeCode.Int64)
{ {
long.TryParse(input, out long result); long.TryParse(input, out long result);
return (dynamic)result;
return result;
} }
else if (typeCode == TypeCode.Int32) else if (typeCode == TypeCode.Int32)
{ {
int.TryParse(input, out int result); int.TryParse(input, out int result);
return (dynamic)result;
return result;
} }
else if (typeCode == TypeCode.Int16) else if (typeCode == TypeCode.Int16)
{ {
short.TryParse(input, out short result); short.TryParse(input, out short result);
return (dynamic)result;
return result;
} }
else if (typeCode == TypeCode.SByte) else if (typeCode == TypeCode.SByte)
{ {
sbyte.TryParse(input, out sbyte result); sbyte.TryParse(input, out sbyte result);
return (dynamic)result;
return result;
} }
else if (typeCode == TypeCode.UInt64) else if (typeCode == TypeCode.UInt64)
{ {
ulong.TryParse(input, out ulong result); ulong.TryParse(input, out ulong result);
return (dynamic)result;
return result;
} }
else if (typeCode == TypeCode.UInt32) else if (typeCode == TypeCode.UInt32)
{ {
uint.TryParse(input, out uint result); uint.TryParse(input, out uint result);
return (dynamic)result;
return result;
} }
else if (typeCode == TypeCode.UInt16) else if (typeCode == TypeCode.UInt16)
{ {
ushort.TryParse(input, out ushort result); ushort.TryParse(input, out ushort result);
return (dynamic)result;
return result;
} }
else if (typeCode == TypeCode.Byte) else if (typeCode == TypeCode.Byte)
{ {
byte.TryParse(input, out byte result); byte.TryParse(input, out byte result);
return (dynamic)result;
return result;
} }
else if (typeCode == TypeCode.Boolean) else if (typeCode == TypeCode.Boolean)
{ {
if (input == "f" || input == "0" || input == "F") if (input == "f" || input == "0" || input == "F")
return (dynamic)false; return false;
bool.TryParse(input, out bool result); bool.TryParse(input, out bool result);
return ((dynamic)result);
return result;
} }
else if (typeCode == TypeCode.DateTime) else if (typeCode == TypeCode.DateTime)
{ {
DateTime.TryParse(input, out DateTime result); DateTime.TryParse(input, out DateTime result);
return ((dynamic)result);
return result;
} }
else if (typeCode == TypeCode.Char) else if (typeCode == TypeCode.Char)
{ {
char.TryParse(input, out char result); char.TryParse(input, out char result);
return ((dynamic)result);
return result;
} }
return default(T); return null;
} }
} }
} }