Skip to content

Commit

Permalink
Added automatic conversion of enum types without explicitly having to…
Browse files Browse the repository at this point in the history
… specify a converter for them.
  • Loading branch information
Ruben Martinez committed Jul 2, 2019
1 parent 64c1fb4 commit 3454080
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
18 changes: 18 additions & 0 deletions DotNet.MultiSourceConfiguration.Tests/ConfigurationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,23 @@ public void BothHandleUndecoratedPropertiesAndPrefixCorectlyWorkTogether()
var configDto = configurationBuilder.Build<UndecoratedPropertyDto>(propertiesPrefix: "someprefix.", handleNonDecoratedProperties: true);
Assert.AreEqual("testValue", configDto.testProperty);
}

private class DtoWithEnumProperties
{
[Property("testProperty")]
public DayOfWeek testProperty { get; set; }
}
[Test]
public void EnumPropertiesAreAutomaticallyConverted()
{
var propertySource1 = new MemorySource();
propertySource1.Add("testProperty", "Monday");

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddSources(propertySource1);

var configDto = configurationBuilder.Build<DtoWithEnumProperties>();
Assert.AreEqual(DayOfWeek.Monday, configDto.testProperty);
}
}
}
9 changes: 8 additions & 1 deletion DotNet.MultiSourceConfiguration/ConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ private void SetPropertyValue<T>(T configurationObject, PropertyInfo dtoProperty
string value;

if (!converters.TryGetValue(dtoProperty.PropertyType, out converter))
throw new InvalidOperationException(string.Format("Unsupported type {0} for field {1}", dtoProperty.PropertyType.Name, propertyAttribute.Property));
{
if (dtoProperty.PropertyType.IsEnum)
{
converter = new EnumConverter(dtoProperty.PropertyType);
}
else
throw new InvalidOperationException(string.Format("Unsupported type {0} for field {1}", dtoProperty.PropertyType.Name, propertyAttribute.Property));
}

if (TryGetStringValue((propertiesPrefix ?? "") + propertyAttribute.Property, out value)) {
dtoProperty.SetValue(configurationObject, converter.FromString(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="ConfigurationBuilder.cs" />
<Compile Include="IConfigurationBuilder.cs" />
<Compile Include="Implementation\DefaultConverterFactory.cs" />
<Compile Include="Implementation\EnumConverter.cs" />
<Compile Include="PropertyAttribute.cs" />
<Compile Include="Implementation\LambdaConverter.cs" />
<Compile Include="Implementation\TypeConverterWrapper.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>DotNet.MultiSourceConfiguration</id>
<version>0.7.2</version>
<version>0.7.3</version>
<title>DotNet.MultiSourceConfiguration</title>
<authors>Rubén Martínez</authors>
<owners>Rubén Martínez</owners>
Expand All @@ -11,7 +11,7 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<summary>Library for reading configuration from multiple sources in .NET.</summary>
<description>Library for reading configuration from multiple sources in .NET. Current sources are in memory properties, app settings file, environment variables and command line arguments. It is also possible to implement your own configuration sources (like database, consul, etcd, etc.)</description>
<releaseNotes>Added support for IEnumerable type conversion.</releaseNotes>
<releaseNotes>Added automatic conversion for enumerations, without explicitly having to specify converters for them.</releaseNotes>
<copyright>Copyright 2019</copyright>
<tags>Configuration Source Multisource Environment Variables Command Line</tags>
</metadata>
Expand Down
27 changes: 27 additions & 0 deletions DotNet.MultiSourceConfiguration/Implementation/EnumConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using MultiSourceConfiguration.Config.Implementation;
using System;

namespace DotNet.MultiSourceConfiguration.Implementation
{
class EnumConverter : UnifiedConverter
{
private Type type;

public EnumConverter(Type type)
{
this.type = type;
}

public override Type Type => type;

public override object FromString(string value)
{
return Enum.Parse(type, value);
}

public override object GetDefaultValue()
{
return Enum.GetValues(type).GetValue(0);
}
}
}
4 changes: 2 additions & 2 deletions DotNet.MultiSourceConfiguration/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.7.2.0")]
[assembly: AssemblyFileVersion("0.7.2.0")]
[assembly: AssemblyVersion("0.7.3.0")]
[assembly: AssemblyFileVersion("0.7.3.0")]

0 comments on commit 3454080

Please sign in to comment.