Skip to content

Commit 7c1071b

Browse files
committed
chore: add zero value check in Load
1 parent f56bb62 commit 7c1071b

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

_examples/main.go

+16
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func main() {
4242
log.Fatal(err)
4343
}
4444
table.Out()
45+
fmt.Println()
4546

4647
/*
4748
+------------+--------------+------------+------------+
@@ -73,6 +74,7 @@ func main() {
7374
log.Fatal(err)
7475
}
7576
table.Out()
77+
fmt.Println()
7678

7779
/*
7880
| InstanceID | InstanceName | AttachedLB | AttachedTG |
@@ -92,6 +94,7 @@ func main() {
9294
log.Fatal(err)
9395
}
9496
table.Out()
97+
fmt.Println()
9598

9699
/*
97100
| InstanceID | InstanceName | AttachedLB | AttachedTG |h
@@ -111,6 +114,7 @@ func main() {
111114
log.Fatal(err)
112115
}
113116
table.Out()
117+
fmt.Println()
114118

115119
/*
116120
+------------+--------------+------------+------------+
@@ -141,6 +145,7 @@ func main() {
141145
log.Fatal(err)
142146
}
143147
table.Out()
148+
fmt.Println()
144149

145150
/*
146151
+------------+--------------+------------+------------+
@@ -173,6 +178,7 @@ func main() {
173178
log.Fatal(err)
174179
}
175180
table.Out()
181+
fmt.Println()
176182

177183
/*
178184
+------------+--------------+------------+---------------------+
@@ -200,6 +206,7 @@ func main() {
200206
log.Fatal(err)
201207
}
202208
table.Out()
209+
fmt.Println()
203210

204211
/*
205212
+----------------+------------------+----------------+----------------+
@@ -263,6 +270,7 @@ func main() {
263270
log.Fatal(err)
264271
}
265272
table.Out()
273+
fmt.Println()
266274

267275
/*
268276
+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
@@ -294,6 +302,7 @@ func main() {
294302
log.Fatal(err)
295303
}
296304
table.Out()
305+
fmt.Println()
297306

298307
/*
299308
+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
@@ -325,6 +334,7 @@ func main() {
325334
log.Fatal(err)
326335
}
327336
table.Out()
337+
fmt.Println()
328338

329339
/*
330340
+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
@@ -356,6 +366,7 @@ func main() {
356366
log.Fatal(err)
357367
}
358368
table.Out()
369+
fmt.Println()
359370

360371
/*
361372
+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
@@ -399,6 +410,7 @@ func main() {
399410
log.Fatal(err)
400411
}
401412
table.Out()
413+
fmt.Println()
402414

403415
/*
404416
+---------------+---------------+
@@ -418,6 +430,7 @@ func main() {
418430
log.Fatal(err)
419431
}
420432
table.Out()
433+
fmt.Println()
421434

422435
/*
423436
+---------------+
@@ -463,6 +476,7 @@ func main() {
463476
log.Fatal(err)
464477
}
465478
table.Out()
479+
fmt.Println()
466480

467481
/*
468482
+-------------------------+-----------------------------------------+
@@ -492,6 +506,7 @@ func main() {
492506
log.Fatal(err)
493507
}
494508
table.Out()
509+
fmt.Println()
495510

496511
/*
497512
| Name | EscatableValue |
@@ -510,6 +525,7 @@ func main() {
510525
log.Fatal(err)
511526
}
512527
table.Out()
528+
fmt.Println()
513529

514530
/*
515531
| Name | EscatableValue |

mintab.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ func (t *Table) Load(input any) error {
155155
if v.Kind() == reflect.Ptr {
156156
v = v.Elem()
157157
}
158-
if v.Kind() == reflect.Struct {
158+
if v.Kind() != reflect.Slice {
159+
if v.IsZero() {
160+
return fmt.Errorf("no data found")
161+
}
159162
v = reflect.Append(reflect.MakeSlice(reflect.SliceOf(v.Type()), 0, 1), v)
160163
}
161164
if v.Len() == 0 {
@@ -197,7 +200,6 @@ func (t *Table) Out() {
197200
case FormatText, FormatCompressedText:
198201
t.printBorder()
199202
}
200-
fmt.Fprintf(t.writer, "\n")
201203
}
202204

203205
// printHeader renders the table header.
@@ -322,7 +324,7 @@ func (t *Table) setData(v reflect.Value) error {
322324
for j, h := range t.header {
323325
field := field.FieldByName(h)
324326
if !field.IsValid() {
325-
return fmt.Errorf("field \"%s\" does not exist", h)
327+
return fmt.Errorf("invalid field detected: %s", h)
326328
}
327329
f, err := t.formatField(field)
328330
if err != nil {

mintab_test.go

+25-21
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ type stringerSample struct {
4646
}
4747

4848
var (
49-
basicsample []basicSample
50-
basicsampleNonSlice basicSample
51-
basicsampleEmpty []basicSample
52-
nestedsample []nestedSample
53-
mergedsample []mergedSample
54-
stringersample stringerSample
55-
basicsamplePtr []*basicSample
56-
basicsampleSlicePtr *[]basicSample
57-
irregularsample []interface{}
49+
basicsample []basicSample
50+
basicsampleEmpty []basicSample
51+
basicsampleNonSlice basicSample
52+
basicsampleNonSliceEmpty basicSample
53+
nestedsample []nestedSample
54+
mergedsample []mergedSample
55+
stringersample stringerSample
56+
basicsamplePtr []*basicSample
57+
basicsampleSlicePtr *[]basicSample
58+
irregularsample []interface{}
5859
)
5960

6061
func TestMain(m *testing.M) {
@@ -72,15 +73,14 @@ func setup() {
7273
{InstanceID: "i-5", InstanceName: "server-5", AttachedLB: []string{"lb-5"}, AttachedTG: []string{}},
7374
{InstanceID: "i-6", InstanceName: "server-6", AttachedLB: []string{}, AttachedTG: []string{"tg-5", "tg-6", "tg-7", "tg-8"}},
7475
}
76+
basicsampleEmpty = []basicSample{}
7577
basicsampleNonSlice = basicSample{
7678
InstanceID: "i-1",
7779
InstanceName: "server-1",
7880
AttachedLB: []string{"lb-1"},
7981
AttachedTG: []string{"tg-1"},
8082
}
81-
basicsampleEmpty = []basicSample{
82-
{},
83-
}
83+
basicsampleNonSliceEmpty = basicSample{}
8484
nestedsample = []nestedSample{
8585
{
8686
BucketName: "bucket1",
@@ -311,6 +311,14 @@ func TestTable_Load(t *testing.T) {
311311
},
312312
wantErr: false,
313313
},
314+
{
315+
name: "struct_empty",
316+
fields: fields{},
317+
args: args{
318+
input: basicsampleNonSliceEmpty,
319+
},
320+
wantErr: true,
321+
},
314322
{
315323
name: "slice_ptr",
316324
fields: fields{},
@@ -331,7 +339,7 @@ func TestTable_Load(t *testing.T) {
331339
name: "slice_empty",
332340
fields: fields{},
333341
args: args{
334-
input: []string{},
342+
input: basicsampleEmpty,
335343
},
336344
wantErr: true,
337345
},
@@ -422,7 +430,6 @@ func TestTable_Out(t *testing.T) {
422430
| | | | tg-7 |
423431
| | | | tg-8 |
424432
+------------+--------------+------------+------------+
425-
426433
`,
427434
},
428435
{
@@ -455,7 +462,6 @@ func TestTable_Out(t *testing.T) {
455462
| | | | | Ingress | tcp | 0 | 65535 | PrefixList | pl-id/pl-name |
456463
| | | | | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
457464
+------------+--------------+-------+-----------------+---------------+------------+----------+--------+---------------+---------------+
458-
459465
`,
460466
},
461467
{
@@ -481,7 +487,6 @@ func TestTable_Out(t *testing.T) {
481487
| i-4 | server-4 | \- | \- |
482488
| i-5 | server-5 | lb-5 | \- |
483489
| i-6 | server-6 | \- | tg-5<br>tg-6<br>tg-7<br>tg-8 |
484-
485490
`,
486491
},
487492
{
@@ -506,7 +511,6 @@ func TestTable_Out(t *testing.T) {
506511
| i-4 | server-4 | - | - |
507512
| i-5 | server-5 | lb-5 | - |
508513
| i-6 | server-6 | - | tg-5&br;tg-6&br;tg-7&br;tg-8 |
509-
510514
`,
511515
},
512516
}
@@ -1011,7 +1015,7 @@ func TestTable_setHeader(t *testing.T) {
10111015
},
10121016
want: want{
10131017
header: []string{"InstanceID", "InstanceName", "AttachedLB", "AttachedTG"},
1014-
columnWidths: []int{10, 12, 10, 10},
1018+
columnWidths: []int{10, 12, 10, 16},
10151019
},
10161020
},
10171021
{
@@ -1021,7 +1025,7 @@ func TestTable_setHeader(t *testing.T) {
10211025
},
10221026
want: want{
10231027
header: []string{"InstanceID", "AttachedLB", "AttachedTG"},
1024-
columnWidths: []int{10, 10, 10},
1028+
columnWidths: []int{10, 10, 16},
10251029
},
10261030
},
10271031
{
@@ -1031,7 +1035,7 @@ func TestTable_setHeader(t *testing.T) {
10311035
},
10321036
want: want{
10331037
header: []string{"InstanceID", "InstanceName", "AttachedLB", "AttachedTG"},
1034-
columnWidths: []int{10, 12, 10, 10},
1038+
columnWidths: []int{10, 12, 10, 16},
10351039
},
10361040
},
10371041
}
@@ -1040,7 +1044,7 @@ func TestTable_setHeader(t *testing.T) {
10401044
tr := &Table{
10411045
ignoredFields: tt.fields.ignoredFields,
10421046
}
1043-
if err := tr.Load(basicsampleEmpty); err != nil {
1047+
if err := tr.Load(basicsample); err != nil {
10441048
t.Fatal(err)
10451049
}
10461050
tr.setHeader(tt.args.typ)

0 commit comments

Comments
 (0)