Skip to content

Commit 48888a2

Browse files
IgorFedchenkoAaronontheweb
authored andcommitted
Fix arrays merge (#139)
* Added failing test * Updated array concatenation implementation
1 parent 37bacb8 commit 48888a2

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/Hocon.Tests/DuplicateKeysAndObjectMerging.cs

+22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Linq;
1111
using System.Text;
1212
using System.Threading.Tasks;
13+
using FluentAssertions;
1314
using Xunit;
1415
using Xunit.Abstractions;
1516

@@ -147,6 +148,27 @@ public void ObjectCanMixBraceAndDotSyntax()
147148
Assert.Equal(32, config.GetInt("foo.z"));
148149
}
149150

151+
// Fix for https://github.com/akkadotnet/HOCON/issues/137
152+
[Fact]
153+
public void ObjectsWithArraysShouldMergeCorrectly()
154+
{
155+
var hocon = @"
156+
c: {
157+
a: [2, 5]
158+
a: [6]
159+
}
160+
";
161+
var hocon2 = @"
162+
c: {
163+
a: [2, 5] [6]
164+
}
165+
";
166+
var config = Parser.Parse(hocon);
167+
config.GetIntList("c.a").Should().Equal(new [] { 6 });
168+
config = Parser.Parse(hocon2);
169+
config.GetIntList("c.a").Should().Equal(new [] { 2, 5, 6 });
170+
}
171+
150172
[Fact]
151173
public void CanMixObjectMergeAndSubstitutions_Issue92()
152174
{

src/Hocon.Tests/Hocon.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15+
<PackageReference Include="FluentAssertions" Version="5.9.0" />
1516
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
1617
<PackageReference Include="xunit" Version="$(XunitVersion)" />
1718
<DotNetCliToolReference Include="dotnet-xunit" Version="$(XunitCliVersion)" />

src/Hocon/Parser.cs

+18-1
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,11 @@ private HoconValue ParseValue(IHoconElement owner)
690690
case TokenType.StartOfArray:
691691
if (value == null)
692692
value = GetHoconValueFromParentElement(owner, _tokens.Current.Type);
693+
694+
// If this array is already initialized, we are going to overwrite it
695+
if (value.Type == HoconType.Array && value.Count > 0)
696+
value.Clear();
697+
693698
value.Add(ParseArray(value));
694699
break;
695700

@@ -902,7 +907,19 @@ private HoconArray ParseArray(IHoconElement owner)
902907

903908
case TokenType.EndOfArray:
904909
valueWasParsed = false;
905-
parsing = false;
910+
911+
// If there is a next array on the same like - let's move to it's first element
912+
if (_tokens.ForwardMatch(TokenType.StartOfArray))
913+
{
914+
_tokens.ToNextSignificant();
915+
_tokens.Next();
916+
}
917+
else
918+
{
919+
// Otherwise, array value is fully loaded and nothing to do here
920+
parsing = false;
921+
}
922+
906923
break;
907924

908925
case TokenType.Comment:

0 commit comments

Comments
 (0)