Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConfigurationKeyNameAttribute to binder and updated tests. #50338

Merged
merged 16 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@
// Changes to this file must follow the https://aka.ms/api-review process.
// ------------------------------------------------------------------------------

using System;

namespace Microsoft.Extensions.Configuration
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public sealed class ConfigurationKeyNameAttribute : Attribute
{
public ConfigurationKeyNameAttribute(string name) => Name = name;

public string Name { get; }
}
public static partial class ConfigurationExtensions
{
public static Microsoft.Extensions.Configuration.IConfigurationBuilder Add<TSource>(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, System.Action<TSource> configureSource) where TSource : Microsoft.Extensions.Configuration.IConfigurationSource, new() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.Extensions.Configuration
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public sealed class ConfigurationKeyNameAttribute : Attribute
{
public ConfigurationKeyNameAttribute(string name) => Name = name;

public string Name { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig
return;
}

propertyValue = BindInstance(property.PropertyType, propertyValue, config.GetSection(property.Name), options);
propertyValue = GetPropertyValue(property, instance, config, options);

if (propertyValue != null && hasSetter)
{
Expand Down Expand Up @@ -575,5 +575,42 @@ private static IEnumerable<PropertyInfo> GetAllProperties(Type type)

return allProperties;
}

private static object GetPropertyValue(PropertyInfo property, object instance, IConfiguration config, BinderOptions options)
{
object propertyValue = property.GetValue(instance);
string propertyName = GetPropertyAttributeKeyName(property);

if (string.IsNullOrEmpty(propertyName))
{
propertyName = property.Name;
}

return BindInstance(property.PropertyType, propertyValue, config.GetSection(propertyName), options);
}

private static string GetPropertyAttributeKeyName(MemberInfo property)
{
if (property == null)
{
return string.Empty;
}

foreach (var attributeData in property.GetCustomAttributesData())
{
if (attributeData.AttributeType != typeof(ConfigurationKeyNameAttribute))
{
continue;
}

var constructorArgument = attributeData.ConstructorArguments.FirstOrDefault();
if (constructorArgument.ArgumentType == typeof(string))
{
return constructorArgument.Value?.ToString() ?? string.Empty;
}
}

return string.Empty;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public ComplexOptions()
internal string InternalProperty { get; set; }
protected string ProtectedProperty { get; set; }


[ConfigurationKeyName("Named_Property")]
public string NamedProperty { get; set; }

protected string ProtectedPrivateSet { get; private set; }

private string PrivateReadOnly { get; }
Expand Down Expand Up @@ -201,6 +205,36 @@ public void CanBindIConfigurationSectionWithDerivedOptionsSection()
Assert.Null(options.Section.Value);
}

[Fact]
public void CanBindAttributesIConfigurationSection()
{
var dic = new Dictionary<string, string>
{
{"Section:Integer", "-2"},
{"Section:Boolean", "TRUe"},
{"Section:Nested:Integer", "11"},
{"Section:Virtual", "Sup"},
{"Section:Named_Property", "Yo"},
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();

var options = config.Get<ConfigurationInterfaceOptions>();

var childOptions = options.Section.Get<DerivedOptions>();

Assert.True(childOptions.Boolean);
Assert.Equal(-2, childOptions.Integer);
Assert.Equal(11, childOptions.Nested.Integer);
Assert.Equal("Derived:Sup", childOptions.Virtual);
Assert.Equal("Yo", childOptions.NamedProperty);

Assert.Equal("Section", options.Section.Key);
Assert.Equal("Section", options.Section.Path);
Assert.Null(options.Section.Value);
}

[Fact]
public void EmptyStringIsNullable()
{
Expand Down