Skip to content

Commit

Permalink
feat: convert struct to single-element slice
Browse files Browse the repository at this point in the history
  • Loading branch information
nekrassov01 committed Feb 24, 2024
1 parent d1608ae commit 972a54f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions mintab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
37 changes: 22 additions & 15 deletions mintab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type mergedSample struct {

var (
basicsample []basicSample
basicsampleNonSlice basicSample
basicsampleEmpty []basicSample
nestedsample []nestedSample
mergedsample []mergedSample
Expand All @@ -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{
{},
}
Expand Down Expand Up @@ -259,7 +266,7 @@ func TestTable_Load(t *testing.T) {
wantErr bool
}{
{
name: "err_margin",
name: "signed_margin",
fields: fields{
margin: -1,
},
Expand All @@ -269,63 +276,63 @@ func TestTable_Load(t *testing.T) {
wantErr: true,
},
{
name: "err_interface{}",
name: "interface{}",
fields: fields{},
args: args{
input: irregularsample,
},
wantErr: true,
},
{
name: "err_ptr",
name: "ptr",
fields: fields{},
args: args{
input: basicsamplePtr,
},
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"},
},
Expand Down

0 comments on commit 972a54f

Please sign in to comment.