Skip to content

Commit 37bacb8

Browse files
IgorFedchenkoAaronontheweb
authored andcommitted
Fixed merge for substitutions (#138)
* Added failing test * Make substitution to use deep copy + fixed deep clone for some types * NRE fix * Fixed deep clone to take substitutions into account
1 parent b9eb927 commit 37bacb8

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed

src/Hocon.Configuration.Test/ConfigurationSpec.cs

+21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Configuration;
1010
using System.Linq;
11+
using FluentAssertions;
1112
using Xunit;
1213
using Xunit.Abstractions;
1314

@@ -273,6 +274,26 @@ public void CanEnumerateQuotedKeys()
273274

274275
Assert.Equal("some quoted, key", enumerable.Select(kvp => kvp.Key).First());
275276
}
277+
278+
[Fact]
279+
public void CanSubstituteArrayCorrectly()
280+
{
281+
var hocon = @"
282+
c: {
283+
q: {
284+
a: [2, 5]
285+
}
286+
}
287+
c: {
288+
m: ${c.q} {a: [6]}
289+
}
290+
";
291+
var config = ConfigurationFactory.ParseString(hocon);
292+
var unchanged = config.GetIntList("c.q.a");
293+
unchanged.Should().Equal(new [] { 2, 5 });
294+
var changed = config.GetIntList("c.m.a");
295+
changed.Should().Equal(new [] { 6 });
296+
}
276297

277298
[Fact]
278299
public void CanParseSerializersAndBindings()

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

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="FluentAssertions" Version="5.9.0" />
910
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
1011
<PackageReference Include="xunit" Version="$(XunitVersion)" />
1112
<DotNetCliToolReference Include="dotnet-xunit" Version="$(XunitCliVersion)" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Hocon.Extensions
2+
{
3+
/// <summary>
4+
/// HoconElementExtensions
5+
/// </summary>
6+
public static class HoconElementExtensions
7+
{
8+
/// <summary>
9+
/// Performs deep clone of the element's value.
10+
/// This is generally the same as <see cref="IHoconElement.Clone"/>, but
11+
/// for substitutions it returns the substitution itself since it's value will be closed
12+
/// during resolution process.
13+
/// </summary>
14+
public static IHoconElement CloneValue(this IHoconElement hoconElement, IHoconElement newParent)
15+
{
16+
return hoconElement is HoconSubstitution ? hoconElement : hoconElement.Clone(newParent);
17+
}
18+
}
19+
}

src/Hocon/Impl/HoconObject.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Linq;
1212
using System.Runtime.InteropServices.ComTypes;
1313
using System.Text;
14+
using Hocon.Extensions;
1415

1516
namespace Hocon
1617
{
@@ -400,7 +401,7 @@ public IHoconElement Clone(IHoconElement newParent)
400401
var clone = new HoconObject(newParent);
401402
foreach (var kvp in this)
402403
{
403-
clone.SetField(kvp.Key, kvp.Value);
404+
clone.SetField(kvp.Key, kvp.Value.CloneValue(clone) as HoconField);
404405
}
405406
return clone;
406407
}

src/Hocon/Impl/HoconValue.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Text;
1414
using System.Text.RegularExpressions;
1515
using System.Threading;
16+
using Hocon.Extensions;
1617

1718
namespace Hocon
1819
{
@@ -706,7 +707,7 @@ public virtual IHoconElement Clone(IHoconElement newParent)
706707
var clone = new HoconValue(newParent);
707708
foreach (var element in this)
708709
{
709-
clone.Add(element);
710+
clone.Add(element.CloneValue(clone));
710711
}
711712
return clone;
712713
}

src/Hocon/Parser.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//-----------------------------------------------------------------------
1+
//-----------------------------------------------------------------------
22
// <copyright file="Parser.cs" company="Hocon Project">
33
// Copyright (C) 2009-2018 Lightbend Inc. <http://www.lightbend.com>
44
// Copyright (C) 2013-2018 .NET Foundation <https://github.com/akkadotnet/hocon>
@@ -184,7 +184,7 @@ private HoconValue ResolveSubstitution(HoconSubstitution sub)
184184

185185
// third case, regular substitution
186186
_root.GetObject().TryGetValue(sub.Path, out var field);
187-
return field;
187+
return field?.Clone(field.Parent) as HoconValue;
188188
}
189189

190190
private bool IsValueCyclic(HoconField field, HoconSubstitution sub)

0 commit comments

Comments
 (0)