From cedfd51f1f2669ac178695eef9ac90e7da1d6ac5 Mon Sep 17 00:00:00 2001 From: jmank88 Date: Sat, 4 Feb 2023 12:43:30 -0600 Subject: [PATCH] add TestUnmarshalArrayOfTables and placeholder error; towards #839 --- unmarshaler.go | 6 +++++- unmarshaler_test.go | 14 +++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/unmarshaler.go b/unmarshaler.go index 70f6ec57..35f6c4d6 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -377,7 +377,11 @@ func (d *decoder) handleArrayTableCollection(key unstable.Iterator, v reflect.Va return v, nil case reflect.Slice: - elem := v.Index(v.Len() - 1) + idx := v.Len() - 1 + if idx < 0 { + return v, fmt.Errorf("TODO uninitialized array table") + } + elem := v.Index(idx) x, err := d.handleArrayTable(key, elem) if err != nil || d.skipUntilTable { return reflect.Value{}, err diff --git a/unmarshaler_test.go b/unmarshaler_test.go index 88255d14..f49d2b14 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -11,9 +11,10 @@ import ( "testing" "time" - "github.com/pelletier/go-toml/v2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/pelletier/go-toml/v2" ) func ExampleDecoder_DisallowUnknownFields() { @@ -3301,3 +3302,14 @@ func TestUnmarshalEmbedNonString(t *testing.T) { require.NoError(t, err) require.Nil(t, d.Foo) } + +func TestUnmarshalArrayOfTables(t *testing.T) { + m := struct { + A []struct { + B struct{} + } + }{} + require.NotPanics(t, func() { + require.Error(t, toml.Unmarshal([]byte(`[[A.B]]`), &m)) + }) +}