Skip to content

Commit 92a3b07

Browse files
committed
working on adding default HOCON loading methods (akkadotnet#151)
Added ConfigurationFactory.FromFile method Added new HOCON loading order per 151 Made ConfigurationFactory.Load Obsolete
1 parent 526e6de commit 92a3b07

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/Hocon.Configuration/ConfigurationFactory.cs

+40-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Configuration;
1010
using System.Diagnostics;
1111
using System.IO;
12+
using System.Linq;
1213
using System.Reflection;
1314

1415
namespace Hocon
@@ -57,6 +58,7 @@ public static Config ParseString(string hocon)
5758
/// The configuration defined in the configuration file. If the section
5859
/// "akka" is not found, this returns an empty Config.
5960
/// </returns>
61+
[Obsolete("Call the ConfigurationFactory.Default method instead.")]
6062
public static Config Load()
6163
{
6264
return Load("akka");
@@ -78,14 +80,51 @@ public static Config Load(string sectionName)
7880

7981
return config;
8082
}
83+
84+
/// <summary>
85+
/// Parses a HOCON file from the filesystem.
86+
/// </summary>
87+
/// <param name="filePath">The path to the file.</param>
88+
/// <returns>A parsed HOCON configuration object.</returns>
89+
/// <throws>ConfigurationException, when the supplied filePath can't be found.</throws>
90+
public static Config FromFile(string filePath)
91+
{
92+
if (File.Exists(filePath))
93+
{
94+
return ParseString(File.ReadAllText(filePath));
95+
}
96+
97+
throw new ConfigurationException($"No HOCON file at {filePath} could be found.");
98+
}
99+
100+
public static readonly string[] DefaultHoconFilePaths = { "app.conf", "app.hocon" };
101+
81102
/// <summary>
82103
/// Retrieves the default configuration that Akka.NET uses
83104
/// when no configuration has been defined.
84105
/// </summary>
85106
/// <returns>The configuration that contains default values for all options.</returns>
86107
public static Config Default()
87108
{
88-
return FromResource("Default.conf");
109+
// attempt to load .hocon files first
110+
foreach (var path in DefaultHoconFilePaths.Where(x => File.Exists(x)))
111+
{
112+
return FromFile(path);
113+
}
114+
115+
// if we made it this far: no default HOCON files found. Check app.config
116+
try
117+
{
118+
return Load("hocon"); // new default
119+
return Load("akka"); // old Akka.NET-specific default
120+
121+
}
122+
catch
123+
{
124+
125+
}
126+
127+
return Empty;
89128
}
90129

91130
/// <summary>

0 commit comments

Comments
 (0)