From 4fea0719be886879d1b834e55202361619755025 Mon Sep 17 00:00:00 2001 From: Albie Date: Thu, 24 Dec 2020 15:36:26 +0000 Subject: [PATCH 1/2] add enum conversion option --- ...ests.cs => RequestDataCompilationTests.cs} | 28 ++++++++++++++++++- DragonFruit.Common.Data/Methods.cs | 18 ++++++++++++ .../Parameters/FormParameter.cs | 7 +++++ .../Parameters/IProperty.cs | 2 ++ .../Parameters/QueryParameter.cs | 8 ++++++ .../Utils/ParameterUtils.cs | 11 ++++++++ 6 files changed, 73 insertions(+), 1 deletion(-) rename DragonFruit.Common.Data.Tests/{QueryCompilationTests.cs => RequestDataCompilationTests.cs} (67%) diff --git a/DragonFruit.Common.Data.Tests/QueryCompilationTests.cs b/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs similarity index 67% rename from DragonFruit.Common.Data.Tests/QueryCompilationTests.cs rename to DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs index d22d292..820904a 100644 --- a/DragonFruit.Common.Data.Tests/QueryCompilationTests.cs +++ b/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs @@ -8,7 +8,7 @@ namespace DragonFruit.Common.Data.Tests { [TestFixture] - public class QueryCompilationTests + public class RequestDataCompilationTests { [TestCase] public void TestQueries() @@ -25,6 +25,16 @@ public void TestQueries() Assert.IsTrue(query.Contains($"{TestRequest.QueryName}={string.Join(":", TestRequest.TestDataset)}")); } + + [TestCase] + public void TestEnumHandling() + { + var query = new TestRequest().FullUrl.Split('?').Last().Split('&'); + + Assert.IsTrue(query.Contains($"enum={nameof(EnumValues.Red)}")); + Assert.IsTrue(query.Contains($"enum={nameof(EnumValues.Blue).ToLower()}")); + Assert.IsTrue(query.Contains($"enum={(int)EnumValues.Green}")); + } } internal class TestRequest : ApiRequest @@ -45,5 +55,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..6a9993e 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().ToKeyValuePair(keyName, culture), + + // default includes string handling + _ => propertyValue.ToKeyValuePair(keyName, culture) + }; + } else { yield return propertyValue.ToKeyValuePair(keyName, culture); From 0b151ba2006ad9307acc931d45fd8f2bf97810db Mon Sep 17 00:00:00 2001 From: Albie Date: Thu, 24 Dec 2020 15:41:27 +0000 Subject: [PATCH 2/2] set the culture to be used --- .../RequestDataCompilationTests.cs | 6 ++++-- DragonFruit.Common.Data/Utils/ParameterUtils.cs | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs b/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs index 820904a..619c374 100644 --- a/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs +++ b/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs @@ -3,6 +3,7 @@ using System.Linq; using DragonFruit.Common.Data.Parameters; +using DragonFruit.Common.Data.Utils; using NUnit.Framework; namespace DragonFruit.Common.Data.Tests @@ -29,10 +30,11 @@ public void TestQueries() [TestCase] public void TestEnumHandling() { - var query = new TestRequest().FullUrl.Split('?').Last().Split('&'); + 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()}")); + Assert.IsTrue(query.Contains($"enum={nameof(EnumValues.Blue).ToLower(CultureUtils.DefaultCulture)}")); Assert.IsTrue(query.Contains($"enum={(int)EnumValues.Green}")); } } diff --git a/DragonFruit.Common.Data/Utils/ParameterUtils.cs b/DragonFruit.Common.Data/Utils/ParameterUtils.cs index 6a9993e..919322b 100644 --- a/DragonFruit.Common.Data/Utils/ParameterUtils.cs +++ b/DragonFruit.Common.Data/Utils/ParameterUtils.cs @@ -65,7 +65,7 @@ internal static IEnumerable> GetParameter(object yield return attribute.EnumHandling.Value switch { EnumHandlingMode.Numeric => ((int)propertyValue).ToKeyValuePair(keyName, culture), - EnumHandlingMode.StringLower => propertyValue.ToString().ToLower().ToKeyValuePair(keyName, culture), + EnumHandlingMode.StringLower => propertyValue.ToString().ToLower(culture).ToKeyValuePair(keyName, culture), // default includes string handling _ => propertyValue.ToKeyValuePair(keyName, culture)