toml-lang / toml-test

A language agnostic test suite for TOML parsers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

valid/table/array-one fails when using inline array and table

arp242 opened this issue · comments

In the Array of Tables section of TOML v1.0 it explicitly says that "you may also use inline tables where appropriate" instead of an Array of Tables.

However toml-test seems to fail encoders if they use inline tables where appropriate instead of an Array of Tables. This doesn't seem to be a requirement of the TOML spec itself.

An example would be an encoder providing the output

people = [{first_name = 'Bruce', last_name = 'Springsteen'}]

in the "valid/table/array-one" encoding test instead of

[[people]]
first_name = "Bruce"
last_name = "Springsteen"

Currently this fails in toml-test due to:

     Type for key 'people' differs:
       Expected:     [map[first_name:Bruce last_name:Springsteen]] ([]map[string]interface {})
       Your encoder: [map[first_name:Bruce last_name:Springsteen]] ([]interface {})

but aren't these simply two different ways of expressing the same thing?

Originally posted by @trevorld in #12 (comment)

I think a similar issue may affect the following tests ("valid/table/array-one" is the most minimal test affected by this):

  • valid/inline-table/key-dotted
  • valid/inline-table/nest
  • valid/key/dotted
  • valid/table/array-implicit
  • valid/table/array-many
  • valid/table/array-nest
  • valid/table/array-one
  • valid/table/array-table-array

Reproducible with:

% cat x
#!/bin/sh

cat <<EOF
[[people]]
first_name = "Bruce"
last_name = "Springsteen"
EOF

% toml-test -encoder -run valid/table/array-one -v ./x
PASS valid/table/array-one
toml-test [./x]: using embedded tests:   1 passed,  0 failed, 155 skipped

% cat y
#!/bin/sh

cat <<EOF
people = [{first_name = 'Bruce', last_name = 'Springsteen'}]
EOF

% toml-test -encoder -run valid/table/array-one -v ./y
FAIL valid/table/array-one
	 Type for key 'people' differs:
	   Expected:     [map[first_name:Bruce last_name:Springsteen]] ([]map[string]interface {})
	   Your encoder: [map[first_name:Bruce last_name:Springsteen]] ([]interface {})

	 input sent to parser-cmd:
	   {
		 "people": [
		   {
			 "first_name": {
			   "type": "string",
			   "value": "Bruce"
			 },
			 "last_name": {
			   "type": "string",
			   "value": "Springsteen"
			 }
		   }
		 ]
	   }

	 output from parser-cmd (stdout):
	   people = [{first_name = 'Bruce', last_name = 'Springsteen'}]

	 want:
	   [[people]]
	   first_name = "Bruce"
	   last_name = "Springsteen"

toml-test [./y]: using embedded tests:   0 passed,  1 failed, 155 skipped

It's kind of an artefact how the TOML library that toml-test uses processes things; with a [[..]] type array we get:

[]map[string]any{
    map[string]any{"first_name":"Bruce", "last_name":"Springsteen"},
}

But with an inline array we get:

[]any{
    map[string]any{"first_name":"Bruce", "last_name":"Springsteen"},
}

This is the same value: a slice of map[string]any, but the values are typed differently.

The below patch should fix it, but I want to have another think because I'm not sure if that will break anything (it's been a few years since I wrote all of this), and it might be better to fix this in the TOML library than here too, because I could imagine this causing issues for other users of the library too.

diff --git toml.go toml.go
index 346f0a9..d900940 100644
--- toml.go
+++ toml.go
@@ -31,9 +31,17 @@ func (r Test) CompareTOML(want, have interface{}) Test {
                return r
        }

+start:
        switch w := want.(type) {
        case map[string]interface{}:
                return r.cmpTOMLMap(w, have)
+       case []map[string]interface{}:
+               ww := make([]interface{}, 0, len(w))
+               for _, v := range w {
+                       ww = append(ww, v)
+               }
+               want = ww
+               goto start
        case []interface{}:
                return r.cmpTOMLArrays(w, have)
        default:
@@ -126,7 +134,7 @@ func deepEqual(want, have interface{}) bool {

 func isTomlValue(v interface{}) bool {
        switch v.(type) {
-       case map[string]interface{}, []interface{}:
+       case map[string]interface{}, []interface{}, []map[string]interface{}:
                return false
        }
        return true