Repository: BurntSushi/toml (go)

Issue:
x = [] decodes into nil
The following program works differently between 0.3.1 and 0.4.1:

```
package main

import (
    "log"
    "reflect"

    "github.com/BurntSushi/toml"
)

func main() {
    blob := `
    x = []
    `
    var d map[string]interface{}
    if _, err := toml.Decode(blob, &d); err != nil {
        log.Fatal(err)
    }

    x := d["x"]
    log.Println(x, reflect.ValueOf(x).IsNil())
}
```

On 0.3.1,
```
[] false
```

On 0.4.1,
```
[] true
```

For my use case, this is a problem because I'm subsequently encoding the value into JSON and it writes `x = null` instead of the expected `x = []`. It also goes deeper than that, for instance `x = [[]]` would get JSONed as `x = [null]`.

Is this an intended change? Do you think it would be possible/correct to restore the previous behaviour?

Pull request description:
Decode S=[] into a non-nil []interface{}.
This restores v0.3 behaviour and also makes it
consistent with how the same decodes into something
like struct { S []string }.

Closes #338

Patch:
diff --git a/parse.go b/parse.go
index 618cbe4..05535a8 100644
--- a/parse.go
+++ b/parse.go
@@ -350,6 +350,10 @@ func (p *parser) valueArray(it item) (interface{}, tomlType) {
 		array []interface{}
 		types []tomlType
 	)
+	// Initialize to a non-nil empty slice. This makes it
+	// consistent with how S = [] decodes into a non-nil slice
+	// inside something like struct { S []string }.
+	array = []interface{}{}
 	for it = p.next(); it.typ != itemArrayEnd; it = p.next() {
 		if it.typ == itemCommentStart {
 			p.expect(itemText)


Test patch:
diff --git a/decode_test.go b/decode_test.go
index c89b572..f826ecf 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -618,6 +618,17 @@ func TestDecodeSlices(t *testing.T) {
 	}
 }
 
+func TestDecodeInterfaceSlice(t *testing.T) {
+	var v map[string]interface{}
+	if _, err := Decode(`S = []`, &v); err != nil {
+		t.Errorf(err.Error())
+	}
+	// S = [] should decode into a non-nil []interface{} (see issue #338).
+	if reflect.ValueOf(v["S"]).IsNil() {
+		t.Errorf("S is nil")
+	}
+}
+
 func TestDecodePrimitive(t *testing.T) {
 	type S struct {
 		P Primitive