From a2fc6f40b3bc305a53a3f05ad4b81d99ebb4aaf1 Mon Sep 17 00:00:00 2001 From: Masayoshi Mizutani Date: Mon, 26 Feb 2024 15:00:08 +0900 Subject: [PATCH] Add handling for nil interface in clone function --- clone.go | 3 +++ clone_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/clone.go b/clone.go index 4e66523..2a7b91a 100644 --- a/clone.go +++ b/clone.go @@ -107,6 +107,9 @@ func (x *masq) clone(fieldName string, src reflect.Value, tag string) reflect.Va return dst case reflect.Interface: + if src.IsNil() { + return src + } return x.clone(fieldName, src.Elem(), tag) default: diff --git a/clone_test.go b/clone_test.go index 3bab049..677e320 100644 --- a/clone_test.go +++ b/clone_test.go @@ -316,3 +316,15 @@ func TestDirectUUID(t *testing.T) { gt.S(t, buf.String()).Contains("stringer") } + +func TestNilInterface(t *testing.T) { + var buf bytes.Buffer + type myStruct struct { + Data interface{} + } + logger := slog.New(slog.NewJSONHandler(&buf, &slog.HandlerOptions{ + ReplaceAttr: masq.New(), + })) + logger.Info("hello", slog.Any("test", myStruct{})) + gt.S(t, buf.String()).Contains("null") +}