From 04ba956ebed3e22690d71875e49d07d57bf35783 Mon Sep 17 00:00:00 2001 From: IgorFedchenko Date: Thu, 5 Dec 2019 00:04:00 +0300 Subject: [PATCH 1/4] Added failing test --- .../ConfigurationSpec.cs | 21 +++++++++++++++++++ .../Hocon.Configuration.Tests.csproj | 1 + 2 files changed, 22 insertions(+) diff --git a/src/Hocon.Configuration.Test/ConfigurationSpec.cs b/src/Hocon.Configuration.Test/ConfigurationSpec.cs index 4be1c520..5107f48c 100644 --- a/src/Hocon.Configuration.Test/ConfigurationSpec.cs +++ b/src/Hocon.Configuration.Test/ConfigurationSpec.cs @@ -8,6 +8,7 @@ using System; using System.Configuration; using System.Linq; +using FluentAssertions; using Xunit; using Xunit.Abstractions; @@ -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() diff --git a/src/Hocon.Configuration.Test/Hocon.Configuration.Tests.csproj b/src/Hocon.Configuration.Test/Hocon.Configuration.Tests.csproj index 6c9f27d8..709e1b5b 100644 --- a/src/Hocon.Configuration.Test/Hocon.Configuration.Tests.csproj +++ b/src/Hocon.Configuration.Test/Hocon.Configuration.Tests.csproj @@ -6,6 +6,7 @@ + From b6edf91b9020f74803ccebf52e051e22ba604bc9 Mon Sep 17 00:00:00 2001 From: IgorFedchenko Date: Thu, 5 Dec 2019 17:17:08 +0300 Subject: [PATCH 2/4] Make substitution to use deep copy + fixed deep clone for some types --- src/Hocon/Impl/HoconObject.cs | 2 +- src/Hocon/Impl/HoconValue.cs | 2 +- src/Hocon/Parser.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Hocon/Impl/HoconObject.cs b/src/Hocon/Impl/HoconObject.cs index 52a96d97..dd991af6 100644 --- a/src/Hocon/Impl/HoconObject.cs +++ b/src/Hocon/Impl/HoconObject.cs @@ -394,7 +394,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.Clone(clone) as HoconField); } return clone; } diff --git a/src/Hocon/Impl/HoconValue.cs b/src/Hocon/Impl/HoconValue.cs index 97e57c15..1dc47cee 100644 --- a/src/Hocon/Impl/HoconValue.cs +++ b/src/Hocon/Impl/HoconValue.cs @@ -706,7 +706,7 @@ public virtual IHoconElement Clone(IHoconElement newParent) var clone = new HoconValue(newParent); foreach (var element in this) { - clone.Add(element); + clone.Add(element.Clone(clone)); } return clone; } diff --git a/src/Hocon/Parser.cs b/src/Hocon/Parser.cs index c53969b6..686eaf2d 100644 --- a/src/Hocon/Parser.cs +++ b/src/Hocon/Parser.cs @@ -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) From b468e60d624e160232922703ad37effaae11613d Mon Sep 17 00:00:00 2001 From: IgorFedchenko Date: Wed, 11 Dec 2019 15:49:09 +0300 Subject: [PATCH 3/4] NRE fix --- src/Hocon/Parser.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Hocon/Parser.cs b/src/Hocon/Parser.cs index 686eaf2d..3a6c88fa 100644 --- a/src/Hocon/Parser.cs +++ b/src/Hocon/Parser.cs @@ -1,4 +1,4 @@ -//----------------------------------------------------------------------- +//----------------------------------------------------------------------- // // Copyright (C) 2009-2018 Lightbend Inc. // Copyright (C) 2013-2018 .NET Foundation @@ -184,7 +184,7 @@ private HoconValue ResolveSubstitution(HoconSubstitution sub) // third case, regular substitution _root.GetObject().TryGetValue(sub.Path, out var field); - return field.Clone(field.Parent) as HoconValue; + return field?.Clone(field.Parent) as HoconValue; } private bool IsValueCyclic(HoconField field, HoconSubstitution sub) From 81f8b24b9214bd7b6e9d08259fd3626e4070c314 Mon Sep 17 00:00:00 2001 From: IgorFedchenko Date: Wed, 11 Dec 2019 16:09:48 +0300 Subject: [PATCH 4/4] Fixed deep clone to take substitutions into account --- .../Extensions/HoconElementExtensions.cs | 19 +++++++++++++++++++ src/Hocon/Impl/HoconObject.cs | 3 ++- src/Hocon/Impl/HoconValue.cs | 3 ++- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/Hocon/Extensions/HoconElementExtensions.cs diff --git a/src/Hocon/Extensions/HoconElementExtensions.cs b/src/Hocon/Extensions/HoconElementExtensions.cs new file mode 100644 index 00000000..0aafb2d7 --- /dev/null +++ b/src/Hocon/Extensions/HoconElementExtensions.cs @@ -0,0 +1,19 @@ +namespace Hocon.Extensions +{ + /// + /// HoconElementExtensions + /// + public static class HoconElementExtensions + { + /// + /// Performs deep clone of the element's value. + /// This is generally the same as , but + /// for substitutions it returns the substitution itself since it's value will be closed + /// during resolution process. + /// + public static IHoconElement CloneValue(this IHoconElement hoconElement, IHoconElement newParent) + { + return hoconElement is HoconSubstitution ? hoconElement : hoconElement.Clone(newParent); + } + } +} \ No newline at end of file diff --git a/src/Hocon/Impl/HoconObject.cs b/src/Hocon/Impl/HoconObject.cs index dd991af6..663d7faf 100644 --- a/src/Hocon/Impl/HoconObject.cs +++ b/src/Hocon/Impl/HoconObject.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Runtime.InteropServices.ComTypes; using System.Text; +using Hocon.Extensions; namespace Hocon { @@ -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(clone) as HoconField); + clone.SetField(kvp.Key, kvp.Value.CloneValue(clone) as HoconField); } return clone; } diff --git a/src/Hocon/Impl/HoconValue.cs b/src/Hocon/Impl/HoconValue.cs index 1dc47cee..ddfa0bf4 100644 --- a/src/Hocon/Impl/HoconValue.cs +++ b/src/Hocon/Impl/HoconValue.cs @@ -13,6 +13,7 @@ using System.Text; using System.Text.RegularExpressions; using System.Threading; +using Hocon.Extensions; namespace Hocon { @@ -706,7 +707,7 @@ public virtual IHoconElement Clone(IHoconElement newParent) var clone = new HoconValue(newParent); foreach (var element in this) { - clone.Add(element.Clone(clone)); + clone.Add(element.CloneValue(clone)); } return clone; }