From 08f3ae92d3935559a5134bc5da900d3784142bf3 Mon Sep 17 00:00:00 2001 From: Peter Date: Sun, 11 Oct 2020 21:33:23 +0200 Subject: [PATCH] fix(std): Parsing inline arrays of inline tables in toml (#7902) --- .../testdata/inlineArrayOfInlineTable.toml | 8 ++++++ std/encoding/toml.ts | 5 +++- std/encoding/toml_test.ts | 25 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 std/encoding/testdata/inlineArrayOfInlineTable.toml diff --git a/std/encoding/testdata/inlineArrayOfInlineTable.toml b/std/encoding/testdata/inlineArrayOfInlineTable.toml new file mode 100644 index 0000000000..a440ff927a --- /dev/null +++ b/std/encoding/testdata/inlineArrayOfInlineTable.toml @@ -0,0 +1,8 @@ +[inlineArray] +string = [ {var = "a string"} ] + +my_points = [ { x = 1, y = 2, z = 3 }, { x = 7, y = 8, z = 9 }, { x = 2, y = 4, z = 8 } ] + +points = [ { x = 1, y = 2, z = 3 }, + { x = 7, y = 8, z = 9 }, + { x = 2, y = 4, z = 8 } ] \ No newline at end of file diff --git a/std/encoding/toml.ts b/std/encoding/toml.ts index 86ee0bdebd..48035945c9 100644 --- a/std/encoding/toml.ts +++ b/std/encoding/toml.ts @@ -305,7 +305,10 @@ class Parser { dataString = dataString.replace(/,]/g, "]"); } - if (dataString[0] === "{" && dataString[dataString.length - 1] === "}") { + if ( + (dataString[0] === "{" && dataString[dataString.length - 1] === "}") || + (dataString[0] === "[" && dataString[dataString.length - 1] === "]") + ) { const reg = /([a-zA-Z0-9-_\.]*) (=)/gi; let result; while ((result = reg.exec(dataString))) { diff --git a/std/encoding/toml_test.ts b/std/encoding/toml_test.ts index 5666a875ac..8e25d61ea1 100644 --- a/std/encoding/toml_test.ts +++ b/std/encoding/toml_test.ts @@ -441,3 +441,28 @@ Deno.test({ assertEquals(actual, expected); }, }); + +Deno.test({ + name: "[TOML] Inline Array of Inline Table", + fn(): void { + const expected = { + inlineArray: { + string: [{ var: "a string" }], + my_points: [ + { x: 1, y: 2, z: 3 }, + { x: 7, y: 8, z: 9 }, + { x: 2, y: 4, z: 8 }, + ], + points: [ + { x: 1, y: 2, z: 3 }, + { x: 7, y: 8, z: 9 }, + { x: 2, y: 4, z: 8 }, + ], + }, + }; + const actual = parseFile( + path.join(testdataDir, "inlineArrayOfInlineTable.toml"), + ); + assertEquals(actual, expected); + }, +});