diff --git a/mintab.go b/mintab.go index 52cab4e..64745e3 100644 --- a/mintab.go +++ b/mintab.go @@ -155,8 +155,8 @@ func (t *Table) Load(input any) error { if v.Kind() == reflect.Ptr { v = v.Elem() } - if v.Kind() != reflect.Slice { - return fmt.Errorf("input must be a slice or a pointer to a slice") + if v.Kind() == reflect.Struct { + v = reflect.Append(reflect.MakeSlice(reflect.SliceOf(v.Type()), 0, 1), v) } if v.Len() == 0 { return fmt.Errorf("no data found") diff --git a/mintab_test.go b/mintab_test.go index 1eea05d..b66f289 100644 --- a/mintab_test.go +++ b/mintab_test.go @@ -40,6 +40,7 @@ type mergedSample struct { var ( basicsample []basicSample + basicsampleNonSlice basicSample basicsampleEmpty []basicSample nestedsample []nestedSample mergedsample []mergedSample @@ -63,6 +64,12 @@ func setup() { {InstanceID: "i-5", InstanceName: "server-5", AttachedLB: []string{"lb-5"}, AttachedTG: []string{}}, {InstanceID: "i-6", InstanceName: "server-6", AttachedLB: []string{}, AttachedTG: []string{"tg-5", "tg-6", "tg-7", "tg-8"}}, } + basicsampleNonSlice = basicSample{ + InstanceID: "i-1", + InstanceName: "server-1", + AttachedLB: []string{"lb-1"}, + AttachedTG: []string{"tg-1"}, + } basicsampleEmpty = []basicSample{ {}, } @@ -259,7 +266,7 @@ func TestTable_Load(t *testing.T) { wantErr bool }{ { - name: "err_margin", + name: "signed_margin", fields: fields{ margin: -1, }, @@ -269,7 +276,7 @@ func TestTable_Load(t *testing.T) { wantErr: true, }, { - name: "err_interface{}", + name: "interface{}", fields: fields{}, args: args{ input: irregularsample, @@ -277,7 +284,7 @@ func TestTable_Load(t *testing.T) { wantErr: true, }, { - name: "err_ptr", + name: "ptr", fields: fields{}, args: args{ input: basicsamplePtr, @@ -285,47 +292,47 @@ func TestTable_Load(t *testing.T) { wantErr: false, }, { - name: "err_slice_ptr", + name: "struct", fields: fields{}, args: args{ - input: basicsampleSlicePtr, + input: basicsampleNonSlice, }, wantErr: false, }, { - name: "err_slice_ptr", + name: "slice_ptr", fields: fields{}, args: args{ - input: "aaa", + input: basicsampleSlicePtr, }, - wantErr: true, + wantErr: false, }, { - name: "err_slice_ptr", + name: "string", fields: fields{}, args: args{ - input: []string{}, + input: "aaa", }, wantErr: true, }, { - name: "err_struct_in_slice", + name: "slice_empty", fields: fields{}, args: args{ - input: []string{"dummy"}, + input: []string{}, }, wantErr: true, }, { - name: "err_irregular", + name: "slice_string", fields: fields{}, args: args{ - input: irregularsample, + input: []string{"dummy"}, }, wantErr: true, }, { - name: "err_irregular", + name: "invalid_header", fields: fields{ header: []string{"dummmy"}, },