Skip to content

Commit

Permalink
Add support for deserializing to arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Mar 5, 2022
1 parent 21de2ab commit c80c3b4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
39 changes: 39 additions & 0 deletions src/Tomlyn.Tests/ModelTests/ReflectionModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public void TestModelWithSpecialDictionary()
a = 1
b = true
".ReplaceLineEndings().Trim();

StandardTests.DisplayHeader("input");
Console.WriteLine(input);
var model = Toml.ToModel<ModelWithSpecialDictionary>(input);
Expand All @@ -304,6 +305,28 @@ public void TestModelWithSpecialDictionary()
Assert.AreEqual(output, input);
}

[Test]
public void TestModelWithArray()
{
var input = @"values = ['1', '2', '3']";

StandardTests.DisplayHeader("input");
Console.WriteLine(input);
var model = Toml.ToModel<ModelWithArray>(input);
CollectionAssert.AreEqual(new string[] { "1", "2", "3"}, model.Values);
}

[Test]
public void TestModelWithFixedList()
{
var input = @"values = ['1', '2', '3']";

StandardTests.DisplayHeader("input");
Console.WriteLine(input);
var model = Toml.ToModel<ModelWithFixedList>(input);
CollectionAssert.AreEqual(new List<string> { "1", "2", "3" }, model.Values);
}

public class SimpleModel
{
public SimpleModel()
Expand All @@ -326,6 +349,22 @@ public SimpleModel()
public List<SimpleSubModel> SubModels { get; }
}

public class ModelWithArray
{
public string[]? Values { get; set; }
}

public class ModelWithFixedList
{
public ModelWithFixedList()
{
Values = new List<string>();
}

public List<string> Values { get; }
}


public class SimpleSubModel
{
public string? Id { get; set; }
Expand Down
17 changes: 12 additions & 5 deletions src/Tomlyn/Model/SyntaxToModelTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,24 +270,27 @@ public override void Visit(IntegerValueSyntax integerValueSyntax)
_currentValue = integerValueSyntax.Value;
}

public override void Visit(ArraySyntax array)
public override void Visit(ArraySyntax arraySyntax)
{
var stackCount = _objectStack.Count;
try
{
var list = _context.CreateInstance(_currentTargetType!, ObjectKind.Array);
var items = arraySyntax.Items;
var currentTargetType = _currentTargetType!;
var isArray = currentTargetType.IsArray;
var list = isArray ? Array.CreateInstance(currentTargetType.GetElementType()!, items.ChildrenCount) : _context.CreateInstance(currentTargetType, ObjectKind.Array);
var array = list as Array;
var fastList = list as IList;
var accessor = _context.GetAccessor(list.GetType());
var listAccessor = accessor as ListDynamicAccessor;

// Fail if we don't have a list accessor
if (listAccessor is null)
{
_context.Diagnostics.Error(array.Span, $"Invalid list type {list.GetType().FullName}. Getting a {accessor} instead.");
_context.Diagnostics.Error(arraySyntax.Span, $"Invalid list type {list.GetType().FullName}. Getting a {accessor} instead.");
return;
}

var items = array.Items;
for (int i = 0; i < items.ChildrenCount; i++)
{
var item = items.GetChild(i)!;
Expand All @@ -299,7 +302,11 @@ public override void Visit(ArraySyntax array)
continue;
}

if (fastList is not null)
if (array is not null)
{
array.SetValue(itemValue, i);
}
else if (fastList is not null)
{
fastList.Add(itemValue);
}
Expand Down

0 comments on commit c80c3b4

Please sign in to comment.