Skip to content

Commit

Permalink
Merge pull request #16 from m-mizutani/fix/nil-error
Browse files Browse the repository at this point in the history
Add handling for nil interface in clone function
  • Loading branch information
m-mizutani authored Feb 26, 2024
2 parents ccdd4ed + a2fc6f4 commit 89175da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 89175da

Please sign in to comment.