Skip to content

Commit

Permalink
Fixed merge for substitutions (#138)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
IgorFedchenko authored and Aaronontheweb committed Dec 27, 2019
1 parent 6d14466 commit c386b2e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/Hocon.Configuration.Test/ConfigurationSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Configuration;
using System.Linq;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -273,6 +274,26 @@ public void CanEnumerateQuotedKeys()

Assert.Equal("some quoted, key", enumerable.Select(kvp => kvp.Key).First());
}

[Fact]
public void CanSubstituteArrayCorrectly()
{
var hocon = @"
c: {
q: {
a: [2, 5]
}
}
c: {
m: ${c.q} {a: [6]}
}
";
var config = ConfigurationFactory.ParseString(hocon);
var unchanged = config.GetIntList("c.q.a");
unchanged.Should().Equal(new [] { 2, 5 });
var changed = config.GetIntList("c.m.a");
changed.Should().Equal(new [] { 6 });
}

[Fact]
public void CanParseSerializersAndBindings()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
<DotNetCliToolReference Include="dotnet-xunit" Version="$(XunitCliVersion)" />
Expand Down
19 changes: 19 additions & 0 deletions src/Hocon/Extensions/HoconElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Hocon.Extensions
{
/// <summary>
/// HoconElementExtensions
/// </summary>
public static class HoconElementExtensions
{
/// <summary>
/// Performs deep clone of the element's value.
/// This is generally the same as <see cref="IHoconElement.Clone"/>, but
/// for substitutions it returns the substitution itself since it's value will be closed
/// during resolution process.
/// </summary>
public static IHoconElement CloneValue(this IHoconElement hoconElement, IHoconElement newParent)
{
return hoconElement is HoconSubstitution ? hoconElement : hoconElement.Clone(newParent);
}
}
}
3 changes: 2 additions & 1 deletion src/Hocon/Impl/HoconObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using Hocon.Extensions;

namespace Hocon
{
Expand Down Expand Up @@ -400,7 +401,7 @@ public IHoconElement Clone(IHoconElement newParent)
var clone = new HoconObject(newParent);
foreach (var kvp in this)
{
clone.SetField(kvp.Key, kvp.Value);
clone.SetField(kvp.Key, kvp.Value.CloneValue(clone) as HoconField);
}
return clone;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Hocon/Impl/HoconValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Hocon.Extensions;

namespace Hocon
{
Expand Down Expand Up @@ -706,7 +707,7 @@ public virtual IHoconElement Clone(IHoconElement newParent)
var clone = new HoconValue(newParent);
foreach (var element in this)
{
clone.Add(element);
clone.Add(element.CloneValue(clone));
}
return clone;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Hocon/Parser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// <copyright file="Parser.cs" company="Hocon Project">
// Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
// Copyright (C) 2013-2018 .NET Foundation <https://github.com/akkadotnet/hocon>
Expand Down Expand Up @@ -184,7 +184,7 @@ private HoconValue ResolveSubstitution(HoconSubstitution sub)

// third case, regular substitution
_root.GetObject().TryGetValue(sub.Path, out var field);
return field;
return field?.Clone(field.Parent) as HoconValue;
}

private bool IsValueCyclic(HoconField field, HoconSubstitution sub)
Expand Down

0 comments on commit c386b2e

Please sign in to comment.