Skip to content

Commit 1734274

Browse files
add default HOCON loading methods (#151) (#152)
* working on adding default HOCON loading methods (#151) Added ConfigurationFactory.FromFile method Added new HOCON loading order per 151 Made ConfigurationFactory.Load Obsolete * added specs * cleaned up Default() method * added default HOCON reading instructions to README
1 parent 320a57c commit 1734274

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ HOCON is significantly harder to specify and to parse than
4141
JSON. Think of it as moving the work from the person maintaining
4242
the config file to the computer program.
4343

44+
## Default HOCON Configuration Sources
45+
By default the HOCON library will look for HOCON configurations in the following locations whenever you call the `Hocon.Configuration.ConfigurationFactory.Default()` method:
46+
47+
1. [.NET Core / .NET Framework] An "app.conf" or an "app.hocon" file in the current working directory of the executable when it loads;
48+
2. [.NET Framework] - the `<hocon>` `ConfigurationSection` inside `App.config` or `Web.config`, which should also resolve #8 and #9
49+
3. [.NET Framework] - and a legacy option, to load the old `<akka>` HOCON section for backwards compatibility purposes with all users who have been using HOCON with Akka.NET.
50+
4451
## Definitions
4552

4653
- a _key_ is a string JSON would have to the left of `:` and a _value_ is

src/Hocon.Configuration.Test/ConfigurationSpec.cs

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ public void DeserializesHoconConfigurationFromNetConfigFile()
4242
}
4343
#endif
4444

45+
/// <summary>
46+
/// Should follow the load order rules specified in https://github.com/akkadotnet/HOCON/issues/151
47+
/// </summary>
48+
[Fact]
49+
public void CanLoadDefaultConfig()
50+
{
51+
var defaultConf = ConfigurationFactory.Default();
52+
defaultConf.Should().NotBe(ConfigurationFactory.Empty);
53+
defaultConf.HasPath("root.simple-string").Should().BeTrue();
54+
defaultConf.GetString("root.simple-string").Should().Be("Hello HOCON2");
55+
}
56+
4557
[Fact]
4658
public void CanMergeObjects()
4759
{

src/Hocon.Configuration.Test/Hocon.Configuration.Tests.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
<Reference Include="System.Configuration" />
2828
</ItemGroup>
2929

30+
<ItemGroup>
31+
<None Update="app.conf">
32+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
33+
</None>
34+
</ItemGroup>
35+
3036
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
3137
<Copy SourceFiles="App.config" DestinationFiles="$(OutDir)\testhost.dll.config" />
3238
</Target>

src/Hocon.Configuration.Test/app.conf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
root
2+
{
3+
simple-string = "Hello HOCON2"
4+
}

src/Hocon.Configuration/ConfigurationFactory.cs

+43-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,54 @@ 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+
var def = Load("hocon"); // new default
119+
if (def == null || def.IsEmpty)
120+
{
121+
return Load("akka"); // old Akka.NET-specific default
122+
}
123+
124+
}
125+
catch
126+
{
127+
128+
}
129+
130+
return Empty;
89131
}
90132

91133
/// <summary>

0 commit comments

Comments
 (0)