Skip to content

Commit

Permalink
Merge pull request #344 from harshanarayana/bug/GIT-343/logr-handle-m…
Browse files Browse the repository at this point in the history
…arshalling-non-string-values

kvlistformat: fix the issue with display marshalled value for non string type
  • Loading branch information
k8s-ci-robot committed Aug 4, 2022
2 parents 0990e81 + dcddc5f commit d64acbd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/serialize/keyvalues.go
Expand Up @@ -145,7 +145,7 @@ func KVListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
case string:
writeStringValue(b, true, value)
default:
writeStringValue(b, false, fmt.Sprintf("%+v", v))
writeStringValue(b, false, fmt.Sprintf("%+v", value))
}
case []byte:
// In https://github.com/kubernetes/klog/pull/237 it was decided
Expand Down
29 changes: 29 additions & 0 deletions internal/serialize/keyvalues_test.go
Expand Up @@ -39,13 +39,42 @@ func (p point) String() string {
return fmt.Sprintf("x=%d, y=%d", p.x, p.y)
}

type dummyStruct struct {
key string
value string
}

func (d *dummyStruct) MarshalLog() interface{} {
return map[string]string{
"key-data": d.key,
"value-data": d.value,
}
}

type dummyStructWithStringMarshal struct {
key string
value string
}

func (d *dummyStructWithStringMarshal) MarshalLog() interface{} {
return fmt.Sprintf("%s=%s", d.key, d.value)
}

// Test that kvListFormat works as advertised.
func TestKvListFormat(t *testing.T) {
var emptyPoint *point
var testKVList = []struct {
keysValues []interface{}
want string
}{
{
keysValues: []interface{}{"data", &dummyStruct{key: "test", value: "info"}},
want: " data=map[key-data:test value-data:info]",
},
{
keysValues: []interface{}{"data", &dummyStructWithStringMarshal{key: "test", value: "info"}},
want: ` data="test=info"`,
},
{
keysValues: []interface{}{"pod", "kubedns"},
want: " pod=\"kubedns\"",
Expand Down

0 comments on commit d64acbd

Please sign in to comment.