Skip to content

Fixed merge for substitutions #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -394,7 +395,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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to clone all child elements as well, to make deep copy of referenced subtree

}
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. <http://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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning clone of the references sub-tree, which is safe for modifications

}

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