Skip to content

Commit 560db07

Browse files
IgorFedchenkoAaronontheweb
authored andcommitted
Add config.ToString(bool useFallbackValues) overload to include fallback values (#162)
* Added verification red test * Added Config.ToString() overload to include fallback values
1 parent 0b05f64 commit 560db07

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Hocon.Configuration.Test/ConfigurationSpec.cs

+16
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,22 @@ public void CanUseFluentMultiLevelFallback()
260260
Assert.Equal(-1, config.GetInt("foo.bar.borkbork"));
261261
}
262262

263+
[Fact]
264+
public void ShouldSerializeFallbackValues()
265+
{
266+
var a = ConfigurationFactory.ParseString(@" akka : {
267+
some-key : value
268+
}");
269+
var b = ConfigurationFactory.ParseString(@"akka : {
270+
other-key : 42
271+
}");
272+
273+
var c = a.WithFallback(b);
274+
c.GetInt("akka.other-key").Should().Be(42, "Fallback value should exist as data");
275+
c.ToString().Should().NotContain("other-key", "Fallback values are ignored by default");
276+
c.ToString(useFallbackValues: true).Should().Contain("other-key", "Fallback values should be displayed when requested");
277+
}
278+
263279
[Fact]
264280
public void CanParseQuotedKeys()
265281
{

src/Hocon.Configuration/Config.cs

+16
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ public Config(HoconRoot source, Config fallback):base(source?.Value, source?.Sub
5151
Fallback = fallback;
5252
}
5353

54+
/// <summary>
55+
/// Returns string representation of <see cref="Config"/>, allowing to include fallback values
56+
/// </summary>
57+
/// <param name="useFallbackValues">If set to <c>true</c>, fallback values are included in the output</param>
58+
public string ToString(bool useFallbackValues)
59+
{
60+
if (!useFallbackValues)
61+
return base.ToString();
62+
63+
var config = this;
64+
while (config.Fallback != null)
65+
config = config.Fallback;
66+
67+
return config.ToString();
68+
}
69+
5470
/// <summary>
5571
/// Generates a deep clone of the current configuration.
5672
/// </summary>

0 commit comments

Comments
 (0)