Skip to content

Commit

Permalink
klogr: report missing values with "(MISSING)"
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pohly committed Feb 14, 2022
1 parent c734dc7 commit cda3be7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
4 changes: 4 additions & 0 deletions internal/serialize/keyvalues.go
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions klogr/klogr_test.go
Expand Up @@ -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=<nil>
expectedKlogOutput: `"test" akey="avalue" akey2="(MISSING)"
`,
},
"should correctly html characters": {
Expand All @@ -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=<nil> akey="avalue" akey2=<nil>
expectedKlogOutput: `"test" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)"
`,
},
"should correctly print regular error types": {
Expand Down
10 changes: 2 additions & 8 deletions test/output_test.go
Expand Up @@ -42,15 +42,9 @@ func TestKlogrOutput(t *testing.T) {
`I output.go:<LINE>] "test" keyWithoutValue="(MISSING)"
I output.go:<LINE>] "test" keyWithoutValue="(MISSING)" anotherKeyWithoutValue="(MISSING)"
I output.go:<LINE>] "test" keyWithoutValue="(MISSING)"
`: `I output.go:<LINE>] "test" keyWithoutValue=<nil>
`: `I output.go:<LINE>] "test" keyWithoutValue="(MISSING)"
I output.go:<LINE>] "test" keyWithoutValue="anotherKeyWithoutValue"
I output.go:<LINE>] "test" keyWithoutValue=<nil>
`,
`I output.go:<LINE>] "test" akey="avalue" akey2="(MISSING)"
`: `I output.go:<LINE>] "test" akey="avalue" akey2=<nil>
`,
`I output.go:<LINE>] "test" basekey1="basevar1" basekey2="(MISSING)" akey="avalue" akey2="(MISSING)"
`: `I output.go:<LINE>] "test" basekey1="basevar1" basekey2=<nil> akey="avalue" akey2=<nil>
I output.go:<LINE>] "test" keyWithoutValue="(MISSING)"
`,
}
Output(t, OutputConfig{
Expand Down

0 comments on commit cda3be7

Please sign in to comment.