diff --git a/internal/serialize/keyvalues.go b/internal/serialize/keyvalues.go index f85d7ccf..ad6bf111 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 d4b33851..1bf4842b 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\"",