Skip to content

Commit d64a5d2

Browse files
committed
Fix inline tables for issue #47
1 parent b3b7c75 commit d64a5d2

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/Tomlyn.Tests/ModelTests/ReflectionModelTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,51 @@ public void TestReflectionFieldsModelWithConvertName()
538538
Assert.AreEqual(127, result);
539539
}
540540

541+
[Test]
542+
public void TestReflectionModelWithInlineTable()
543+
{
544+
var input = @"name = ""this is a name""
545+
values = [""a"", ""b"", ""c"", 1]
546+
intvalues = 1
547+
intvalue = 2
548+
doublevalue = 2.5
549+
sub = [ { id = ""id1"", publish = true }, { id = ""id2"", publish = false }, { id = ""id3"" } ]
550+
";
551+
var syntax = Toml.Parse(input);
552+
Assert.False(syntax.HasErrors, "The document should not have any errors");
553+
554+
StandardTests.Dump(input, syntax, syntax.ToString());
555+
556+
557+
var options = new TomlModelOptions() { ConvertPropertyName = name => name.ToLowerInvariant(), ConvertFieldName = name => name.ToLowerInvariant() };
558+
var model = syntax.ToModel<SimpleModel>(options);
559+
560+
Assert.AreEqual("this is a name", model.Name);
561+
Assert.AreEqual(new List<string>() { "a", "b", "c", "1" }, model.Values);
562+
Assert.AreEqual(new List<int>() { 1 }, model.IntValues);
563+
Assert.AreEqual(2, model.IntValue);
564+
Assert.AreEqual(2.5, model.DoubleValue);
565+
Assert.AreEqual(3, model.SubModels.Count);
566+
var sub = model.SubModels[0];
567+
Assert.AreEqual("id1", sub.Id);
568+
Assert.True(sub.Publish);
569+
sub = model.SubModels[1];
570+
Assert.AreEqual("id2", sub.Id);
571+
Assert.False(sub.Publish);
572+
sub = model.SubModels[2];
573+
Assert.AreEqual("id3", sub.Id);
574+
Assert.False(sub.Publish);
575+
576+
model.SubModels[2].Value = 127;
577+
578+
var toml = Toml.FromModel(model);
579+
StandardTests.DisplayHeader("toml from model");
580+
Console.WriteLine(toml);
581+
582+
var model2 = Toml.ToModel(toml);
583+
var result = (model2["sub"] as TomlTableArray)?[2]?["value"];
584+
Assert.AreEqual(127, result);
585+
}
541586

542587
[Test]
543588
public void TestReflectionModelWithErrors()

src/Tomlyn/Model/SyntaxToModelTransform.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ public override void Visit(ArraySyntax arraySyntax)
293293

294294
for (int i = 0; i < items.ChildrenCount; i++)
295295
{
296+
_currentTargetType = listAccessor.ElementType;
296297
var item = items.GetChild(i)!;
297298
item.Accept(this);
298299

@@ -316,6 +317,7 @@ public override void Visit(ArraySyntax arraySyntax)
316317
}
317318
}
318319
_currentValue = list;
320+
_currentTargetType = currentTargetType;
319321
}
320322
finally
321323
{

0 commit comments

Comments
 (0)