diff --git a/devslog.go b/devslog.go index 170cb96..4242339 100644 --- a/devslog.go +++ b/devslog.go @@ -558,6 +558,9 @@ func (h *developHandler) elementType(t reflect.Type, v reflect.Value, l int, p i // b = atb(s) } + case reflect.Interface: + v = reflect.ValueOf(v.Interface()) + b = h.elementType(v.Type(), v, l, p) default: b = atb("Unknown type: ") b = append(b, atb(v.Kind())...) diff --git a/devslog_test.go b/devslog_test.go index dc5697f..68f32d5 100644 --- a/devslog_test.go +++ b/devslog_test.go @@ -68,6 +68,7 @@ func Test_Types(t *testing.T) { test_SliceBig(t, opts) test_Map(t, opts) test_MapOfPointers(t, opts) + test_MapOfInterface(t, opts) test_Struct(t, opts) test_Group(t, opts) test_LogValuer(t, opts) @@ -653,6 +654,26 @@ func test_MapOfPointers(t *testing.T, o *Options) { } } +func test_MapOfInterface(t *testing.T, o *Options) { + w := &MockWriter{} + logger := slog.New(NewHandler(w, o)) + + m := map[int]any{0: "a", 1: "b"} + mp := &m + + logger.Info("msg", + slog.Any("m", m), + slog.Any("mp", mp), + slog.Any("mpp", &mp), + ) + + expected := []byte("\x1b[2m\x1b[37m[]\x1b[0m \x1b[42m\x1b[30m INFO \x1b[0m \x1b[32mmsg\x1b[0m\n\x1b[32mM\x1b[0m \x1b[35mm\x1b[0m : \x1b[34m2\x1b[0m \x1b[33mm\x1b[0m\x1b[33ma\x1b[0m\x1b[33mp\x1b[0m\x1b[32m[\x1b[0m\x1b[33mi\x1b[0m\x1b[33mn\x1b[0m\x1b[33mt\x1b[0m\x1b[32m]\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\x1b[33m \x1b[0m\x1b[33m{\x1b[0m\x1b[33m}\x1b[0m\n \x1b[32m0\x1b[0m: a\n \x1b[32m1\x1b[0m: b\n\x1b[32mM\x1b[0m \x1b[35mmp\x1b[0m : \x1b[34m2\x1b[0m \x1b[31m*\x1b[0m\x1b[33mm\x1b[0m\x1b[33ma\x1b[0m\x1b[33mp\x1b[0m\x1b[32m[\x1b[0m\x1b[33mi\x1b[0m\x1b[33mn\x1b[0m\x1b[33mt\x1b[0m\x1b[32m]\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\x1b[33m \x1b[0m\x1b[33m{\x1b[0m\x1b[33m}\x1b[0m\n \x1b[32m0\x1b[0m: a\n \x1b[32m1\x1b[0m: b\n\x1b[32mM\x1b[0m \x1b[35mmpp\x1b[0m: \x1b[34m2\x1b[0m \x1b[31m*\x1b[0m\x1b[31m*\x1b[0m\x1b[33mm\x1b[0m\x1b[33ma\x1b[0m\x1b[33mp\x1b[0m\x1b[32m[\x1b[0m\x1b[33mi\x1b[0m\x1b[33mn\x1b[0m\x1b[33mt\x1b[0m\x1b[32m]\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\x1b[33m \x1b[0m\x1b[33m{\x1b[0m\x1b[33m}\x1b[0m\n \x1b[32m0\x1b[0m: a\n \x1b[32m1\x1b[0m: b\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_Struct(t *testing.T, o *Options) { w := &MockWriter{} logger := slog.New(NewHandler(w, o))