From dcddc5f51aea4b875ac0026e7bb7671da8fb1fa0 Mon Sep 17 00:00:00 2001 From: Harsha Narayana Date: Wed, 3 Aug 2022 18:18:45 +0530 Subject: [PATCH] kvlistformat: fix the issue with display marshalled value for non string type --- internal/serialize/keyvalues.go | 2 +- internal/serialize/keyvalues_test.go | 29 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/internal/serialize/keyvalues.go b/internal/serialize/keyvalues.go index f85d7ccf8..ad6bf1116 100644 --- a/internal/serialize/keyvalues.go +++ b/internal/serialize/keyvalues.go @@ -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 diff --git a/internal/serialize/keyvalues_test.go b/internal/serialize/keyvalues_test.go index d4b338519..1bf4842b7 100644 --- a/internal/serialize/keyvalues_test.go +++ b/internal/serialize/keyvalues_test.go @@ -39,6 +39,27 @@ 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 @@ -46,6 +67,14 @@ func TestKvListFormat(t *testing.T) { 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\"",