Skip to content

Commit

Permalink
Fixes a bug in the ConfigFile class
Browse files Browse the repository at this point in the history
The basic `ConfigFile.Deserialize` method was incorrectly putting a `FileInfo` into the `GenericConfig` field
  • Loading branch information
atruskie committed Feb 17, 2018
1 parent 620b644 commit cc45832
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
17 changes: 12 additions & 5 deletions src/Acoustics.Shared/ConfigFile/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ public Config()

internal Config(TextReader streamReader, string configPath)
{
this.GenericConfig = Yaml.Deserialize<object>(streamReader);
this.ConfigPath = configPath;
this.Initialize(streamReader, configPath);
}

internal Config(object document, string configPath)
internal Config(string configPath)
{
this.GenericConfig = document;
this.ConfigPath = configPath;
using (var fileStream = File.OpenText(configPath))
{
this.Initialize(fileStream, configPath);
}
}

private delegate bool Convert<T>(string value, out T convertedValue);
Expand Down Expand Up @@ -144,6 +145,12 @@ private static Convert<T> GetEnumConverter<T>()
return (string value, out T convertedValue) => Enum.TryParse(value, true, out convertedValue);
}

private void Initialize(TextReader streamReader, string configPath)
{
this.GenericConfig = Yaml.Deserialize<object>(streamReader);
this.ConfigPath = configPath;
}

// Recursive!
private void VisitAll(object node, Dictionary<string, string> result, string prefix)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Acoustics.Shared/ConfigFile/ConfigFile.Profiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static bool TryGetProfile<TConfig>(TConfig configuration, string profileN
return false;
}

profile = new Config(mapping, configuration.ConfigPath);
profile = new Config() { GenericConfig = mapping, ConfigPath = configuration.ConfigPath };
return true;
}

Expand Down
24 changes: 16 additions & 8 deletions src/Acoustics.Shared/ConfigFile/ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ namespace Acoustics.Shared.ConfigFile
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;

using Contracts;
using Fasterflect;

using log4net;

using YamlDotNet.RepresentationModel;

using Zio;

public static partial class ConfigFile
Expand Down Expand Up @@ -53,18 +48,31 @@ public static string ConfigFolder

public static Config Deserialize(FileInfo file)
{
var config = new Config(file, file.FullName);
return Deserialize(file.FullName);
}

public static Config Deserialize(string path)
{
var config = new Config(path);
config.InvokeLoaded();
return config;
}

public static T Deserialize<T>(FileInfo file)
where T : Config
{
(object generic, T config) = Yaml.LoadAndDeserialize<T>(file);
return Deserialize<T>(file.FullName);
}

public static T Deserialize<T>(string path)
where T : Config
{
path = Path.GetFullPath(path);

(object generic, T config) = Yaml.LoadAndDeserialize<T>(path);

config.GenericConfig = generic;
config.ConfigPath = file.FullName;
config.ConfigPath = path;
config.InvokeLoaded();

return config;
Expand Down

0 comments on commit cc45832

Please sign in to comment.