Skip to content
This repository was archived by the owner on Oct 19, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Nett:
+ Fix: Improve conversion error messages [#29](https://github.com/paiden/Nett/issues/29)

AspNet:
+ Add: Support for array of tables and jagged arrays conversion [#75](https://github.com/paiden/Nett/issues/75)
+ Fix: Fix missing dependency for NuGet package [#72](https://github.com/paiden/Nett/issues/72)

Exp:
Expand Down
20 changes: 15 additions & 5 deletions Source/Nett.AspNet/ProviderDictionaryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private static void ProcessTable(Dictionary<string, string> dict, TomlTable tabl
{
switch (r.Value)
{
case TomlTableArray ta: ProcessTableArray(dict, ta, FullKey(r)); break;
case TomlTable t: ProcessTable(dict, t, keyPrefix + r.Key + ":"); break;
case TomlArray a: ProcessArray(dict, a, FullKey(r)); break;
case TomlBool b: AddEntry(r, b.Value.ToString(CultureInfo.InvariantCulture)); break;
Expand All @@ -43,18 +44,27 @@ string FullKey(KeyValuePair<string, TomlObject> row)
=> keyPrefix + row.Key;
}

private static void ProcessTableArray(Dictionary<string, string> dict, TomlTableArray tableArray, string keyPrefix)
{
for (int i = 0; i < tableArray.Items.Count; i++)
{
TomlTable table = tableArray.Items[i];
ProcessTable(dict, table, $"{keyPrefix}:{i}:");
}
}

private static void ProcessArray(Dictionary<string, string> dict, TomlArray array, string fullKey)
{
for (int i = 0; i < array.Items.Length; i++)
{
if (array.Items[i] is TomlArray a)
{
throw new InvalidOperationException(
$"AspNet provider cannot handle jagged arrays, only simple arrays are supported." +
$"The arrays key is '{fullKey}'.");
ProcessArray(dict, a, $"{fullKey}:{i}");
}
else
{
dict[$"{fullKey}:{i}"] = array.Items[i].ToString();
}

dict[$"{fullKey}:{i}"] = array.Items[i].ToString();
}
}
}
Expand Down
54 changes: 45 additions & 9 deletions Test/Nett.AspNet.Tests/ProviderDictionaryConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,68 @@ public void ToProviderDictionary_CreatesDictionaryCorrespondingToTomlContent()
}

[Fact]
public void ToProviderDictionary_WhenTomlContainsTblArray_ProducesUsefulErrorMessage()
public void ToProviderDictionary_Converts_TomlTableArray()
{
// Arrange
var tml = Toml.ReadString(@"
[[x]]
[[x]]");
[[tableArray]]

[[tableArray]]
STA1 = ""array1""
STA2 = 1

[[tableArray]]

[[tableArray]]
STA1 = ""array3""
STA2 = 3

[[tableArray.nestedTableArray]]
NTA1 = ""nestedArray0""
NTA2 = 10

[[tableArray.nestedTableArray]]

[[tableArray.nestedTableArray]]
NTA1 = ""nestedArray2""
NTA2 = 22");

// Act
Action a = () => ProviderDictionaryConverter.ToProviderDictionary(tml);
var providerDict = ProviderDictionaryConverter.ToProviderDictionary(tml);

// Assert
a.ShouldThrow<InvalidOperationException>().WithMessage("AspNet provider cannot handle TOML object of type 'array of tables'. The objects key is 'x'.");
providerDict.Should().Equal(new Dictionary<string, string>()
{
{ "tableArray:1:STA1", "array1" },
{ "tableArray:1:STA2", "1" },
{ "tableArray:3:STA1", "array3" },
{ "tableArray:3:STA2", "3" },
{ "tableArray:3:nestedTableArray:0:NTA1", "nestedArray0" },
{ "tableArray:3:nestedTableArray:0:NTA2", "10" },
{ "tableArray:3:nestedTableArray:2:NTA1", "nestedArray2" },
{ "tableArray:3:nestedTableArray:2:NTA2", "22" },
});
}

[Fact]
public void ToProviderDictionary_WhenTomlContainsJaggedArray_ProducesUsefulErrorMessage()
public void ToProviderDictionary_Converts_JaggedArray()
{
// Arrange
var tml = Toml.ReadString(@"
x = [[1]]");
jaggedArray = [ [ ""index00"", ""index01"" ], [ ""index10"" ], [], [ ""index30"" ] ]
");

// Act
Action a = () => ProviderDictionaryConverter.ToProviderDictionary(tml);
var providerDict = ProviderDictionaryConverter.ToProviderDictionary(tml);

// Assert
a.ShouldThrow<InvalidOperationException>().WithMessage("AspNet provider cannot handle jagged arrays, only simple arrays are supported.The arrays key is 'x'.");
providerDict.Should().Equal(new Dictionary<string, string>()
{
{ "jaggedArray:0:0", "index00" },
{ "jaggedArray:0:1", "index01" },
{ "jaggedArray:1:0", "index10" },
{ "jaggedArray:3:0", "index30" }
});
}
}
}