Skip to content

Commit

Permalink
Fix panic on empty interface field
Browse files Browse the repository at this point in the history
  • Loading branch information
leonid-shevtsov authored and david-littlefarmer committed Sep 11, 2024
1 parent d4b5e3b commit dd7530d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions devslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,12 @@ func (h *developHandler) elementType(t reflect.Type, v reflect.Value, l int, p i
}
}
case reflect.Interface:
v = reflect.ValueOf(v.Interface())
b = h.elementType(v.Type(), v, l, p)
if v.IsZero() {
b = nilString()
} else {
v = reflect.ValueOf(v.Interface())
b = h.elementType(v.Type(), v, l, p)
}
default:
b = atb("Unknown type: ")
b = append(b, atb(v.Kind())...)
Expand Down
24 changes: 24 additions & 0 deletions devslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func Test_Types(t *testing.T) {
test_MapOfPointers(t, opts)
test_MapOfInterface(t, opts)
test_Struct(t, opts)
test_NilInterface(t, opts)
test_Group(t, opts)
test_LogValuer(t, opts)
test_LogValuerPanic(t, opts)
Expand Down Expand Up @@ -734,6 +735,29 @@ func test_Struct(t *testing.T, o *Options) {
}
}

func test_NilInterface(t *testing.T, o *Options) {
w := &MockWriter{}
logger := slog.New(NewHandler(w, o))

type StructWithInterface struct {
Data any
}

s := StructWithInterface{}

logger.Info("msg",
slog.Any("s", s),
)

expected := []byte(
"\x1b[2m\x1b[37m[]\x1b[0m \x1b[42m\x1b[30m INFO \x1b[0m \x1b[32mmsg\x1b[0m\n\x1b[33mS\x1b[0m \x1b[35ms\x1b[0m: \x1b[33md\x1b[0m\x1b[33me\x1b[0m\x1b[33mv\x1b[0m\x1b[33ms\x1b[0m\x1b[33ml\x1b[0m\x1b[33mo\x1b[0m\x1b[33mg\x1b[0m\x1b[33m.\x1b[0m\x1b[33mS\x1b[0m\x1b[33mt\x1b[0m\x1b[33mr\x1b[0m\x1b[33mu\x1b[0m\x1b[33mc\x1b[0m\x1b[33mt\x1b[0m\x1b[33mW\x1b[0m\x1b[33mi\x1b[0m\x1b[33mt\x1b[0m\x1b[33mh\x1b[0m\x1b[33mI\x1b[0m\x1b[33mn\x1b[0m\x1b[33mt\x1b[0m\x1b[33me\x1b[0m\x1b[33mr\x1b[0m\x1b[33mf\x1b[0m\x1b[33ma\x1b[0m\x1b[33mc\x1b[0m\x1b[33me\x1b[0m\n \x1b[32mData\x1b[0m: \x1b[31m<\x1b[0m\x1b[33mnil\x1b[0m\x1b[31m>\x1b[0m\n\n",
)

if !bytes.Equal(w.WrittenData, expected) {
t.Errorf("\nExpected:\n%s\nGot:\n%s\nExpected:\n%[1]q\nGot:\n%[2]q", expected, w.WrittenData)
}
}

func test_Group(t *testing.T, o *Options) {
w := &MockWriter{}
logger := slog.New(NewHandler(w, o))
Expand Down

0 comments on commit dd7530d

Please sign in to comment.