diff --git a/DragonFruit.Common.Data.Tests/QueryCompilationTests.cs b/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs similarity index 65% rename from DragonFruit.Common.Data.Tests/QueryCompilationTests.cs rename to DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs index d22d292..619c374 100644 --- a/DragonFruit.Common.Data.Tests/QueryCompilationTests.cs +++ b/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs @@ -3,12 +3,13 @@ using System.Linq; using DragonFruit.Common.Data.Parameters; +using DragonFruit.Common.Data.Utils; using NUnit.Framework; namespace DragonFruit.Common.Data.Tests { [TestFixture] - public class QueryCompilationTests + public class RequestDataCompilationTests { [TestCase] public void TestQueries() @@ -25,6 +26,17 @@ public void TestQueries() Assert.IsTrue(query.Contains($"{TestRequest.QueryName}={string.Join(":", TestRequest.TestDataset)}")); } + + [TestCase] + public void TestEnumHandling() + { + var request = new TestRequest(); + var query = request.FullUrl.Split('?').Last().Split('&'); + + Assert.IsTrue(query.Contains($"enum={nameof(EnumValues.Red)}")); + Assert.IsTrue(query.Contains($"enum={nameof(EnumValues.Blue).ToLower(CultureUtils.DefaultCulture)}")); + Assert.IsTrue(query.Contains($"enum={(int)EnumValues.Green}")); + } } internal class TestRequest : ApiRequest @@ -45,5 +57,21 @@ internal class TestRequest : ApiRequest [QueryParameter(QueryName, CollectionConversionMode.Concatenated, CollectionSeparator = ":")] public string[] ConcatenatedData { get; set; } = TestDataset; + + [QueryParameter("enum", EnumHandlingMode.String)] + public EnumValues StringEnum => EnumValues.Red; + + [QueryParameter("enum", EnumHandlingMode.StringLower)] + public EnumValues SmallStringEnum => EnumValues.Blue; + + [QueryParameter("enum", EnumHandlingMode.Numeric)] + public EnumValues NumericEnum => EnumValues.Green; + } + + public enum EnumValues + { + Red, + Blue, + Green = 512 } } diff --git a/DragonFruit.Common.Data/Methods.cs b/DragonFruit.Common.Data/Methods.cs index 2ae94cd..2b4f4de 100644 --- a/DragonFruit.Common.Data/Methods.cs +++ b/DragonFruit.Common.Data/Methods.cs @@ -61,4 +61,22 @@ public enum CollectionConversionMode /// Concatenated } + + public enum EnumHandlingMode + { + /// + /// Convert to the integer representation + /// + Numeric, + + /// + /// Convert to string form + /// + String, + + /// + /// Convert to lowercase string form + /// + StringLower + } } diff --git a/DragonFruit.Common.Data/Parameters/FormParameter.cs b/DragonFruit.Common.Data/Parameters/FormParameter.cs index 7a35abd..b78af59 100644 --- a/DragonFruit.Common.Data/Parameters/FormParameter.cs +++ b/DragonFruit.Common.Data/Parameters/FormParameter.cs @@ -19,6 +19,12 @@ public FormParameter(string name) Name = name; } + public FormParameter(string name, EnumHandlingMode enumHandling) + : this(name) + { + EnumHandling = enumHandling; + } + public FormParameter(string name, CollectionConversionMode collectionHandling) : this(name) { @@ -27,6 +33,7 @@ public FormParameter(string name, CollectionConversionMode collectionHandling) public string? Name { get; set; } public CollectionConversionMode? CollectionHandling { get; set; } + public EnumHandlingMode? EnumHandling { get; set; } public string? CollectionSeparator { get; set; } } diff --git a/DragonFruit.Common.Data/Parameters/IProperty.cs b/DragonFruit.Common.Data/Parameters/IProperty.cs index 2ea4744..ad21926 100644 --- a/DragonFruit.Common.Data/Parameters/IProperty.cs +++ b/DragonFruit.Common.Data/Parameters/IProperty.cs @@ -11,6 +11,8 @@ public interface IProperty CollectionConversionMode? CollectionHandling { get; set; } + EnumHandlingMode? EnumHandling { get; set; } + string? CollectionSeparator { get; set; } } } diff --git a/DragonFruit.Common.Data/Parameters/QueryParameter.cs b/DragonFruit.Common.Data/Parameters/QueryParameter.cs index 0fb28bc..f6aad14 100644 --- a/DragonFruit.Common.Data/Parameters/QueryParameter.cs +++ b/DragonFruit.Common.Data/Parameters/QueryParameter.cs @@ -19,6 +19,12 @@ public QueryParameter(string name) Name = name; } + public QueryParameter(string name, EnumHandlingMode enumHandling) + : this(name) + { + EnumHandling = enumHandling; + } + public QueryParameter(string name, CollectionConversionMode collectionConversionMode) : this(name) { @@ -27,6 +33,8 @@ public QueryParameter(string name, CollectionConversionMode collectionConversion public string? Name { get; set; } public CollectionConversionMode? CollectionHandling { get; set; } + public EnumHandlingMode? EnumHandling { get; set; } + public string? CollectionSeparator { get; set; } } } diff --git a/DragonFruit.Common.Data/Utils/ParameterUtils.cs b/DragonFruit.Common.Data/Utils/ParameterUtils.cs index 609299c..919322b 100644 --- a/DragonFruit.Common.Data/Utils/ParameterUtils.cs +++ b/DragonFruit.Common.Data/Utils/ParameterUtils.cs @@ -60,6 +60,17 @@ internal static IEnumerable> GetParameter(object yield return entry; } } + else if (property.PropertyType.IsEnum && attribute.EnumHandling.HasValue) + { + yield return attribute.EnumHandling.Value switch + { + EnumHandlingMode.Numeric => ((int)propertyValue).ToKeyValuePair(keyName, culture), + EnumHandlingMode.StringLower => propertyValue.ToString().ToLower(culture).ToKeyValuePair(keyName, culture), + + // default includes string handling + _ => propertyValue.ToKeyValuePair(keyName, culture) + }; + } else { yield return propertyValue.ToKeyValuePair(keyName, culture);