From c043034706941058254f9ca9801b0ad5b76800fa Mon Sep 17 00:00:00 2001 From: Kyle Reynolds Date: Wed, 6 Mar 2024 14:24:10 -0700 Subject: [PATCH] adding support in fields for map string fix for issue 648 --- fields.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/fields.go b/fields.go index 23606ddd..6fec8965 100644 --- a/fields.go +++ b/fields.go @@ -15,11 +15,13 @@ func isNilValue(i interface{}) bool { func appendFields(dst []byte, fields interface{}, stack bool) []byte { switch fields := fields.(type) { case []interface{}: + // Existing handling for []interface{} if n := len(fields); n&0x1 == 1 { // odd number fields = fields[:n-1] } dst = appendFieldList(dst, fields, stack) case map[string]interface{}: + // Existing handling for map[string]interface{} keys := make([]string, 0, len(fields)) for key := range fields { keys = append(keys, key) @@ -30,6 +32,19 @@ func appendFields(dst []byte, fields interface{}, stack bool) []byte { kv[0], kv[1] = key, fields[key] dst = appendFieldList(dst, kv, stack) } + case map[string]string: + // New handling for map[string]string + keys := make([]string, 0, len(fields)) + for key := range fields { + keys = append(keys, key) + } + sort.Strings(keys) + for _, key := range keys { + val := fields[key] + // Directly append the string value since we know the type + dst = enc.AppendKey(dst, key) + dst = enc.AppendString(dst, val) + } } return dst }