diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 99db0d5..3e8d465 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,7 +10,7 @@ on: jobs: lint: - uses: endobit/ci/.github/workflows/go-lint.yaml@main + uses: endobit/ci/.github/workflows/go-lint.yaml@v1.3.1 test: - uses: endobit/ci/.github/workflows/go-test.yaml@main + uses: endobit/ci/.github/workflows/go-test.yaml@v1.3.1 secrets: inherit diff --git a/table.go b/table.go index 5f2861d..07ccb4c 100644 --- a/table.go +++ b/table.go @@ -115,7 +115,8 @@ func New(opts ...func(*Table)) *Table { // flushed if a new struct type is written. If a is not a struct, an error table // will be added to the output. func (t *Table) Write(a any) { - if reflect.TypeOf(a).Kind() != reflect.Struct { + typ := reflect.TypeOf(a) + if typ == nil || typ.Kind() != reflect.Struct { msg := struct { Error error Type string diff --git a/table_test.go b/table_test.go index 0568bf0..76a5ffe 100644 --- a/table_test.go +++ b/table_test.go @@ -254,6 +254,21 @@ func TestWriteNonStruct(t *testing.T) { } } +func TestWriteNil(t *testing.T) { + var buf bytes.Buffer + + tbl := New(WithWriter(&buf)) + + tbl.Write(nil) + _ = tbl.Flush() + + output := buf.String() + + if !strings.Contains(output, "not a struct") { + t.Errorf("expected error message in output, got: %q", output) + } +} + func TestEmptyTable(t *testing.T) { var buf bytes.Buffer