Skip to content

Commit

Permalink
Fix inline tables for issue #47
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Oct 27, 2022
1 parent b3b7c75 commit d64a5d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Tomlyn.Tests/ModelTests/ReflectionModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,51 @@ public void TestReflectionFieldsModelWithConvertName()
Assert.AreEqual(127, result);
}

[Test]
public void TestReflectionModelWithInlineTable()
{
var input = @"name = ""this is a name""
values = [""a"", ""b"", ""c"", 1]
intvalues = 1
intvalue = 2
doublevalue = 2.5
sub = [ { id = ""id1"", publish = true }, { id = ""id2"", publish = false }, { id = ""id3"" } ]
";
var syntax = Toml.Parse(input);
Assert.False(syntax.HasErrors, "The document should not have any errors");

StandardTests.Dump(input, syntax, syntax.ToString());


var options = new TomlModelOptions() { ConvertPropertyName = name => name.ToLowerInvariant(), ConvertFieldName = name => name.ToLowerInvariant() };
var model = syntax.ToModel<SimpleModel>(options);

Assert.AreEqual("this is a name", model.Name);
Assert.AreEqual(new List<string>() { "a", "b", "c", "1" }, model.Values);
Assert.AreEqual(new List<int>() { 1 }, model.IntValues);
Assert.AreEqual(2, model.IntValue);
Assert.AreEqual(2.5, model.DoubleValue);
Assert.AreEqual(3, model.SubModels.Count);
var sub = model.SubModels[0];
Assert.AreEqual("id1", sub.Id);
Assert.True(sub.Publish);
sub = model.SubModels[1];
Assert.AreEqual("id2", sub.Id);
Assert.False(sub.Publish);
sub = model.SubModels[2];
Assert.AreEqual("id3", sub.Id);
Assert.False(sub.Publish);

model.SubModels[2].Value = 127;

var toml = Toml.FromModel(model);
StandardTests.DisplayHeader("toml from model");
Console.WriteLine(toml);

var model2 = Toml.ToModel(toml);
var result = (model2["sub"] as TomlTableArray)?[2]?["value"];
Assert.AreEqual(127, result);
}

[Test]
public void TestReflectionModelWithErrors()
Expand Down
2 changes: 2 additions & 0 deletions src/Tomlyn/Model/SyntaxToModelTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ public override void Visit(ArraySyntax arraySyntax)

for (int i = 0; i < items.ChildrenCount; i++)
{
_currentTargetType = listAccessor.ElementType;
var item = items.GetChild(i)!;
item.Accept(this);

Expand All @@ -316,6 +317,7 @@ public override void Visit(ArraySyntax arraySyntax)
}
}
_currentValue = list;
_currentTargetType = currentTargetType;
}
finally
{
Expand Down

0 comments on commit d64a5d2

Please sign in to comment.