Skip to content

Commit d64acbd

Browse files
authored
Merge pull request #344 from harshanarayana/bug/GIT-343/logr-handle-marshalling-non-string-values
kvlistformat: fix the issue with display marshalled value for non string type
2 parents 0990e81 + dcddc5f commit d64acbd

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

internal/serialize/keyvalues.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func KVListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
145145
case string:
146146
writeStringValue(b, true, value)
147147
default:
148-
writeStringValue(b, false, fmt.Sprintf("%+v", v))
148+
writeStringValue(b, false, fmt.Sprintf("%+v", value))
149149
}
150150
case []byte:
151151
// In https://github.com/kubernetes/klog/pull/237 it was decided

internal/serialize/keyvalues_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,42 @@ func (p point) String() string {
3939
return fmt.Sprintf("x=%d, y=%d", p.x, p.y)
4040
}
4141

42+
type dummyStruct struct {
43+
key string
44+
value string
45+
}
46+
47+
func (d *dummyStruct) MarshalLog() interface{} {
48+
return map[string]string{
49+
"key-data": d.key,
50+
"value-data": d.value,
51+
}
52+
}
53+
54+
type dummyStructWithStringMarshal struct {
55+
key string
56+
value string
57+
}
58+
59+
func (d *dummyStructWithStringMarshal) MarshalLog() interface{} {
60+
return fmt.Sprintf("%s=%s", d.key, d.value)
61+
}
62+
4263
// Test that kvListFormat works as advertised.
4364
func TestKvListFormat(t *testing.T) {
4465
var emptyPoint *point
4566
var testKVList = []struct {
4667
keysValues []interface{}
4768
want string
4869
}{
70+
{
71+
keysValues: []interface{}{"data", &dummyStruct{key: "test", value: "info"}},
72+
want: " data=map[key-data:test value-data:info]",
73+
},
74+
{
75+
keysValues: []interface{}{"data", &dummyStructWithStringMarshal{key: "test", value: "info"}},
76+
want: ` data="test=info"`,
77+
},
4978
{
5079
keysValues: []interface{}{"pod", "kubedns"},
5180
want: " pod=\"kubedns\"",

0 commit comments

Comments
 (0)