Skip to content

Commit 9f1e06e

Browse files
committed
Fix inlining detection (Fixes #99)
1 parent 1fbee77 commit 9f1e06e

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/Tomlyn.Tests/SerializationTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,22 @@ public void TestCrlfInMultilineString()
2626

2727
Assert.AreEqual("property = '''string\r\nwith\r\nnewlines'''" + Environment.NewLine, Toml.FromModel(model));
2828
}
29+
30+
[Test]
31+
public void TestArrayWithPrimitives()
32+
{
33+
var model = new TomlTable()
34+
{
35+
["mixed-array"] = new TomlArray()
36+
{
37+
new TomlTable() { ["a"] = 1 },
38+
2, // If instead the second or final item in the array was the table, it works as expected.
39+
3
40+
}
41+
};
42+
43+
var result = Toml.FromModel(model).ReplaceLineEndings("\n").Trim();
44+
Console.WriteLine(result);
45+
Assert.AreEqual("mixed-array = [{a = 1}, 2, 3]", result);
46+
}
2947
}

src/Tomlyn/Model/ModelToTomlTransform.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,24 +437,30 @@ private bool IsRequiringInline(ListDynamicAccessor accessor, object value, int p
437437
// This is only working for default TomlTableArray model
438438
if (value is TomlTableArray) return false;
439439

440+
bool inlining = false;
441+
440442
foreach (var element in accessor.GetElements(value))
441443
{
442444
if (element is null) continue; // TODO: should this log an error?
443445
var elementAccessor = _context.GetAccessor(element.GetType());
444446

445-
if (elementAccessor is PrimitiveDynamicAccessor) return true;
447+
if (elementAccessor is PrimitiveDynamicAccessor)
448+
{
449+
return true;
450+
}
446451

447452
if (elementAccessor is ListDynamicAccessor listDynamicAccessor)
448453
{
449-
return IsRequiringInline(listDynamicAccessor, element, parentConsecutiveList + 1);
450-
454+
inlining = IsRequiringInline(listDynamicAccessor, element, parentConsecutiveList + 1);
451455
}
452456
else if (elementAccessor is ObjectDynamicAccessor objAccessor)
453457
{
454458
// Case of an array-of-array of table
455459
if (parentConsecutiveList > 1) return true;
456-
return IsRequiringInline(objAccessor, element);
460+
inlining = IsRequiringInline(objAccessor, element);
457461
}
462+
463+
if (inlining) return true;
458464
}
459465

460466
// Specially for empty list

0 commit comments

Comments
 (0)