Skip to content

Commit

Permalink
Merge pull request #55 from aspriddell/enum-serializing
Browse files Browse the repository at this point in the history
add enum conversion option
  • Loading branch information
aspriddell authored Dec 24, 2020
2 parents ee3e88a + 0b151ba commit 47d0ed4
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
}
}
18 changes: 18 additions & 0 deletions DragonFruit.Common.Data/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,22 @@ public enum CollectionConversionMode
/// </summary>
Concatenated
}

public enum EnumHandlingMode
{
/// <summary>
/// Convert to the integer representation
/// </summary>
Numeric,

/// <summary>
/// Convert to string form
/// </summary>
String,

/// <summary>
/// Convert to lowercase string form
/// </summary>
StringLower
}
}
7 changes: 7 additions & 0 deletions DragonFruit.Common.Data/Parameters/FormParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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; }
}
Expand Down
2 changes: 2 additions & 0 deletions DragonFruit.Common.Data/Parameters/IProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface IProperty

CollectionConversionMode? CollectionHandling { get; set; }

EnumHandlingMode? EnumHandling { get; set; }

string? CollectionSeparator { get; set; }
}
}
8 changes: 8 additions & 0 deletions DragonFruit.Common.Data/Parameters/QueryParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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; }
}
}
11 changes: 11 additions & 0 deletions DragonFruit.Common.Data/Utils/ParameterUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ internal static IEnumerable<KeyValuePair<string, string>> GetParameter<T>(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);
Expand Down

0 comments on commit 47d0ed4

Please sign in to comment.