Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kvlistformat: fix the issue with display marshalled value for non string type #344

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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