Skip to content

Commit 3431aa1

Browse files
committed
Made methods of InstanceCreator virtual
1 parent b9e918d commit 3431aa1

File tree

5 files changed

+84
-69
lines changed

5 files changed

+84
-69
lines changed

src/Thinktecture.Configuration.JsonFile/Configuration/JsonFileConfigurationLoader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public class JsonFileConfigurationLoader : IConfigurationLoader<JToken, JToken>
3333
/// <param name="jsonSerializerSettingsProvider">Provides <see cref="JsonSerializerSettings"/> for deserialization of file content to <see cref="JToken"/>.</param>
3434
/// <param name="jsonConfigurationProviderFactory">A factory for creation of <see cref="IConfigurationProvider{TRawDataIn,TRawDataOut}"/>.</param>
3535
public JsonFileConfigurationLoader([NotNull] IFile file, [NotNull] IJsonTokenConverter tokenConverter, [NotNull] string[] filePaths, [CanBeNull] IEncoding encoding = null,
36-
[CanBeNull] Func<JsonSerializerSettings> jsonSerializerSettingsProvider = null, [CanBeNull] Func<JToken[], IJsonTokenConverter, IConfigurationProvider<JToken, JToken>> jsonConfigurationProviderFactory = null)
36+
[CanBeNull] Func<JsonSerializerSettings> jsonSerializerSettingsProvider = null,
37+
[CanBeNull] Func<JToken[], IJsonTokenConverter, IConfigurationProvider<JToken, JToken>> jsonConfigurationProviderFactory = null)
3738
{
3839
if (filePaths == null)
3940
throw new ArgumentNullException(nameof(filePaths));

src/Thinktecture.Extensions.Configuration/Configuration/InstanceCreator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected InstanceCreator([NotNull] CultureInfo culture)
2929
public abstract IConversionResult Create(Type type);
3030

3131
/// <inheritdoc />
32-
public IConversionResult Create(Type type, string value)
32+
public virtual IConversionResult Create(Type type, string value)
3333
{
3434
if (type == null)
3535
throw new ArgumentNullException(nameof(type));
@@ -55,7 +55,7 @@ public IConversionResult Create(Type type, string value)
5555
}
5656

5757
/// <inheritdoc />
58-
public Array CreateArray(Type elementType, int length)
58+
public virtual Array CreateArray(Type elementType, int length)
5959
{
6060
if (elementType == null)
6161
throw new ArgumentNullException(nameof(elementType));

src/Thinktecture.Extensions.Configuration/Configuration/MicrosoftConfigurationConverter.cs

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,7 @@ private IConversionResult CreateAndPopulate([NotNull] Type type, [NotNull] IConf
9393
return new ConversionResult(null);
9494

9595
if (type.IsArray)
96-
{
97-
var elementType = type.GetElementType();
98-
99-
if (instance.IsCreated)
100-
{
101-
var currentArraySize = (instance.Value as Array)?.Length;
102-
103-
if (currentArraySize > 0)
104-
{
105-
_logger.LogWarning(@"One of the parent configuration objects has a property of type {type} that contains {size} elements before deserializion.
106-
This array along with its elements are going to be discarded. Please make check that no memory leaks occur. Configuration path: {path}", $"{elementType.Name}[]", currentArraySize, (config as IConfigurationSection)?.Path);
107-
}
108-
}
109-
110-
if (!hasChildConfigs && configValue?.Length == 0)
111-
return new ConversionResult(null);
112-
113-
return CreateAndPopulateArray(elementType, config);
114-
}
96+
return CreateAndPopulateArray(type, config, instance);
11597

11698
if (IsComplexType(type, config))
11799
{
@@ -130,6 +112,77 @@ private IConversionResult CreateAndPopulate([NotNull] Type type, [NotNull] IConf
130112
return ConversionResult.Invalid;
131113
}
132114

115+
[NotNull]
116+
private IConversionResult CreateAndPopulateArray([NotNull] Type type, [NotNull] IConfiguration config, [NotNull] IConversionInstance instance)
117+
{
118+
if (type == null)
119+
throw new ArgumentNullException(nameof(type));
120+
if (config == null)
121+
throw new ArgumentNullException(nameof(config));
122+
if (instance == null)
123+
throw new ArgumentNullException(nameof(instance));
124+
125+
var elementType = type.GetElementType();
126+
127+
if (instance.IsCreated)
128+
{
129+
var currentArraySize = (instance.Value as Array)?.Length;
130+
131+
if (currentArraySize > 0)
132+
{
133+
_logger.LogWarning(@"One of the parent configuration objects has a property of type {type} that contains {size} elements before deserializion.
134+
This array along with its elements are going to be discarded. Please make check that no memory leaks occur. Configuration path: {path}", $"{elementType.Name}[]", currentArraySize, (config as IConfigurationSection)?.Path);
135+
}
136+
}
137+
138+
var hasChildConfigs = config.GetChildren().Any();
139+
var configValue = (config as IConfigurationSection)?.Value;
140+
141+
if (!hasChildConfigs && configValue?.Length == 0)
142+
return new ConversionResult(null);
143+
144+
return CreateAndPopulateArray(elementType, config);
145+
}
146+
147+
[NotNull]
148+
private IConversionResult CreateAndPopulateArray([NotNull] Type elementType, [NotNull] IConfiguration config)
149+
{
150+
if (elementType == null)
151+
throw new ArgumentNullException(nameof(elementType));
152+
if (config == null)
153+
throw new ArgumentNullException(nameof(config));
154+
155+
var children = config.GetChildren()
156+
.Select(c =>
157+
{
158+
if (c.Key != null && Int32.TryParse(c.Key, out var index))
159+
return new { Index = index, Configuration = c };
160+
161+
_logger.LogWarning("The index of the collection of type {type} is not an integer. Key: {key}, path: {path}", elementType.FullName, c.Key, c.Path);
162+
return null;
163+
})
164+
.Where(i => i != null)
165+
.ToArray();
166+
167+
if (children.Length == 0 && (config as IConfigurationSection)?.Value == null)
168+
return new ConversionResult(null);
169+
170+
var array = _instanceCreator.CreateArray(elementType, children.Length == 0 ? 0 : children.Max(c => c.Index) + 1);
171+
172+
if (array == null)
173+
throw new ConfigurationSerializationException($"Instance creator returned null instead of an array of type {elementType.Name}");
174+
175+
foreach (var child in children)
176+
{
177+
var itemResult = CreateAndPopulate(elementType, child.Configuration, ConversionInstance.Empty);
178+
179+
if (itemResult.IsValid)
180+
array.SetValue(itemResult.Value, child.Index);
181+
}
182+
183+
return new ConversionResult(array);
184+
}
185+
133186
private static bool IsComplexType([NotNull] Type type, [NotNull] IConfiguration config)
134187
{
135188
var typeInfo = type.GetTypeInfo();
@@ -420,46 +473,6 @@ private void PopulateDictionary([NotNull] Type keyType, [NotNull] Type valueType
420473
}
421474
}
422475

423-
[NotNull]
424-
private IConversionResult CreateAndPopulateArray([NotNull] Type elementType, [NotNull] IConfiguration config)
425-
{
426-
if (elementType == null)
427-
throw new ArgumentNullException(nameof(elementType));
428-
if (config == null)
429-
throw new ArgumentNullException(nameof(config));
430-
431-
var children = config.GetChildren()
432-
.Select(c =>
433-
{
434-
if (c.Key == null || !Int32.TryParse(c.Key, out var index))
435-
{
436-
_logger.LogWarning("The index of the collection of type {type} is not an integer. Key: {key}, path: {path}", elementType.FullName, c.Key, c.Path);
437-
return null;
438-
}
439-
440-
return new { Index = index, Configuration = c };
441-
})
442-
.Where(i => i != null)
443-
.ToArray();
444-
445-
if (children.Length == 0 && (config as IConfigurationSection)?.Value == null)
446-
return new ConversionResult(null);
447-
448-
var array = _instanceCreator.CreateArray(elementType, children.Length == 0 ? 0 : children.Max(c => c.Index) + 1);
449-
450-
if (array == null)
451-
throw new ConfigurationSerializationException($"Instance creator returned null instead of an array of type {elementType.Name}");
452-
453-
foreach (var child in children)
454-
{
455-
var itemResult = CreateAndPopulate(elementType, child.Configuration, ConversionInstance.Empty);
456-
if (itemResult.IsValid)
457-
array.SetValue(itemResult.Value, child.Index);
458-
}
459-
460-
return new ConversionResult(array);
461-
}
462-
463476
[NotNull]
464477
private IConversionResult ConvertFromString([NotNull] Type type, [CanBeNull] string value)
465478
{

src/Thinktecture.Extensions.Configuration/Configuration/MicrosoftConfigurationLoader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class MicrosoftConfigurationLoader : IConfigurationLoader<IConfiguration,
1313
{
1414
[NotNull]
1515
private readonly Func<IConfiguration, MicrosoftConfigurationProvider> _providerFactory;
16+
1617
[NotNull]
1718
private readonly IConfiguration _configuration;
1819

@@ -22,7 +23,7 @@ public class MicrosoftConfigurationLoader : IConfigurationLoader<IConfiguration,
2223
/// <param name="configuration">The source to be used during deserialization of the configurations.</param>
2324
/// <param name="providerFactory">Factory for creation of <see cref="IMicrosoftConfigurationConverter"/>.</param>
2425
public MicrosoftConfigurationLoader([NotNull] IConfiguration configuration,
25-
[NotNull] Func<IConfiguration, MicrosoftConfigurationProvider> providerFactory)
26+
[NotNull] Func<IConfiguration, MicrosoftConfigurationProvider> providerFactory)
2627
{
2728
_providerFactory = providerFactory ?? throw new ArgumentNullException(nameof(providerFactory));
2829
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));

test/Thinktecture.Configuration.JsonFile.Autofac.Tests/Configuration/JsonTokenConverterTests/Convert.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ public void Should_set_inner_property_to_null_if_override_property_is_null()
302302
public void Should_ignore_null_values()
303303
{
304304
var tokens = GetTokens(new
305-
{
306-
InnerConfiguration = new { InnerConfiguration = new ConfigurationWithDefaultCtor() }
307-
},
305+
{
306+
InnerConfiguration = new { InnerConfiguration = new ConfigurationWithDefaultCtor() }
307+
},
308308
null,
309309
new
310310
{
@@ -321,9 +321,9 @@ public void Should_ignore_null_values()
321321
public void Should_start_deserializing_after_last_nulltoken()
322322
{
323323
var tokens = GetTokens(new
324-
{
325-
InnerConfiguration = new { InnerConfiguration = new ConfigurationWithDefaultCtor() }
326-
},
324+
{
325+
InnerConfiguration = new { InnerConfiguration = new ConfigurationWithDefaultCtor() }
326+
},
327327
JToken.Parse("null"),
328328
new
329329
{

0 commit comments

Comments
 (0)