From 1fe65a0cac0fc0d109af49ada24a3f74bbef7b06 Mon Sep 17 00:00:00 2001 From: francois samin Date: Mon, 2 May 2022 11:56:06 +0200 Subject: [PATCH] fix: dont dump detailled map of the target if the target is a map Signed-off-by: francois samin --- dump_test.go | 16 ++++++++++++++++ encoder.go | 6 ++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/dump_test.go b/dump_test.go index 05a8629..1dbd367 100644 --- a/dump_test.go +++ b/dump_test.go @@ -694,6 +694,22 @@ func Test_DumpTime(t *testing.T) { } +func Test_DumpMap(t *testing.T) { + m := map[string]interface{}{ + "string": "foobar", + } + e := dump.NewDefaultEncoder() + e.ExtraFields.Len = true + e.ExtraFields.Type = true + e.ExtraFields.DetailedStruct = true + e.ExtraFields.DetailedMap = true + e.ExtraFields.DetailedArray = true + result, err := e.ToStringMap(m) + require.NoError(t, err) + t.Log(result) + require.Len(t, result, 3) +} + func Test_DumpTimeWithDetailledStruct(t *testing.T) { m := map[string]interface{}{ "string": "foobar", diff --git a/encoder.go b/encoder.go index c97fb2d..15a4983 100644 --- a/encoder.go +++ b/encoder.go @@ -263,8 +263,10 @@ func (e *Encoder) fDumpMap(w map[string]interface{}, i interface{}, roots []stri w[nodeLenFormatted] = lenKeys } if e.ExtraFields.DetailedMap { - structKey := strings.Join(sliceFormat(roots, e.Formatters), e.Separator) - w[structKey] = i + if len(roots) != 0 { + structKey := strings.Join(sliceFormat(roots, e.Formatters), e.Separator) + w[structKey] = i + } } return nil }