forked from serilog/serilog-settings-configuration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectArgumentValue.cs
362 lines (322 loc) · 13.5 KB
/
ObjectArgumentValue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Serilog.Configuration;
using Serilog.Debugging;
namespace Serilog.Settings.Configuration;
class ObjectArgumentValue : ConfigurationArgumentValue
{
readonly IConfigurationSection _section;
readonly IReadOnlyCollection<Assembly> _configurationAssemblies;
public ObjectArgumentValue(IConfigurationSection section, IReadOnlyCollection<Assembly> configurationAssemblies)
{
_section = section ?? throw new ArgumentNullException(nameof(section));
// used by nested logger configurations to feed a new pass by ConfigurationReader
_configurationAssemblies = configurationAssemblies ?? throw new ArgumentNullException(nameof(configurationAssemblies));
}
public override object? ConvertTo(Type toType, ResolutionContext resolutionContext)
{
// return the entire section for internal processing
if (toType == typeof(IConfigurationSection)) return _section;
// process a nested configuration to populate an Action<> logger/sink config parameter?
var typeInfo = toType.GetTypeInfo();
if (typeInfo.IsGenericType &&
typeInfo.GetGenericTypeDefinition() is { } genericType && genericType == typeof(Action<>))
{
var configType = typeInfo.GenericTypeArguments[0];
IConfigurationReader configReader = new ConfigurationReader(_section, _configurationAssemblies, resolutionContext);
return configType switch
{
_ when configType == typeof(LoggerConfiguration) => new Action<LoggerConfiguration>(configReader.Configure),
_ when configType == typeof(LoggerSinkConfiguration) => new Action<LoggerSinkConfiguration>(configReader.ApplySinks),
_ when configType == typeof(LoggerEnrichmentConfiguration) => new Action<LoggerEnrichmentConfiguration>(configReader.ApplyEnrichment),
_ => throw new ArgumentException($"Configuration resolution for `Action<{configType.Name}>` parameter type at the path `{_section.Path}` is not implemented.")
};
}
if (toType.IsArray)
return CreateArray();
// Only try to call ctor when type is explicitly specified in _section
if (TryCallCtorExplicit(_section, resolutionContext, out var ctorResult))
return ctorResult;
if (IsContainer(toType, out var elementType) && TryCreateContainer(out var container))
return container;
// Without a type explicitly specified, attempt to call ctor of toType
if (TryCallCtorImplicit(_section, toType, resolutionContext, out ctorResult))
return ctorResult;
// MS Config binding can work with a limited set of primitive types and collections
return _section.Get(toType);
object CreateArray()
{
var arrayElementType = toType.GetElementType()!;
var configurationElements = _section.GetChildren().ToArray();
var array = Array.CreateInstance(arrayElementType, configurationElements.Length);
for (var i = 0; i < configurationElements.Length; ++i)
{
var argumentValue = FromSection(configurationElements[i], _configurationAssemblies);
var value = argumentValue.ConvertTo(arrayElementType, resolutionContext);
array.SetValue(value, i);
}
return array;
}
bool TryCreateContainer([NotNullWhen(true)] out object? result)
{
result = null;
if (IsConstructableDictionary(toType, elementType, out var concreteType, out var keyType, out var valueType, out var addMethod))
{
result = Activator.CreateInstance(concreteType) ?? throw new InvalidOperationException($"Activator.CreateInstance returned null for {concreteType}");
foreach (var section in _section.GetChildren())
{
var argumentValue = FromSection(section, _configurationAssemblies);
var key = new StringArgumentValue(section.Key).ConvertTo(keyType, resolutionContext);
var value = argumentValue.ConvertTo(valueType, resolutionContext);
addMethod.Invoke(result, [key, value]);
}
return true;
}
if (IsConstructableContainer(toType, elementType, out concreteType, out addMethod))
{
result = Activator.CreateInstance(concreteType) ?? throw new InvalidOperationException($"Activator.CreateInstance returned null for {concreteType}");
foreach (var section in _section.GetChildren())
{
var argumentValue = FromSection(section, _configurationAssemblies);
var value = argumentValue.ConvertTo(elementType, resolutionContext);
addMethod.Invoke(result, new[] { value });
}
return true;
}
return false;
}
}
bool TryCallCtorExplicit(
IConfigurationSection section, ResolutionContext resolutionContext, [NotNullWhen(true)] out object? value)
{
var typeDirective = section.GetValue<string>("$type") switch
{
not null => "$type",
null => section.GetValue<string>("type") switch
{
not null => "type",
null => null,
},
};
var type = typeDirective switch
{
not null => Type.GetType(section.GetValue<string>(typeDirective)!, throwOnError: false),
null => null,
};
if (type is null or { IsAbstract: true })
{
value = null;
return false;
}
else
{
var suppliedArguments = section.GetChildren().Where(s => s.Key != typeDirective)
.ToDictionary(s => s.Key, StringComparer.OrdinalIgnoreCase);
return TryCallCtor(type, suppliedArguments, resolutionContext, out value);
}
}
bool TryCallCtorImplicit(
IConfigurationSection section, Type parameterType, ResolutionContext resolutionContext, out object? value)
{
var suppliedArguments = section.GetChildren()
.ToDictionary(s => s.Key, StringComparer.OrdinalIgnoreCase);
return TryCallCtor(parameterType, suppliedArguments, resolutionContext, out value);
}
bool TryCallCtor(Type type, Dictionary<string, IConfigurationSection> suppliedArguments, ResolutionContext resolutionContext, [NotNullWhen(true)] out object? value)
{
var binding = type.GetConstructors()
.Select(ci =>
{
var args = new List<object?>();
var matches = 0;
var stringMatches = 0;
var suppliedNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var p in ci.GetParameters())
{
if (suppliedArguments.TryGetValue(p.Name ?? "", out var argValue))
{
args.Add(argValue);
matches += 1;
if (p.ParameterType == typeof(string))
{
stringMatches += 1;
}
if (p.Name != null)
{
suppliedNames.Add(p.Name);
}
}
else
{
if (p.HasDefaultValue)
{
args.Add(p.DefaultValue);
}
else
{
return new
{
ci,
args,
isCallable = false,
matches,
stringMatches,
usedArguments = suppliedNames
};
}
}
}
return new
{
ci,
args,
isCallable = true,
matches,
stringMatches,
usedArguments = suppliedNames
};
})
.Where(binding => binding.isCallable)
.OrderByDescending(binding => binding.matches)
.ThenByDescending(binding => binding.stringMatches)
.ThenBy(binding => binding.args.Count)
.FirstOrDefault();
if (binding == null)
{
value = null;
return false;
}
for (var i = 0; i < binding.ci.GetParameters().Length; ++i)
{
if (binding.args[i] is IConfigurationSection section)
{
var argumentValue = FromSection(section, _configurationAssemblies);
binding.args[i] = argumentValue.ConvertTo(binding.ci.GetParameters()[i].ParameterType, resolutionContext);
}
}
value = binding.ci.Invoke(binding.args.ToArray());
foreach (var pi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if (!binding.usedArguments.Contains(pi.Name) &&
suppliedArguments.TryGetValue(pi.Name, out var section)
&& pi.CanWrite &&
// This avoids trying to call esoteric indexers and so on.
pi.GetSetMethod(false)?.GetParameters().Length == 1)
{
var propertyValue = FromSection(section, _configurationAssemblies);
try
{
pi.SetValue(value, propertyValue.ConvertTo(pi.PropertyType, resolutionContext));
}
catch (Exception ex)
{
SelfLog.WriteLine($"Serilog.Settings.Configuration: Property setter on {type} failed: {ex}");
}
}
}
return true;
}
static bool IsContainer(Type type, [NotNullWhen(true)] out Type? elementType)
{
elementType = null;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
elementType = type.GetGenericArguments()[0];
return true;
}
foreach (var iface in type.GetInterfaces())
{
if (iface.IsGenericType)
{
if (iface.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
elementType = iface.GetGenericArguments()[0];
return true;
}
}
}
return false;
}
static bool IsConstructableDictionary(Type type, Type elementType, [NotNullWhen(true)] out Type? concreteType, [NotNullWhen(true)] out Type? keyType, [NotNullWhen(true)] out Type? valueType, [NotNullWhen(true)] out MethodInfo? addMethod)
{
concreteType = null;
keyType = null;
valueType = null;
addMethod = null;
if (!elementType.IsGenericType || elementType.GetGenericTypeDefinition() != typeof(KeyValuePair<,>))
{
return false;
}
var argumentTypes = elementType.GetGenericArguments();
keyType = argumentTypes[0];
valueType = argumentTypes[1];
if (type.IsAbstract)
{
concreteType = typeof(Dictionary<,>).MakeGenericType(argumentTypes);
if (!type.IsAssignableFrom(concreteType))
{
return false;
}
}
else
{
concreteType = type;
}
if (concreteType.GetConstructor(Type.EmptyTypes) == null)
{
return false;
}
foreach (var method in concreteType.GetMethods())
{
if (method is { IsStatic: false, Name: "Add" })
{
var parameters = method.GetParameters();
if (parameters.Length == 2 && parameters[0].ParameterType == keyType && parameters[1].ParameterType == valueType)
{
addMethod = method;
return true;
}
}
}
return false;
}
static bool IsConstructableContainer(Type type, Type elementType, [NotNullWhen(true)] out Type? concreteType, [NotNullWhen(true)] out MethodInfo? addMethod)
{
addMethod = null;
if (type.IsAbstract)
{
concreteType = typeof(List<>).MakeGenericType(elementType);
if (!type.IsAssignableFrom(concreteType))
{
concreteType = typeof(HashSet<>).MakeGenericType(elementType);
if (!type.IsAssignableFrom(concreteType))
{
concreteType = null;
return false;
}
}
}
else
{
concreteType = type;
}
if (concreteType.GetConstructor(Type.EmptyTypes) == null)
{
return false;
}
foreach (var method in concreteType.GetMethods())
{
if (method is { IsStatic: false, Name: "Add" })
{
var parameters = method.GetParameters();
if (parameters.Length == 1 && parameters[0].ParameterType == elementType)
{
addMethod = method;
return true;
}
}
}
return false;
}
}