From 2270be6d95be8db45f685fd3a310273b6895bb86 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Wed, 9 Feb 2022 16:53:28 +0100 Subject: [PATCH] klogr: report missing values with "(MISSING)" klogr had a special value for a missing value in a key/value pair, so the intention probably was to use that as replacement, the same way as klog does it. But because of a bug in the code which removed duplicates, it treated a missing value like nil. Now the behavior is consistent with klog. --- internal/serialize/keyvalues.go | 4 ++++ klogr/klogr_test.go | 8 ++++---- test/output_test.go | 10 ++-------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/internal/serialize/keyvalues.go b/internal/serialize/keyvalues.go index 479a37351..c3211ec58 100644 --- a/internal/serialize/keyvalues.go +++ b/internal/serialize/keyvalues.go @@ -36,7 +36,11 @@ func TrimDuplicates(kvLists ...[]interface{}) [][]interface{} { // initialise this output slice outs[i] = []interface{}{} // obtain a reference to the kvList we are processing + // and make sure it has an even number of entries kvList := kvLists[i] + if len(kvList)%2 != 0 { + kvList = append(kvList, missingValue) + } // start iterating at len(kvList) - 2 (i.e. the 2nd last item) for // slices that have an even number of elements. diff --git a/klogr/klogr_test.go b/klogr/klogr_test.go index 7775b436e..45565b271 100644 --- a/klogr/klogr_test.go +++ b/klogr/klogr_test.go @@ -113,9 +113,9 @@ func testOutput(t *testing.T, format string) { klogr: new(), text: "test", keysAndValues: []interface{}{"akey", "avalue", "akey2"}, - expectedOutput: ` "msg"="test" "akey"="avalue" "akey2"=null + expectedOutput: ` "msg"="test" "akey"="avalue" "akey2"="(MISSING)" `, - expectedKlogOutput: `"test" akey="avalue" akey2= + expectedKlogOutput: `"test" akey="avalue" akey2="(MISSING)" `, }, "should correctly html characters": { @@ -131,9 +131,9 @@ func testOutput(t *testing.T, format string) { klogr: new().WithValues("basekey1", "basevar1", "basekey2"), text: "test", keysAndValues: []interface{}{"akey", "avalue", "akey2"}, - expectedOutput: ` "msg"="test" "basekey1"="basevar1" "basekey2"=null "akey"="avalue" "akey2"=null + expectedOutput: ` "msg"="test" "basekey1"="basevar1" "basekey2"="(MISSING)" "akey"="avalue" "akey2"="(MISSING)" `, - expectedKlogOutput: `"test" basekey1="basevar1" basekey2= akey="avalue" akey2= + expectedKlogOutput: `"test" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)" `, }, "should correctly print regular error types": { diff --git a/test/output_test.go b/test/output_test.go index 9b3161761..ddb47044b 100644 --- a/test/output_test.go +++ b/test/output_test.go @@ -42,15 +42,9 @@ func TestKlogrOutput(t *testing.T) { `I output.go:] "test" keyWithoutValue="(MISSING)" I output.go:] "test" keyWithoutValue="(MISSING)" anotherKeyWithoutValue="(MISSING)" I output.go:] "test" keyWithoutValue="(MISSING)" -`: `I output.go:] "test" keyWithoutValue= +`: `I output.go:] "test" keyWithoutValue="(MISSING)" I output.go:] "test" keyWithoutValue="anotherKeyWithoutValue" -I output.go:] "test" keyWithoutValue= -`, - `I output.go:] "test" akey="avalue" akey2="(MISSING)" -`: `I output.go:] "test" akey="avalue" akey2= -`, - `I output.go:] "test" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)" -`: `I output.go:] "test" basekey1="basevar1" basekey2= akey="avalue" akey2= +I output.go:] "test" keyWithoutValue="(MISSING)" `, } Output(t, OutputConfig{