-
Notifications
You must be signed in to change notification settings - Fork 130
Add support for custom types in arrays and custom collections #201
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
51a4fca
Merge pull request #112 from serilog/dev
nblumhardt 008a61e
Merge pull request #146 from serilog/dev
tsimbalar fc0969f
Merge pull request #171 from serilog/dev
nblumhardt 5b67913
Dev version bump [skip ci]
nblumhardt 352cd85
Clarify role of Microsoft.Extensions.Configuration.Json; resolves #181
nblumhardt a0c5f45
Wrap a (scrolly) long line [skip ci]
nblumhardt 50fd5a9
Add support for custom types in arrays and custom collections
sungam3r 63efc49
remove parens
sungam3r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,39 +220,14 @@ internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMet | |
select new | ||
{ | ||
Name = argument.Key, | ||
Value = GetArgumentValue(argument) | ||
Value = GetArgumentValue(argument, _configurationAssemblies) | ||
}).ToDictionary(p => p.Name, p => p.Value) | ||
select new { Name = name, Args = callArgs })) | ||
.ToLookup(p => p.Name, p => p.Args); | ||
|
||
return result; | ||
|
||
IConfigurationArgumentValue GetArgumentValue(IConfigurationSection argumentSection) | ||
{ | ||
IConfigurationArgumentValue argumentValue; | ||
|
||
// Reject configurations where an element has both scalar and complex | ||
// values as a result of reading multiple configuration sources. | ||
if (argumentSection.Value != null && argumentSection.GetChildren().Any()) | ||
throw new InvalidOperationException( | ||
$"The value for the argument '{argumentSection.Path}' is assigned different value " + | ||
"types in more than one configuration source. Ensure all configurations consistently " + | ||
"use either a scalar (int, string, boolean) or a complex (array, section, list, " + | ||
"POCO, etc.) type for this argument value."); | ||
|
||
if (argumentSection.Value != null) | ||
{ | ||
argumentValue = new StringArgumentValue(argumentSection.Value); | ||
} | ||
else | ||
{ | ||
argumentValue = new ObjectArgumentValue(argumentSection, _configurationAssemblies); | ||
} | ||
|
||
return argumentValue; | ||
} | ||
|
||
string GetSectionName(IConfigurationSection s) | ||
static string GetSectionName(IConfigurationSection s) | ||
{ | ||
var name = s.GetSection("Name"); | ||
if (name.Value == null) | ||
|
@@ -262,6 +237,31 @@ string GetSectionName(IConfigurationSection s) | |
} | ||
} | ||
|
||
internal static IConfigurationArgumentValue GetArgumentValue(IConfigurationSection argumentSection, IReadOnlyCollection<Assembly> configurationAssemblies) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it began to be used in another place |
||
{ | ||
IConfigurationArgumentValue argumentValue; | ||
|
||
// Reject configurations where an element has both scalar and complex | ||
// values as a result of reading multiple configuration sources. | ||
if (argumentSection.Value != null && argumentSection.GetChildren().Any()) | ||
throw new InvalidOperationException( | ||
$"The value for the argument '{argumentSection.Path}' is assigned different value " + | ||
"types in more than one configuration source. Ensure all configurations consistently " + | ||
"use either a scalar (int, string, boolean) or a complex (array, section, list, " + | ||
"POCO, etc.) type for this argument value."); | ||
|
||
if (argumentSection.Value != null) | ||
{ | ||
argumentValue = new StringArgumentValue(argumentSection.Value); | ||
} | ||
else | ||
{ | ||
argumentValue = new ObjectArgumentValue(argumentSection, configurationAssemblies); | ||
} | ||
|
||
return argumentValue; | ||
} | ||
|
||
static IReadOnlyCollection<Assembly> LoadConfigurationAssemblies(IConfigurationSection section, AssemblyFinder assemblyFinder) | ||
{ | ||
var assemblies = new Dictionary<string, Assembly>(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Serilog.Configuration; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
using Serilog.Configuration; | ||
|
||
namespace Serilog.Settings.Configuration | ||
{ | ||
class ObjectArgumentValue : IConfigurationArgumentValue | ||
|
@@ -47,8 +47,72 @@ public object ConvertTo(Type toType, ResolutionContext resolutionContext) | |
} | ||
} | ||
|
||
// MS Config binding | ||
if (toType.IsArray) | ||
return CreateArray(); | ||
|
||
if (IsContainer(toType, out var elementType) && TryCreateContainer(out var result)) | ||
return result; | ||
|
||
// MS Config binding can work with a limited set of primitive types and collections | ||
return _section.Get(toType); | ||
|
||
object CreateArray() | ||
{ | ||
var elementType = toType.GetElementType(); | ||
var configurationElements = _section.GetChildren().ToArray(); | ||
var result = Array.CreateInstance(elementType, configurationElements.Length); | ||
for (int i = 0; i < configurationElements.Length; ++i) | ||
{ | ||
var argumentValue = ConfigurationReader.GetArgumentValue(configurationElements[i], _configurationAssemblies); | ||
var value = argumentValue.ConvertTo(elementType, resolutionContext); | ||
result.SetValue(value, i); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
bool TryCreateContainer(out object result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If false fallbacks to ms config binding implementation. |
||
{ | ||
result = null; | ||
|
||
if (toType.GetConstructor(Type.EmptyTypes) == null) | ||
return false; | ||
|
||
// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers#collection-initializers | ||
var addMethod = toType.GetMethods().FirstOrDefault(m => !m.IsStatic && m.Name == "Add" && m.GetParameters()?.Length == 1 && m.GetParameters()[0].ParameterType == elementType); | ||
if (addMethod == null) | ||
return false; | ||
|
||
var configurationElements = _section.GetChildren().ToArray(); | ||
result = Activator.CreateInstance(toType); | ||
|
||
for (int i = 0; i < configurationElements.Length; ++i) | ||
{ | ||
var argumentValue = ConfigurationReader.GetArgumentValue(configurationElements[i], _configurationAssemblies); | ||
var value = argumentValue.ConvertTo(elementType, resolutionContext); | ||
addMethod.Invoke(result, new object[] { value }); | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
private static bool IsContainer(Type type, out Type elementType) | ||
{ | ||
elementType = null; | ||
foreach (var iface in type.GetInterfaces()) | ||
{ | ||
if (iface.IsGenericType) | ||
{ | ||
if (iface.GetGenericTypeDefinition() == typeof(IEnumerable<>)) | ||
{ | ||
elementType = iface.GetGenericArguments()[0]; | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for the additional target framework here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this target because I saw references to it in the conditions.